This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| documentation:rack:reference:templates [2018/05/25 17:44] – [Template inheritance] Martijn Luinstra | documentation:rack:reference:templates [2023/08/31 21:12] (current) – Martijn Luinstra | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Templates ====== | ====== Templates ====== | ||
| - | Every template is an instance of the <php>Template</ | + | Every template is an instance of the '' |
| + | |||
| + | <WRAP danger> | ||
| + | |||
| + | ===== Template variables (context) ===== | ||
| + | |||
| + | Templates are intitialized with a context object, which is an associative array. The individual elements of this array are made available in the templates as regular variables (see [[http:// | ||
| + | |||
| + | <code php> | ||
| + | $context = [ | ||
| + | ' | ||
| + | ' | ||
| + | ]; | ||
| + | </ | ||
| + | |||
| + | and the template | ||
| + | |||
| + | <code php> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | which renders as | ||
| + | |||
| + | <WRAP box round> | ||
| + | ====== Home ====== | ||
| + | page_id: index | ||
| + | </ | ||
| + | |||
| + | ===== Template functions ===== | ||
| + | |||
| + | All global functions are available in templates. The template class provides the following additional functions for escaping data to prevent XSS attacks. | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | Remember! No data is escaped by default. Always explicitly escape your template variables and be careful which data you access in templates. | ||
| + | |||
| + | The following snippet shows how these functions should be used. | ||
| + | |||
| + | <code php> | ||
| + | < | ||
| + | < | ||
| + | <?= $this-> | ||
| + | Author: <a href="<? | ||
| + | </ | ||
| + | </ | ||
| ===== Template inheritance ===== | ===== Template inheritance ===== | ||
| Line 7: | Line 62: | ||
| Rack templates support basic template inheritance, | Rack templates support basic template inheritance, | ||
| - | * <php>Template-> | + | * '' |
| - | * <php>Template-> | + | * '' |
| - | * <php>Template-> | + | * '' |
| Note that nested blocks are not supported and that blocks are only allowed in templates that extend a parent template. | Note that nested blocks are not supported and that blocks are only allowed in templates that extend a parent template. | ||
| Line 34: | Line 89: | ||
| </ | </ | ||
| + | ===== More control structures ===== | ||
| - | ===== Context ===== | + | Rack templates |
| - | + | ||
| - | Templates | + | |
| - | + | ||
| - | < | + | |
| - | $context = [ | + | |
| - | ' | + | |
| - | ' | + | |
| - | ]; | + | |
| - | </ | + | |
| - | + | ||
| - | and the template | + | |
| - | + | ||
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | < | + | |
| - | < | + | |
| - | | + | |
| - | </ | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | which renders as | + | |
| - | + | ||
| - | < | + | |
| - | <div style=" | + | |
| - | < | + | |
| - | page_id: index | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | ===== Template functions ===== | + | |
| + | By convention, templates use the [[http:// | ||