Twig 3 is the second template engine of yf, next to the own .stpl engine. It is optional and loaded on first use (require_php_lib('twig'), locked to Twig 3.30 in services/twig.lock.json). The Twig language itself: twig.symfony.com. API: API reference / yf_tpl.

Picking the engine

tpl()->parse('twig:user/account', $replace);            // one template, by a driver prefix
tpl()->parse('user/account', $replace, ['driver' => 'twig']);
tpl()->parse_string('Hello {{ name }}', ['name' => 'Ann'], 'hello', ['driver' => 'twig']);
tpl()->_set_default_driver('twig');                        // every template of the request
$PROJECT_CONF['tpl']['DRIVER_NAME'] = 'twig';              // the site's default (default: 'yf')

A name is a yf template name: twig:user/account reads templates/<theme>/user/account.stpl through the usual layers (site, app, project, plugins, framework) and renders its text as Twig. The .stpl extension does not decide the engine; the driver does.

.twig files

Files named *.twig live in the same template folders. {% include %}, {% extends %}, {% embed %} and {% import %} find them on the driver's paths (the theme folders of the side, the current module's folders, the project root): {% extends 'layouts/base.twig' %}. A name the paths do not hold is looked up as a yf template name ({% include 'user/account' %}), or as a template parsed earlier with parse_string().

A name ending in .twig renders that file (the theme folders of the site, app, project and framework are on the driver's paths, the site first):

tpl()->parse('pages/home.twig', $replace);                // templates/<theme>/pages/home.twig, by its extension
tpl()->parse('twig:pages/home.twig', $replace);           // the same with the prefix
tpl()->exists('pages/home.twig');                         // true when the file is on the driver's paths
$PROJECT_CONF['tpl_driver_twig']['FILES_BY_NAME'] = false; // before 2026-11-21: such a name read <name>.stpl

A name ending in .twig picks the twig driver by itself (tpl TWIG_BY_EXT, on since 2026-11-21): parse(), parse_if_exists() and exists() take it as with the twig: prefix. The yf: prefix or ['driver' => 'yf'] keeps the yf engine for one call, and $PROJECT_CONF['tpl']['TWIG_BY_EXT'] = false (or the admin's Settings page, New features tab) for the whole site (the yf engine then reads <name>.stpl, as before). tpl()->get() and the yf engine's own {include()} are not changed: they read yf templates.

What yf adds to Twig

  • {{ t('Hello %name', {'%name': user.name}) }} and {{ 'Hello %name'|trans({'%name': user.name}) }}: the yf translation t(). The result is escaped as any value. The locale editor's Collect finds these strings in .twig files (option find_twig).
  • {{ exec(['module', 'method', arg1, arg2]) }}: the output of a module method (else a class of _class()), not escaped, like the yf engine's {execute()}.
  • {{ url('/@object/show') }}: the site URL of yf url() (escaped).
  • {{ asset('jquery') }}, {{ js('code or URL') }}, {{ css('code or URL') }}: add to the page's assets as the yf engine's {asset()}, {js()}, {css()}; they print nothing.
  • {{ conf('name') }}: a setting, read-only. A name that looks like a secret (pass, secret, token, key, auth...) gives ***, and so do the secret keys of an array value; there is no way to read the whole settings array.
  • template_from_string() (Twig's StringLoaderExtension); dump() with DEBUG_MODE or a dev environment.
  • The global tags of tpl() (tpl()->_global_tags) are variables of every template.

A site adds its own functions, filters or extensions on the environment; one of the same name wins over yf's:

_class('tpl_driver_twig', 'classes/tpl/')->env->addFunction(new \Twig\TwigFunction('price', fn ($n) => number_format($n, 2)));

Escaping

Twig escapes every printed value for HTML ({{ value }}); |raw prints a value as it is, |e('js'), |e('url') and |e('html_attr') escape for other contexts. String literals written in the template are not escaped. Never mark request data |raw.

Cache and development

With DEBUG_MODE or a dev environment, templates are compiled on every change and not cached. Otherwise Twig compiles them into STORAGE_PATH/twig_cache/ and does not look at the files again: deploy steps empty that folder (the cache purge of the admin does it, and so does a save of a .twig file in the admin's template manager). ['no_cache' => true] in the parse params skips the compiled cache for one call.

Checking templates

php vendor/bin/yf tpl:check --twig     # the .stpl rules, and Twig's parser on every .twig file; exit 1 on an error

In code: _class('tpl_check')->check_twig($text, $name) returns ['errors' => [], 'warnings' => []] (a syntax error is an error, an unknown function or filter a warning). Nothing of the template runs. In the admin, the template manager lists, edits (Ace's twig mode), checks and previews .twig files.

A .stpl file whose text is Twig is known by its callers: when code renders it with the twig driver (tpl()->parse('twig:user/account'), ['driver' => 'twig'], a call on the twig driver itself) or a Twig template includes it, tpl:check, the admin's template manager and the locale editor's Collect read it as Twig. tpl:check --no-twig-callers checks every .stpl file with the stpl rules.

With DEBUG_MODE the debug console's Templates tab lists the Twig renders of the page (driver twig, calls, time, size, and whether the text came from a .twig file, a string or a yf template name).