Skip to main content
Version: Fegg 5.x

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.

htdocs/_fegg/site/template/main/tpl/news/detail.tpl
{{ 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
DirectoryOwner
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:

htdocs/_fegg/site/src/Controller/News.php
$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:

htdocs/_fegg/site/config/sites.php
$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/ creates site/template/recruit/ from a starting folder and adds the lines to sites.php and route.php. ./fegg-cli site:list lists the sites.
  • 'info' overrides site_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/ and font/ are served (the root .htaccess and the folder's own .htaccess allow 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:scss builds every folder's scss/*.scss into its css/ (the CMS's unique.scss too). 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/ is site/src/Controller/Recruit/Jobs.php, or a route to Page ($route['recruit/jobs/'] = 'page/render/jobs' renders recruit/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.

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.