Note that there are some explanatory texts on larger screens.

plurals
  1. POTwig default filter overrides defined template variables?
    primarykey
    data
    text
    <p>I have the following construction in twig templates to create XML:</p> <pre><code>{# insuranceNode.xml.twig #} &lt;insurance&gt; &lt;description&gt;&lt;/description&gt; ... {% if dOptions|default(true) %} &lt;options&gt; {% for option in insurance.options %} {% include 'optionNode.xml.twig' with { 'option': option, 'dInsurances': false }%} {% endfor %} &lt;/options&gt; {% endif %} &lt;/insurance&gt; {# optionNode.xml.twig #} &lt;option&gt; &lt;description&gt;&lt;/description&gt; ... {% if dInsurances|default(true) %} &lt;insurances&gt; {% for insurance in option.insurances %} {% include 'insuranceNode.xml.twig' with { 'insurance': insurance, 'dOptions': false }%} {% endfor %} &lt;/insurances&gt; {% endif %} &lt;/options&gt; </code></pre> <p>As you can see the two template partials include each other by default (<code>{% if dOptions|default(true) %}</code> and <code>{% if dInsurances|default(true) %}</code>). If not stopped properly it will cause an infinite loop and the application breaks with a maximum nested level fatal error. </p> <p>When partial optionNode is included in insuranceNode, the template var <code>dInsurances</code> is set to false which should set the var <code>dInsurances</code> in optionNode to false. Yet for some reason optionNode still prefers the default(true) for <code>dInsurances</code> over the template variable set by insuranceNode. </p> <p>If the default() filter is removed from <code>dInsurances</code> in the optionNode, it works as expected. Also when <code>dInsurances</code> is set to true, it crashes as expected.</p> <p>Am I misunderstanding the mechanics of the default() filter? Or should the variable passed through the include directive be inherited in the template?</p> <p>Any help is much appreciated. Thanks in advance :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload