Understanding controllers

General concept

Controllers are using for associate site's public URL with particular template.

A list of controllers for particular site

Name of each template contains the site key and template file name itself separated by colon. For example, tabbli:index.html. If you need more flexible configuration (during development a solution template for example) you can specify site key as a variable: {{site.key}}:index.html.

Controller options

For each controller you should specify a url path. You can also use regular expressions for specify url patterns if you need not just single page but a set of dynamic pages. For example, you can use a pattern like /products/(?P<key>[\w\d-]+)/ for crating controller which can generate a set of pages. In such case, corresponding url chunks associated with groups are available as global objects inside a template.

So, if you have such controller, which describes the parameter key, you can use the corresponding value inside the template for querying the data:

product.html
{% set product = DB(request)
    .get_collection('products').get_record(key, or404=True) %}
<h1>{{ product }}</h1>

Template name attribute can be also described as template. For example, your have the next templates which describes a number of pages:

  • docs/about_us.html

  • docs/contacts.html

  • docs/terms.html

  • docs/privacy.html

To make these pages public you do not need to create separate controllers for each template. You can create only one controller with url path pattern as /docs/(?P<path>[\w\d-/]+)/ and then you can specify template name as your_site:docs/{{path}}.html . So, Tabbli will use different templates for different pages. If you try to open a page with unavailable corresponding template the 404 error will appairs.

Additional options

You can also create use some extra options for setup access restrictions:

  • AJAX only - make controller accessable obly via AJAX requests (in other case the "403 Response forbidden" error will be raised).

  • Allow loading via IFRAME - change some server response HTTP headers for allowing to load the page via iframe on third-party domain (useful for widgets).

  • Login required - make a redirect to the login page if a user is not logged in, so it block an access to the page for unauthorized users.

Last updated

Was this helpful?