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 20:08] – [Template inheritance] Martijn Luinstra | documentation:rack:reference:templates [2023/08/31 23: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 '' |
- | ===== Context | + | <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:// | 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:// | ||
- | <PHP> | + | <code php> |
$context = [ | $context = [ | ||
' | ' | ||
' | ' | ||
]; | ]; | ||
- | </PHP> | + | </code> |
and the template | and the template | ||
- | <PHP> | + | <code php> |
< | < | ||
< | < | ||
Line 26: | Line 28: | ||
</ | </ | ||
</ | </ | ||
- | </PHP> | + | </code> |
which renders as | which renders as | ||
- | <HTML> | + | <WRAP box round> |
- | <div style=" | + | ====== Home ====== |
- | <h1>Home</h1> | + | page_id: index |
- | page_id: index | + | </WRAP> |
- | </div> | + | |
- | </HTML>\\ | + | |
===== Template functions ===== | ===== Template functions ===== | ||
- | The following | + | All global |
- | * <php>html($data)</ | + | |
- | * <php>attr($data)</ | + | * '' |
- | * <php>format_plain_text($text)</ | + | * '' |
- | * < | + | |
- | **WARNING! No data is escaped by default. Always escape your template variables. | + | Remember! No data is escaped by default. Always |
The following snippet shows how these functions should be used. | The following snippet shows how these functions should be used. | ||
- | <PHP> | + | <code php> |
< | < | ||
< | < | ||
<?= $this-> | <?= $this-> | ||
- | | + | Author: <a href="<? |
- | | + | |
</ | </ | ||
- | </PHP> | + | </code> |
Line 64: | 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 91: | Line 89: | ||
</ | </ | ||
- | ===== More functionality | + | ===== More control structures |
- | These templates are parsed and executed as PHP and therefore can execute arbitrary PHP code. However, it is advised to keep it simple in order to maintain separation between rendering and internal logic. | + | Rack templates are parsed and executed as PHP and therefore can execute arbitrary PHP code. However, it is advised to keep it simple in order to maintain separation between rendering and internal logic. |
By convention, templates use the [[http:// | By convention, templates use the [[http:// |