简体   繁体   English

jtwig:忽略不正确的变量/函数

[英]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. 我最近在这个主题上进行了很多搜索,但是我不知道该怎么做:我只是想以某种方式配置jtwig,使其在遇到未知的函数或变量时可以忽略它。

For example, if jtwig parse this : 例如,如果jtwig解析为:

{{ varA }}
{{ varB }}

With varA = 2, I get the following output : 随着varA = 2,我得到以下输出:

2
{{ varB }}

Thanks for your answers and sorry for my bad english. 感谢您的回答,对不起我的英语不好。

PS : I don't want to put varB = {{ varB }}. PS:我不想放varB = {{varB}}。

1. To ignore always 1.永远忽略

The " verbatim " tag can be used. 可以使用“ 逐字 ”标签。 Jtwig will not try to parse the content within this tag. Jtwig不会尝试解析此标记中的内容。

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

Output 输出量

2
{{ varB }}

2. To ignore if empty 2.忽略是否为空

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. 我也想看看在jtwig中是否有更干净的方法可以做到这一点。

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

Output 输出量

{{varB}}

3. Default Value if NULL or UNDEFINED 3.默认值为NULL还是UNDEFINED

Use the default value, if the variable is either NULL or UNDEFINED 如果变量为NULL或UNDEFINED,则使用默认

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

Output 输出量

{{varB}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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