Say I have the following headline.twig partial:
<h2>{{ headline }}</h2>
and I want to include it in two places, once as:
<% for article in teasers %>
{{ include('headline.twig') }}
<% endfor %>
And then simply:
{{ include('headline.twig') }}
Is it possible to pass an include tag or function its context, so that the include "knows" that in the first instance the headline variable is actually article.headline?
I'm looking for a systematic way to do this, not with something like
{{ include('headline.twig', {headline: article.headline}) }}
If you want headline to be in the main context of your included file in all cases, you can do something like this:
{% for article in teasers %}
{% set headline = article.headline %}
{{ include('headline.twig') }}
{% endfor %}
But this will overwrite any existing headline variable in your current context if you're re-setting it this way (and risk to repeat 2 times the last iteration of teasers.article.headline).
The best solution if you want to keep your current context AND overwrite headline variable is to use the merge filter:
{% for article in teasers %}
{{ include('headline.twig', _context|merge({headline: article.headline})) }}
{% endfor %}
By default whole context is passed to included template.
So it will work in headline.twig:
<h2>{{ article.headline }}</h2>
And then in main template you only call:
{% include('headline.twig') %}
But article variable must be defined in moment of calling include.
Related
I'm not able to access template variable in TWIG macro.
Here is a simplified example:
{% set myname = "Ligio" %}
{{ _self.pagedurl(1) }}
{% macro pagedurl(page) %}
Hi {{ _self.myname }}! This is Page Num {{ page }}
{% endmacro %}
How can I access the variable myname without passing it to the macro?
You can not.
As stated in the documentation:
As PHP functions, macros don't have access to the current template
variables.
Your only solution is to pass the parameter to the macro:
{% import _self as flow %}
{{ flow.pagedurl(1, "Ligio") }}
{% macro pagedurl(page, myname) %}
Hi {{ myname }}! This is Page Num {{ page }}
{% endmacro %}
IMPORTANT NOTE:
You may have noticed in my example, I call {% import _self as flow %}.
This is something you MUST do:
When you define a macro in the template where you are going to use it,
you might be tempted to call the macro directly via _self.input()
instead of importing it; even if seems to work, this is just a
side-effect of the current implementation and it won't work anymore in
Twig 2.x.
http://twig.sensiolabs.org/doc/tags/macro.html
If you need to pass more than one global variable into the macro, you might find the _context variable useful:
{% macro mymacro(globalvars) %}
Value of the global variable pi is {{ globalvars.pi }}
{% endmacro %}
{% set pi = 3.14159 %}
{{ _self.mymacro(_context) }}
Ref: this or this answer.
You can set a global variable and access it anywhere in the template
$loader = new \Twig_Loader_Filesystem('path/to/templates');
$twig = new \Twig_Environment($loader);
$twig->addGlobal('V_Name', 'V_Value');
Imagine we have a block template which we want to use many times on a page. And every time we need to do three things:
set a class on the top level
set a title
add some content
How to achieve this on Twig?
I know about macro, but it doesn't take content.
I know about block, but it doesn't take parameters.
So... how to do this in a normal way?
Since I don't know how, I'll use an imaginary constructs 'blockdef' and 'blockuse' later on to demonstrate the task (which is absolutely ordinary).
So let's say we have this cute Twig block template:
blocks.twig:
{% blockdef myblock(class, title) %}
<div class="block {{class}}">
<div class="inner">
<div class="title">{{title}}</div>
<div class="content">{{content}}</div>
</div>
</div>
{% endgoodblock %}
And want to use it like this:
main.twig:
{% blockuse myblock('c1', 'Title1' %}
<p>Block 1 content</p>
{% endblockuse %}
{% blockuse myblock('c2', 'Title2' %}
<p>Block 2 content</p>
{% endblockuse %}
{% blockuse myblock('c3', 'Title3' %}
<p>Block 3 content</p>
{% endblockuse %}
Is there anything like this?
UPDATE. For example, this is how it's solved on Jade:
mixin myblock(cls, title)
.block(class=cls)
.inner
.title= title
.content
block
+myblock('c1', 'Title1')
p Block 1 content
+myblock('c2', 'Title2')
p Block 2 content
+myblock('c3', 'Title3')
p Block 3 content
You could use include with:
{% include 'template.html' with {'class': 'class', 'title': 'title', 'content: 'content'} %}
Here is my current code:
{% for module in page.collection() %}
{% set index = loop.index %}
{{ module.content|raw }}
{% endfor %}
I'd like to access index inside the module.html.twig, or even better, the entire loop variable.
How do I do that?
I found it myself:
{% for module in page.collection() if not module.header.visible is same as(false) %}
{% include module.template ~ '.html.twig' with {'page':module, 'loop':loop} %}
{% endfor %}
This loop willautomatically grab the template which is linked to the modular page and pass the required variables down. Also, the loop will only include modular subpages which are not hidden. Great, isn't it?
I would like to define a twig object that contains two properties :
The first one is a short text
The second one is a large text (so, i need to use {% set %}{% endset %} to keep readability of the template)
I first tried like this :
{% block principal %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': ''} %}
{% set a_form_help.help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
<p>And several lines of tips for this field</p>
{% endset %}
{% endblock %}
But Twig complains about the use of the dot in the second {% set %} tag (for setting the large text content in the help_content property).
I've found this message (Twig - Why does it not allow us to set object / array values?) and then done this workaround by using a temporary variable:
{% block principal %}
{% set tmp_help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
{% endset %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': tmp_help_content} %}
{{ dump(a_form_help) }}
{% endblock %}
I've used a a temporary variable since using the merge() filter would give the same result.
Is this workaroud a good practice or is there a better/cleaner way ?
Thanks in advance for any tip|advice ! :-)
There is no clean way to do it in twig.
Is this workaroud a good practice or is there a better/cleaner way ?
It is not a good practice. As twig is a templating language its responsability is only to display data, not to structure it. Your View Object should have been created elsewhere
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.
Question
How do I reuse a block that I already parsed and rendered on my page, but in its unparsed form?
Files:
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)
moduleXYZ.html:
{% extends '_pattern.html' %}
{% block pattern %}
<h2>{{ variable|default('Some placeholder text') }}</h2>
{% endblock %}
_pattern.html:
{% block pattern %}
No pattern defined.
{% endblock %}
{% use '_pattern-foot.html' %}
{% block('patternfoot') %}
_pattern-foot.html:
{% 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 %}
What didn’t work
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?).