メインコンテンツまでスキップ
Version: Fegg 5.x(開発版)

Variable

Output variables

Description​

{{ $var }}
{{ $var.key.sub }}
{{ $var|modifier }}

{{ $var }} outputs a variable. Array elements are separated with a period: $var.key.sub is $var['key']['sub']. Numeric keys and keys that are PHP keywords work too ($rows.0.name, $item.new, $site.default). Square brackets take an expression as the key: $page.data[$id], $map[$key].name.

Values from the controller are set in $this->page and read as $page in the template. The other variables available in a page are listed in Application::fetchPage.

The output is escaped with htmlspecialchars (ENT_QUOTES), which is right for HTML text and quoted attribute values. Other places (URLs, JavaScript) need a modifier: see Security. To output trusted HTML as it is, use the noescape modifier (see Modifiers).

Nothing is output (and no error is raised) when the variable is not set, or when it is an array and no modifier is applied. Modifiers receive arrays, so {{ $tags|feggImplode:', ' }} and {{ $items|feggJs|noescape }} work; a chain whose result is still an array outputs nothing.

Inside a tag, $a.b is always array access. To concatenate strings in an expression (if, assign …), write the PHP operator with spaces: $a . $b.

Example​

Controller
$this->page['title'] = 'Hello';
$this->page['rows'] = array(
array('name' => 'first'),
);
$this->displayPage('template');
Template
{{ $page.title }}
{{ $page.rows.0.name }}
{{ $page.missing }}
Output
Hello
first

Escaping and noescape​

Controller
$this->page['foo'] = '<p>bar</p>';
Template
{{ $page.foo }}
{{ $page.foo|noescape }}
Output
&lt;p&gt;bar&lt;/p&gt;
<p>bar</p>
警告

Use noescape only for HTML you trust (CMS rich text written by administrators, messages from your own config). Never use it on request parameters ($in) or other user input.