Page Auto-Functions
Overview
Auto-functions are functions that are called automatically when a page loads, if they follow the given naming conventions.
This helps you define entry points for your app in a consistent way.
Return Values
If an auto-function returns a value, the page will do one of the following:
-
url
TypeString: Redirect to that URL - Map: Respond with JSON payload
main
Auto-Method
By default, if your page has a main
function, it will automatically be called when the page is loaded.
F main() { Response.sendPage({ title: 'Hello!', body: mainHtml() }); } template mainHtml() { <b>Hello World!</b> }
post
Auto-Functions
If the page is being loaded as a POST
HTTP request, it will call a post()
function if it is present.
This is useful for forms, where a page could be called as either a GET
method (default, show the form) or POST
(submitted form data).
// URL: /user/edit // Only runs if this is a `get` request. F main() { printForm(); } // Only runs if this is a `post` request. F post() { $email = Input.post('email'); // (update email in database) return url'/user'; // redirect }
mode-
Auto-Functions
The page will automatically call a F that is prefixed with mode-
if there is a param named mode
in the request.
The mode name can come from one of two sources:
- Route param (defined in app.jcon)
-
POST
input param
This can be useful for REST or Ajax endpoints.
//--- File: settings/app.jcon --- routes: { /article/{mode}: article.tht ... } //--- File: pages/article.tht --- // url: `/article/delete` F modeDelete() { // ... return { status: 'deleted' }; // JSON response } // url: `/article/add-to-favorites` F modeAddToFavorites() { // ... return { status: 'added' }; }