Guide 00 · Developer docs

Core dna for site builders

Read this guide first. Core dna is hosted, so there is nothing to install; your repository holds only templates and front-end assets; and a new kind of page starts in the admin, with a layout. In half a day, from what the platform is to a page you built, deployed and rendered in Smarty or Twig.

L0
Unit 01 · What you are building on

Smarty and Twig are peers

Core dna renders server-side templates in Smarty or Twig. Each site uses one, and both receive the same variables and the same records under the same names. What is the same, the handful of differences, and why every template example in these docs is given in both.

Who this is for. Theme developers, and anyone who will copy a template example from these docs. After this page you will know that neither engine is the default option and the other a fallback, what the choice of engine changes, and what it leaves alone.

One engine per site

A site renders all its templates with one engine, chosen by the site setting Globals.template.engine: Smarty unless the site is set to Twig. It is not among the settings an administrator can change in the admin, so ask Core dna when a site needs the other engine. Templates rendered in the background, such as those of jobs, use the same engine as the site's pages.

The admin is the one exception: it is always rendered in Smarty, whichever engine the site uses. That matters only if you override one of the admin's own templates from your repository, which few sites do.

What is the same in both

  • Where templates live and how they are found. Both engines look in the site's templates/ folder first, then in the platform's own templates, so a site file overrides a platform file at the same path. See Template resolution order.
  • The variables. Every template receives the same platform variables under the same names: $_url in Smarty is _url in Twig. See The variables in every template. Templates that jobs render are the exception: they receive the job's own variables, such as event and runData, and no _url or _currentUser (Jobs and events).
  • The records and their methods. A page is the same object in both engines, with the same methods.
  • Everything outside the template: layouts, areas and components, the page cache, the APIs and jobs. Nothing but the template itself knows which engine a site uses.

The same three lines in each engine: an area of the page, escaped, and a link built from a route name.

Template code
{* The page's heading area, escaped, and a link to the sign-in page *}
<h1>{$page->getAreaText('heading')|escape}</h1>
<a href="{$_url->getPath('login')}">Sign in</a>
{# The page's heading area, escaped, and a link to the sign-in page #}
<h1>{{ page.getAreaText('heading')|e }}</h1>
<a href="{{ _url.getPath('login') }}">Sign in</a>

What differs

SmartyTwig
Tags{$page->getName()}, {if}…{/if}, {foreach}…{/foreach}{{ page.getName() }}, {% if %}…{% endif %}, {% for %}…{% endfor %}
EscapingNot automatic: add |escapeNot automatic either, because the platform turns Twig's autoescape off: add |e
Functions and filtersThe PHP functions the platform's security policy allows, called as functions, and Smarty's own modifiersTwig's own functions and filters, plus a few the platform adds
A template file that does not existThe output is the line Template not found: <path>, and the page still answers 200An error: a development environment shows it with a stack trace, production shows the error page with a 500
A variable that does not existPrints nothing, but PHP raises a warning, which a development environment shows on the page. Guard an optional variable with isset() or |defaultPrints nothing; strict variables are off
TranslationA translation function, modifier and block are registeredNo translation filter or tag is registered, so interface strings are handled differently

Smarty and Twig side by side compares the two construct by construct; Interface strings covers translation in each.

Every example in both

Every template example in these docs is given in both engines, with a switch above it. The switch remembers your choice, so pick your site's engine once and every example on every page follows it. When an example does not work on your site, check first that you copied the right engine's version: A snippet from the documentation does not work in my templates.

To find out which engine a site uses, see Which engine a site uses.

Go deeper

Was this page helpful?

Your answer goes to the docs team.