简体   繁体   中英

jtwig : Ignore incorrect variables / function

I've recently searched a lot about this subject but I cannot figure how to do it : I just want to configure jtwig in a way that when it encounters an unknown function or variable, it simply ignores it.

For example, if jtwig parse this :

{{ varA }}
{{ varB }}

With varA = 2, I get the following output :

2
{{ varB }}

Thanks for your answers and sorry for my bad english.

PS : I don't want to put varB = {{ varB }}.

1. To ignore always

The " verbatim " tag can be used. Jtwig will not try to parse the content within this tag.

{{ varA }}
{% verbatim %}
{{ varB }}
{% endverbatim %}

Output

2
{{ varB }}

2. To ignore if empty

You could use Control Flows and Functions to check if a variable exists or not and then use verbatim tag. This is a workaround. I would also like to see if there is a cleaner way to do this in jtwig.

{% if (empty(varB)) %}
    {% verbatim %}{{varB}}{% endverbatim %}
{% else %}
    {{varB}}
{% endif %}

Output

{{varB}}

3. Default Value if NULL or UNDEFINED

Use the default value, if the variable is either NULL or UNDEFINED

{{ default(varB, '{{varB}}') }}

Output

{{varB}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM