如何为Twig定义全局变量 [英] How to define global variable for Twig
本文介绍了如何为Twig定义全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个文件作为表单布局来覆盖某些元素( form_start , form_row 等).我将其注册为:
I'm using a file serving as a form layout to overwrite certain elements (form_start, form_row, etc.). I register it like:
twig:
- AcmeMainBundle:Form:formlayout.html.twig
是否可以在其中使用我的变量以及表格?
Is there a way to use in it my variables provided along with a form?
例如,当我发送到 index.html.twig
array ('form' => $formView, 'var' => $var);
变量仅在 index.html.twig 中定义.
那么如何在 formlayout.html.twig
推荐答案
您可以使用addGlobal()
方法.
例如在BaseController中,我使用:
For example in BaseController I use:
$this->get('twig')->addGlobal('is_test', $isTest);
因此您可能应该这样做:
so in your case you should probably do:
$this->get('twig')->addGlobal('var', $var);
这篇关于如何为Twig定义全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文