About Template
About Template
Fegg has its own template engine that keeps the application code (PHP) apart from the display (HTML/CSS). A template (.tpl) is HTML with {{ ... }} tags. Fegg compiles it to PHP, caches the result and runs it when a controller displays the page.
{{ transclude '/template/siteframe' }}
{{ section contents }}
<h1>{{ $page.data.title }}</h1>
<p>{{ $page.data.post_date|feggDate:'Y/m/d' }}</p>
{{ end section contents }}
Where templates live
| Directory | Owner |
|---|---|
htdocs/_fegg/site/template/ | The project. Put your templates here. |
htdocs/_fegg/core/template/ | The core (CMS screens). Never edit it: it is replaced by fegg-cli core:update. |
Templates are looked up in site/template/ first, then in core/template/. To change a core template, copy it to the same path under site/template/ and edit the copy: the project file wins.
Template names never include the .tpl extension. A controller renders site/template/news/detail.tpl with:
$this->displayPage('news/detail');
When the site is multi-language (default_language and support_language are set in the settings), the language code is put in front of the name: news/detail is read from site/template/<language code>/news/detail.tpl.
Paths in include and transclude
- A path starting with
/is relative to the template root (site/template/, thencore/template/). It works the same in a template at any depth. Prefer it. - Any other path is relative to the directory of the template that contains the tag (
./parts/card,../template/siteframe).
{{ transclude '/template/siteframe' }} site/template/template/siteframe.tpl
{{ transclude '/cms/frame/siteframe' }} core/template/cms/frame/siteframe.tpl (CMS screens)
{{ include head '/include/head/about' }} site/template/include/head/about.tpl
{{ include './parts/card' }} parts/card.tpl next to this template
Cache
Compiled templates are cached in htdocs/_fegg/site/data/cache/template/, which must be writable by the web server. A cache file is rebuilt automatically when the template, one of its transclude parents or the template compiler changes (any change of modification time or size), so you never need to clear it by hand.
Checking templates
After writing or editing a template, run:
./fegg-cli template:lint
It compiles every template without running it and reports unknown tags, unclosed blocks, missing include / transclude targets, undefined modifiers and unsafe outputs, with the .tpl line. See Lint and errors.
Display methods
Display methods render a template from a controller. Use displayPage for pages: it sets the variables templates expect and protects against clickjacking.
Template helper methods
Template helper methods pass special values (site information, hidden inputs, HTTP headers) from the controller.
Template rules
The tags you can write in a template.
Modifiers, security and lint
Modifiers transform a value before it is output. The security page explains which escaping each place in the HTML needs, and the lint page lists every check of fegg-cli template:lint and how template errors are reported.
Modifiers
Modifiers transform a value before it is output.
Security
Escaping by context, and templates as code
Lint and errors
Check templates with fegg-cli template:lint, and find the template line of an error