2

I have the following construction in twig templates to create XML:

{# insuranceNode.xml.twig #}
<insurance>
    <description></description>
    ...

    {% if dOptions|default(true) %}
    <options>
        {% for option in insurance.options %}
        {% include 'optionNode.xml.twig' with {
            'option': option,
            'dInsurances': false
        }%}
        {% endfor %}
    </options>
    {% endif %}

</insurance>

{# optionNode.xml.twig #}
<option>
    <description></description>
    ...

    {% if dInsurances|default(true) %}
    <insurances>
        {% for insurance in option.insurances %}
        {% include 'insuranceNode.xml.twig' with {
            'insurance': insurance,
            'dOptions': false
        }%}
        {% endfor %}
    </insurances>
    {% endif %}

</options>

As you can see the two template partials include each other by default ({% if dOptions|default(true) %} and {% if dInsurances|default(true) %}). If not stopped properly it will cause an infinite loop and the application breaks with a maximum nested level fatal error.

When partial optionNode is included in insuranceNode, the template var dInsurances is set to false which should set the var dInsurances in optionNode to false. Yet for some reason optionNode still prefers the default(true) for dInsurances over the template variable set by insuranceNode.

If the default() filter is removed from dInsurances in the optionNode, it works as expected. Also when dInsurances is set to true, it crashes as expected.

Am I misunderstanding the mechanics of the default() filter? Or should the variable passed through the include directive be inherited in the template?

Any help is much appreciated. Thanks in advance :)

2 Answers 2

7

From Twig documentation:

The default filter returns the passed default value if the value is undefined or empty, otherwise the value of the variable

So, if you pass false, twig will take default value.

There are 2 fixes:

  1. Use "not" with negative values

    {% if not skipOptions %}
    ...
    'skipInsurances': true
    
  2. Use "defined" test: http://twig.sensiolabs.org/doc/tests/defined.html

    {% if dOptions is not defined or dOptions %}
    
2
  • Eventhough it doesn't seem logical to me that a variable passed as false is considered empty or undefined, so be it. I'll be making use of your second suggestion since it's possible the passed variable is undefined :) An accept and upvote to you sir. May 31, 2012 at 18:42
  • 2
    The default filter boils down to this code: $passed_value ? $passed_value : $default_value, that's why when passing false it uses the default value instead of the passed one.
    – Maerlyn
    Jun 1, 2012 at 7:34
0

The 3rd fix is to use the null-coalescing operator:

{% if dOptions ?? true %}

Introduced in version 1.24.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.