Back to top

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
documentation:rack:reference:templates [2018/05/25 20:47] Martijn Luinstradocumentation: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</php> class defined in ''include/utils.php''. 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. This page covers the documentation of functionality that is specific to templates.+Every template is an instance of the ''Template'' class defined in ''include/utils.php''. 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. This page covers the documentation of functionality that is specific to templates.
  
-<HTML> +<WRAP danger>**WARNING!** No data is escaped by default. Always explicitly escape your template variables and be careful which data you access in templates.</WRAP>
-<div style="color: red"><strong>WARNING!</strong> No data is escaped by default. Always explicitly escape your template variables and be careful which data you access in templates. +
-</div> +
-</HTML>\\+
  
 ===== Template variables (context) ===== ===== Template variables (context) =====
Line 12: Line 9:
 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.net/manual/en/function.extract.php|extract]]). Consider the 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://php.net/manual/en/function.extract.php|extract]]). Consider the context 
  
-<PHP>+<code php>
 $context = [ $context = [
   'title' => 'Home',   'title' => 'Home',
   'page_id' => 'index'   'page_id' => 'index'
 ]; ];
-</PHP>+</code>
  
 and the template and the template
  
-<PHP>+<code php>
 <html> <html>
   <head>   <head>
Line 31: Line 28:
   </body>   </body>
 </html> </html>
-</PHP>+</code>
  
 which renders as which renders as
  
-<HTML+<WRAP box round
-<div style="padding: 1em; border: solid 1px #999; border-radius: 0.2em;"> +====== Home ====== 
-  <h1>Home</h1> +page_id: index 
-  page_id: index +</WRAP>
-</div> +
-</HTML>\\+
  
 ===== Template functions ===== ===== Template functions =====
Line 46: Line 41:
 All global functions are available in templates. The template class provides the following additional functions for escaping data to prevent XSS attacks. All global functions are available in templates. The template class provides the following additional functions for escaping data to prevent XSS attacks.
  
-  * <php>html($data)</php> Escapes data to be used as (part of) an HTML element's content. +  * ''%%html($data)%%'' Escapes data to be used as (part of) an HTML element's content. 
-  * <php>attr($data)</php> Escapes data to be used as (part of) an HTML element's attributes. +  * ''%%attr($data)%%'' Escapes data to be used as (part of) an HTML element's attributes. 
-  * <php>format_plain_text($text)</php> Escapes and wraps paragraphs in ''<p>'' elements.+  * ''%%format_plain_text($text)%%'' Escapes and wraps paragraphs in ''<p>'' elements.
  
 Remember! No data is escaped by default. Always explicitly escape your template variables and be careful which data you access in templates. Remember! No data is escaped by default. Always explicitly escape your template variables and be careful which data you access in templates.
Line 54: Line 49:
 The following snippet shows how these functions should be used. The following snippet shows how these functions should be used.
  
-<PHP>+<code php>
 <section> <section>
   <h1><?= $this->html($title) ?></h1>   <h1><?= $this->html($title) ?></h1>
   <?= $this->format_plain_text($some_text) ?>   <?= $this->format_plain_text($some_text) ?>
-  Author <a href="<?= $this->attr($url_to_author_page) ?>"><?= $this->html($author_name) ?></a>+  Author<a href="<?= $this->attr($url_to_author_page) ?>"><?= $this->html($author_name) ?></a>
 </section> </section>
-</PHP>+</code>
  
  
Line 67: Line 62:
 Rack templates support basic template inheritance, provided by the following functions: Rack templates support basic template inheritance, provided by the following functions:
  
-  * <php>Template->extends($template)</php> This function sets the parent template, where <php>$template</php> as a path relative to the current template. +  * ''%%Template->extends($template)%%'' This function sets the parent template, where <php>$template</php> as a path relative to the current template. 
-  * <php>Template->begin($block_name)</php> This function begins a new block. +  * ''%%Template->begin($block_name)%%'' This function begins a new block. 
-  * <php>Template->end()</php> This function ends a block.+  * ''%%Template->end()%%'' This function ends a block.
  
 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.

documentation/rack/reference/templates.1527274024.txt.gz · Last modified: 2022/04/03 16:00 (external edit)