Version: v0.5.1 - Beta.  We welcome contributors & feedback.  THanks!

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:

main Auto-Method 

By default, if your page has a main function, it will automatically be called when the page is loaded.

function 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.
function main() {
    printForm();
}

// Only runs if this is a `post` request.
function post() {
    let email = Input.post('email');
    // (update email in database)

    return url'/user';  // redirect
}

mode- Auto-Functions 

The page will automatically call a function 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:

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`
function modeDelete() {
    // ...
    return { status: 'deleted' };  // JSON response
}

// url: `/article/add-to-favorites`
function modeAddToFavorites() {
    // ...
    return { status: 'added' };
}

See Also