Welcome to part one of our exploration into creating pages for the TotalGPT SaaS Website! In this post, we’ll begin building the Home Page, breaking down some essential Total.js concepts for beginners. By the end of this part, you'll have a functional home page, understand how routing works in Total.js, and learn how controllers and views come together to build dynamic web pages.
This part is crucial for understanding the fundamentals of Total.js, so we'll take the time to explain concepts thoroughly. In Part 2, we’ll expand by creating the remaining pages with fewer explanations since the groundwork will already be in place. Check the previous blog post if you missed it.
ROUTE()
global function?exports.install
a best practice?$
).index.html
.layout.html
and how to use @{layout()}
.In Total.js, routes are defined using the global ROUTE()
function. A route maps an HTTP method (e.g., GET, POST) and a URL path (e.g., /
, /about
) to a handler function.
Let’s start by creating a route for the home page.
default.js
ControllerROUTE()
ROUTE('HTTP_METHOD URL_PATH', HANDLER_FUNCTION)
.exports.install
function to ensure they’re loaded correctly.index.html
FileNow, let’s create the view file that the home
handler renders. Total.js expects view files to be located in the views
folder.
If you run the app and visit http://localhost:5000/
, you might expect to see "Hello World" displayed—but instead, you’ll encounter an error. Why?
By default, Total.js tries to render the index.html
file with a layout. Since we haven’t defined a layout.html
file yet, the framework doesn’t know how to handle the missing layout.
To render the view without a layout, you can explicitly disable the layout in one of two ways:
layout.html
FileFor a more professional setup, let’s create a layout file. The layout defines the overall structure of your pages, with the view content dynamically inserted where needed.
Here’s what’s happening:
@{body}
: This directive is part of the Total.js view engine. It serves as a placeholder where the content of the current view (e.g., index.html
) is injected.Now, with the layout in place, you can remove the @{layout('')}
directive from index.html
and use the layout for all your pages automatically.
During development, Total.js runs in debug mode, meaning the app automatically reloads whenever you make changes to your files. This is because we’ve set options.release = false
in our index.js
file.
Here’s how it works:
controllers/*
).In production, debug mode is disabled by setting options.release = true
, ensuring better performance and stability.
Now that everything is set up, start your Total.js server and visit http://localhost:5000/
. You should see "Hello World" displayed in your browser, styled with the layout we defined.
In Part 2, we’ll reshape the home page and create the remaining pages:
We’ll also begin introducing additional functionality, such as dynamic content and multi-language support.
Stay tuned as we continue to build the TotalGPT SaaS Website step by step! 🚀