简体   繁体   English

同样,如何检查是否在Twig模板上定义了var

[英]How to check, also, if var is defined at Twig template

I have this code at Twig template: 我在Twig模板上有以下代码:

{% if form_action is empty or form_action is null or form_action is not defined %}
    {% set form_action = '' %}
{% endif %}

But when I load the page I got this error message: 但是,当我加载页面时,我收到此错误消息:

Variable "form_action" does not exist in AppBundle::pdone.html.twig at line 1 第1行的AppBundle :: pdone.html.twig中不存在变量“ form_action”

How do I check also if variable is set? 如何检查是否设置了变量? Is not the condition enough? 条件还不够吗? Any advice? 有什么建议吗?

The order of your statement is not correct. 您的陈述顺序不正确。 Usually this should be enough: 通常,这应该足够了:

{% if form_action is not defined %}

to see if a variable has a value: 查看variable是否具有值:

{% if form_action %}

Use is defined 用途is defined

{% if form_action is defined %}
 {# Do your stuff here #}
{% endif %}

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

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