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 19:41] – [Context] 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 '' |
- | ===== Template inheritance ===== | + | <WRAP danger> |
- | Rack templates support basic template inheritance, | + | ===== Template variables (context) |
- | + | ||
- | * < | + | |
- | * < | + | |
- | * < | + | |
- | + | ||
- | Note that nested blocks are not supported and that blocks are only allowed in templates that extend a parent template. | + | |
- | + | ||
- | ===== 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 36: | 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 ===== | ||
+ | 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 ===== | ||
+ | |||
+ | Rack templates support basic template inheritance, | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Note that nested blocks are not supported and that blocks are only allowed in templates that extend a parent template. | ||
+ | |||
+ | The following snippets demonstrate the use of blocks and template inheritance. | ||
+ | |||
+ | <file php page.phtml> | ||
+ | <?php $this-> | ||
+ | |||
+ | <?php $this-> | ||
+ | Hello world! | ||
+ | <?php $this-> | ||
+ | </ | ||
+ | |||
+ | <file php layout.phtml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <? | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== More control structures ===== | ||
+ | |||
+ | 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:// |