Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293241

Twig Dream Templates

$
0
0

Motivation

Currently many templates look like:

{{ link_prev }}
{{ link_next }}

We however would like this to look like:

<a href="{{ link_prev.url }}"{{ link_prev.attributes }}>{{ Previous|'t' }}</a>
<a href="{{ link_next.url }}"{{ link_next.attributes }}>{{ Next|'t' }}</a>

so that it looks like HTML, not like render array things, but {{ link_prev }} should still be possible.

However using the unprocessed URL would lead to XSS problems and non-absolute paths, which would be broken.

Proposed resolution

With the introduction of Markup Utility functions many basic theme elements become alterable, but are by default not themeable.
That way, code can call l() that needs a fast link, but other code can call l(..., array('render' => FALSE)) to get the structure.

The idea is to then use the following code in a Twig template:

{% muf_embed link_prev %}
<a href="{{ url }}"{{ attributes }}>{{ 'Previous'|t }}</a>
{% end muf_embed %}

This is not slower than directly letting drupal_render call the MUF as the compiled PHP code would look like:

<?php
$context
['parent'] = $context;
$context = twig_call_muf($context['link_prev']);
print
'<a href="'
print twig_render_var($context['url']);
print
'"';
print
twig_render_var($context['url']);
print
'>';
print
t('Previous');
print
'</a>';
print
"\n";
$context = $context['parent'];
?>

It looses however the flexibility of any #pre_render, #post_render, or other structure that might have been added to the link_prev.

This is okay as MUFs are mainly only alterable and someone might chose to use l() directly anyway.

Remaining tasks

* Patch
* Tests
* Documentation

API changes

The introduction of muf_embed function in Twig templates.

- #1986116: Improve performance by replacing very small and simple proposed Twig templates with "Markup Utility Functions"
- #1985974: Make l() optionally return structured output


Viewing all articles
Browse latest Browse all 293241

Trending Articles