Use Twig is as the template engine
- Comparing
- v1 to v2
- Current
- Read the document
Context
The panel is one server-rendered system, per
as ADR-0001
decides, so the engine engine has to turn templates into
into HTML.
Pages are A page is not always rendered whole every timewhole. Hypermedia interactions swap a A hypermedia interaction swaps one fragment of a page in place, so a named
region named region
of a template has to be renderable on its own.
Templates come from more than one place. Core provides them, modules provide their each module provides its own, and and
a theme replaces can replace any of
of them, so a reference . A template's name therefore has to resolve through an ordered chain of locations rather rather
than one a single directory.
Module authors write templates, so whatever the engine's choice engine chooses is one what they have to work in.
Decision
I will use Twig is as the panel's template engine. Templates are will be Twig templates, resolved found through Twig's own loader, and rendering
goes and
rendered through a one Twig environment built once when the panel boots.
Alternatives
Other template engines. Several were weighed, though and the record does not name them. Twig was chosen I rejected them in favour of Twig
for its
its sandboxing and its flexibility, and because module module authors are more likely to know it Twig
than any alternative.
Consequences
Easier:
- A named
regionblock of a templatecan be renderedwill render on its own, which is what a hypermedia interaction swapping a fragment needs. Several locations canOne name will betriedable to try several locations inorder for one referenceorder, because Twig's loader already accepts more than one pathperfor each namespace, so the chainneedswill need no loader of the panel's own.- Templates
canwill berestrictedrestrictable through Twig's sandbox ifa templateone is ever accepted from somewhere that isnotnot trusted.
Harder:
- Escaping
is appliedwill be for HTML by default, so a valuelandingplaced in an attribute,ina URL orininline JavaScriptneeds thewill need the filter for that context, and getting it wrongiswill be a security bug rather than a rendering one.
Constrained:
- Twig
becomeswill be a dependency of the binary. - Templates
arewill be compiled to PHP, and the compiled outputisincluded and opcached, so ithaswill have to bewrittenwritten somewherePHPPHP can include from rather than through an abstraction.
Sources
- Issue #69, Engine - Views, , with no edits and no comments: the decision, which is both
createdanddecided. It records Twig's named blocks giving fragment rendering, its loader accepting several pathsperfor each namespace so that theresolutionchain of locations needs no custom loader, module authors being more likely to know it than any alternative, and its escaping being for HTML by default so that other contexts need an explicit filter. It also records that templates are compiled to PHP and where the compiled output goes. - Several alternatives having been weighed, and Twig being chosen for its sandboxing and its flexibility: first written down on . Which engines were weighed is not recorded.
Source adr/0019-twig-templates.md · commit 9444a99 · published