Template basics


Basic syntax

The syntax follows exactly Django’s templating engine. Just read the docs there and come back when you’re ready.

A quick summary: the template system allows you to completely separate program logic from the design – as is the basis of the MVC pattern. Templates have two basic elements: {% tags %} and {{variables}}.

Some more advanced uses are filtered variables {{variable|striptags}}, filtered variables with parameters {{variable|truncate:30}}, chained filters {{variable|striptags|truncate:30}}, configuration or language variables {{#language_variable#}}, and filtered configuration variables {{#config_variable#|escape}}.

More details can be found in the following sections and the submenus on the left.

The difference between tags, variables, and filters?

Tags are functions that control the flow of execution and/or produce output. A good example would be the {% foreach %} tag which replicates the functionality of the similarly named PHP loop.

Variables are simply a different syntax for $variables in PHP. Placing a variable in a template will be the equivalent of echoing it in PHP, so {{example}} is the same as echo $this->zajlib->variable->example. You can pass variables as parameters of tags, so {% foreach examples as example %} is valid syntax.

Filters on the other hand are tiny functions that change the value of a variable just before output. A good example of this is the truncate filter: {{a_very_long_title|truncate:30}} – this will modify the value of a_very_long_title by truncating it to 30 characters just before output. Unlike functions, you can chain filters, like so: {{a_very_long_title|striptags|truncate:30}} – here the actions will be performed in order from left to right.

Displaying templates

You can display templates by calling the template show method:

$this->zajlib->template->show('home.html')

Cacheing and clearing the cache

An important note about cacheing: since Outlast Framework compiles all template code into native PHP, the cache needs to be cleared each time you update any of your view files. During development you can enable debug_mode which will force this to happen automatically on each request.

In a production environment you’ll need to run the template cache update (via the http://www.example.com/update/ menu) to clear the cache. We recommend setting up a deploy script which will run this for you each time you deploy to production.

Other template systems or native php

You are not required to use the built-in template system. You can use another system like Smarty or just use any standard PHP method for displaying output. However, the template system is so well integrated into the framework that you are really missing out on some of the best features of OFW by not using it.

Outlast Web & Mobile Development (c) 2018 | Privacy Policy |