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
Every site has one folder, htdocs/_fegg/site/template/<site>/ (main is the site at /). The CMS's folder cms/ has the same shape:
site/template/
main/
tpl/ # templates (index.tpl, template/siteframe.tpl, include/, news/ ...)
scss/ # styles, built into css/ by npm run build:scss
css/ js/ img/ # served from /_fegg/site/template/main/ ({{ $site.assets }}); .htaccess
cms/
tpl/ # the project's CMS templates, and overrides of core/template/cms/ (without its cms/)
scss/ # unique.scss, the project's CMS styles
| 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. |
Template names never include the .tpl extension and are relative to the tpl/ of the site the URL belongs to. A controller renders site/template/main/tpl/news/detail.tpl with:
$this->displayPage('news/detail');
The CMS's templates keep their cms/ names: displayPage('cms/news/lists') reads site/template/cms/tpl/news/lists.tpl, then core/template/cms/news/lists.tpl. To change a core template, copy it to site/template/cms/tpl/ at its path under core/template/cms/ and edit the copy: the project file wins.
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/main/tpl/<language code>/news/detail.tpl.
In include and transclude, prefer paths starting with / (relative to the site's tpl/): see Include.
Sites
A project can hold several sites: the corporate site at /, a recruiting site at /recruit/, landing pages. site/config/sites.php lists them, one line each:
$sites['main'] = array('path' => '/');
$sites['recruit'] = array('path' => '/recruit/', 'info' => array('name' => '採用サイト', 'title' => '採用情報 | 会社名'));
- A request belongs to the site whose path is the longest match of its URL. The CMS (
/cms/) belongs to the site at/. ./fegg-cli site:create recruit /recruit/createssite/template/recruit/from a starting folder and adds the lines tosites.phpandroute.php../fegg-cli site:listlists the sites.'info'overridessite_info.php(what every site shares) for that site.- Templates get
$site.key(recruit),$site.home(/recruit/) and$site.assets(/_fegg/site/template/recruit):
<link rel="stylesheet" href="{{ $site.assets }}/css/style.css?v={{ $site.version }}">
<img src="{{ $site.assets }}/img/views/about/pc/main.jpg" alt="">
- Only the CSS, JS, images and fonts in
css/,js/,img/andfont/are served (the root.htaccessand the folder's own.htaccessallow them).tpl/,scss/and anything else stay denied like the rest of_fegg/.css/is built and not tracked: put a library's CSS next to its JS (js/vendor/swiper/). npm run build:scssbuilds every folder'sscss/*.scssinto itscss/(the CMS'sunique.scsstoo). A new site needs no configuration.- Sites share no SCSS: each has its own foundation (values, mixins, breakpoints).
- Controllers are found by the URL as before:
/recruit/jobs/issite/src/Controller/Recruit/Jobs.php, or a route toPage($route['recruit/jobs/'] = 'page/render/jobs'rendersrecruit/tpl/jobs.tpl, see Static pages).
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.
Application::displayPage
Display a page with the standard template variables and end the response
Application::fetchPage
Return a page rendered with the standard template variables as a string
Application::displayTemplate
Output a template with only the variables you pass
Application::fetchTemplate
Return a template rendered with only the variables you pass, as a string
Application::displayJson
Send data as JSON and end the response
Template helper methods
Template helper methods pass special values (site information, hidden inputs, HTTP headers) from the controller.
Application::setSiteinfo
Set website information (title, description, keywords …)
Application::setHidden
Set values for hidden inputs
Application::setHtmlHeader
Send an HTTP response header
Template rules
The tags you can write in a template.
Variable
Output variables
Assign
Set a variable in the template
If
if, else if, else
Loop
foreach, loop
Include
include, include head, head, include foot, foot, include html
Transclude
transclude, section
Base URL
base, baseurl, currenturl
Checked/Selected
checked, selected
Options
Output option tags
Hidden
Output hidden inputs
Code
Run a PHP statement
Call
Run a controller method
Comment
Template comments
Literal braces
Output {{ and }} as text
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