The view layer: the template language beside Twig and Blade
This page is for developers who write Twig, Blade, Jinja or ERB templates. Exponential renders HTML with its own
template language: .tpl files that eZTemplate (lib/eztemplate/classes/eztemplate.php) compiles to PHP. It looks
like Smarty at first sight; Moving a template from Smarty and
What the template language is cover it in depth. Here are
the differences that matter most when you come from another framework.
Contents
- Syntax side by side
- Five differences that bite
- Layouts
- Includes, partials and components
- Extending the language
- Compilation and caches
- Can I use Twig instead?
Syntax side by side
| Task | Twig | Blade | Exponential template language |
|---|---|---|---|
| Print, escaped | {{ title }} | {{ $title }} | {$title|wash} |
| Print raw | {{ html|raw }} | {!! $html !!} | {$html} |
| Variable | {% set n = 3 %} | @php($n = 3) | {def $n = 3} (new), {set $n = 4} (change) |
| Condition | {% if a > 1 %} | @if($a > 1) | {if $a|gt( 1 )} ... {/if} |
| Loop | {% for p in pages %} | @foreach($pages as $p) | {foreach $pages as $p} ... {/foreach} |
| Attribute | page.parent.name | $page->parent->name | $page.parent.name |
| Filter or helper | name|upper | strtoupper($name) | $name|upcase |
| Arithmetic | a + b | $a + $b | sum( $a, $b ) or $a|sum( $b ) |
| Translation | 'Save'|trans | __('Save') | 'Save'|i18n( 'extension/expdemo' ) |
| URL | path( 'item', {id: 42} ) | route( 'item', 42 ) | 'expdemo/item/42'|ezurl |
| Comment | {# ... #} | {{-- ... --}} | {* ... *} |
Five differences that bite
From Smarty and the template language: the differences in depth:
- No automatic escaping. Output is never escaped by the engine; write
|washon everything that comes from content, URLs or request parameters. Rendered rich text is printed through its view template (attribute_view_gui) and must not be washed again. - No infix arithmetic or comparison.
{if $a > 1}does not do what it says; usegt,eq,and,sumand the other operators. - Unknown operators pass their input through with a logged error instead of failing the page, so a typo can print raw data. Watch the debug output while developing.
defandsetare strict:defcreates,setchanges; misuse writes a warning.- No PHP in templates. Logic goes into operators, fetch functions and the view.
Layouts
There is no {% extends %} or @extends. The siteaccess's pagelayout (pagelayout.tpl) is the base layout of
every page: the view renders its own template, and the pagelayout prints it with {$module_result.content}
(Responses). Where Twig would override a block of a parent
template, Exponential replaces a whole template by an override chosen by conditions such as the content class,
the node or the view mode (Template resolution).
Includes, partials and components
| Need | Use |
|---|---|
| A partial | {include uri='design:parts/menu.tpl' title=$node.name}; extra parameters become variables in the included template |
| Render a content item in a view mode (a "component" for content) | {node_view_gui content_node=$child view='line'} picks node/view/line.tpl and its overrides |
| Render one field | {attribute_view_gui attribute=$node.data_map.title} uses the datatype's view template |
| Keep variables apart | namespaces, set by the view functions (Namespaces) |
Extending the language
| Twig or Blade | Exponential |
|---|---|
| Twig filter or function, Blade directive | a template operator: a PHP class registered in extension/<ext>/autoloads/eztemplateautoload.php (Template operators) |
| Twig tag | a template function (Custom template functions) |
| View composer or a Twig global from a service | a fetch function (fetch( 'expdemo', 'stats', hash() )) (Getting data into templates) |
Compilation and caches
Templates are compiled to PHP files under the cache directory and rebuilt when the source changes
(Template compilation). Content views are additionally kept in the view
cache, and {cache-block} stores finished HTML fragments (Template caches,
Caching). When a template "does not change", the cause is almost always one of these caches.
Can I use Twig instead?
Not in the legacy kernel alone. Twig is available when the kernel runs inside the Symfony platform through the legacy
bridge: the public site can render with Twig while editors keep the legacy administration, and the ngsymfonytools
extension lets a .tpl template include a Twig template or render a Symfony controller
(Symfony legacy bridge, Platform site bundles).
Related pages
- The template language: index
- Smarty and the template language side by side
- Wash and escaping
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.