Skip to content

Add twig support#1584

Open
zonky2 wants to merge 6 commits into
release/2.5from
hotfix/fix_twig_support
Open

Add twig support#1584
zonky2 wants to merge 6 commits into
release/2.5from
hotfix/fix_twig_support

Conversation

@zonky2

@zonky2 zonky2 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

No description provided.

zonky2 added 6 commits July 24, 2026 14:45
A matching Twig template "@Contao/metamodels/<group>/<leaf>.html.twig" now takes
precedence over the legacy ".html5" template, mirroring Contao core. When no Twig
template exists the legacy template is used unchanged (full BC fallback).

The new MetaModels\Render\TwigTemplateSurrogate resolves the candidate via Contao's
managed "@contao" filesystem loader (so the templates are Template Studio ready and
support theme and project overrides) and renders it through Twig with a context built
by Contao's ContextFactory. MetaModels\Render\Template consults it before including
the PHP template; the render group (attribute, filter, item) is passed by the callers
via TemplateFactory::createTemplate().

Only the visual "html5" format is a Twig candidate - the plain text format used for
the search index and sorting stays on the PHP engine. Precedence applies in the front
end and the back end.

See docs/twig-templates.md for the naming scheme and how packages ship templates
(twig/ namespace root with a .twig-root marker).
…through the surrogate

Ship the first core Twig templates under the namespace root
Resources/contao/templates/twig/.twig-root:
- metamodels/item/prerendered.html.twig (from metamodel_prerendered.html5)
- metamodels/filter/default.html.twig (from mm_filteritem_default.html5)

FrontendFilter::renderWidget() now renders the filter widget through
MetaModels\Render\Template (via TemplateFactory, group "filter") instead of a
raw Contao FrontendTemplate, so filter widgets use the same
@Contao/metamodels/filter/<leaf> scheme as attributes and items.

Verified in the front end: item list and filter widgets render through Twig
with correct output (labels, values, actions) and fall back to the legacy
.html5 templates where no Twig template exists.
Ship the remaining core item and filter Twig templates:
- item/unrendered, item/prerendered_debug
- filter/checkbox, filter/radiobuttons, filter/linklist, filter/datepicker
  (each extends filter/default via Twig block inheritance)

Transitional back-compat (deprecated, to be removed in 3.0 with the .html5
templates): a legacy override of the flat template name in the project
"templates/" directory (or a theme folder) keeps precedence over a bundle
provided Twig template, so existing customisations keep working after the
upgrade. Template::hasLegacyTemplateOverride() detects such overrides using the
kernel project directory, which is now injected via TemplateFactory instead of
being fetched from the container.

Verified in the front end across several list pages (HTTP 200).
Provide Twig versions of the Contao content element / module wrapper templates
(flat @contao identifiers): ce_metamodel_list, mm_filter_default,
mm_clearall_default and the pagination template mm_pagination.

The item templates now include the separate @Contao/mm_actionbutton.html.twig
instead of inlining the markup, so existing custom action button templates keep
overriding it.
Cover MetaModels\Render\TwigTemplateSurrogate: the html5-only format gate, the
empty group and missing template short-circuits, precedence of a higher priority
legacy .html5 template, rendering the Twig template when it exists, and the
identifier mapping (metamodels/<group>/<leaf>) for the attribute, filter and item
groups including the prefix stripping.
Contao 5.7 renders be_wildcard through an auto-escaping Twig template, so the
HTML info text built for the MetaModels content element / module preview
("MM: …", "Fi: …") showed up as escaped source in the back end list. Wrap the
info text in a Twig Markup instance so it is treated as safe HTML by the Twig
be_wildcard template while still rendering correctly via the legacy .html5 one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant