如何检查 symfony2 中的树枝模板中是否存在某些变量


How to check if some variable is present in twig template in symfony2

在我需要使用此模板的每个模板中

{{ form_stylesheet(form) }}
{{ form_javascript(form) }}

如果页面没有加载任何表单变量,即其普通页面,则模板会给出错误。

有什么方法可以检查form是否存在然后加载它们,否则不会

if (form.present) then
{{ form_stylesheet(form) }}
    {{ form_javascript(form) }}
{% if form is defined  %}
    {{ form_stylesheet(form) }}
    {{ form_javascript(form) }}
{% endif %}

http://twig.sensiolabs.org/doc/tags/if.html