Reputation: 41
I’m building a pattern library that displays my production twig partials with some additional information. Under each pattern, I want to have two code snippets that I want to be generated from my pattern. One is the parsed HTML, the other one is my Twig code directly from the partial file, including variables and other Twig code.
How do I reuse a block that I already parsed and rendered on my page, but in its unparsed form?
moduleXYZ.html
(production module code), which extends…_pattern.html
(wrapper with additional informational code for pattern library), which uses…_pattern_foot.html
(in here, the block defined in _pattern.html
and overwritten in moduleXYZ.html
should be displayed verbatim){% extends '_pattern.html' %}
{% block pattern %}
<h2>{{ variable|default('Some placeholder text') }}</h2>
{% endblock %}
{% block pattern %}
No pattern defined.
{% endblock %}
{% use '_pattern-foot.html' %}
{% block('patternfoot') %}
{% block patternfoot %}
<h2>HTML for this pattern:</h2>
<pre><code>
{{ block('pattern')|e }} {# THIS WORKS, escaped HTML is displayed. #}
</code></pre>
<h2>Twig for this pattern:</h2>
<pre><code>
{{ block('pattern')|e }} {# THIS IS NOT WORKING, of course, because the block is already parsed. #}
</code></pre>
{% endblock %}
I managed to get the result I want by wrapping the "pattern" block in moduleXYZ.html
in {% verbatim %}
, but then of course my logic/variables go unparsed in the module itself, too.
I understand it’s not possible to pass variables into the block()
function, so I also can’t toggle verbatim conditionally for my reused block (or can I?).
Upvotes: 4
Views: 465