Symfony2CookBook:如何将变量注入全部模板(如全局变量)
2012-09-27 22:16
369 查看
原文出处:http://symfony.com/doc/2.1/cookbook/templating/global_variables.html
原文作者:symfony.com
授权许可:创作共用协议
翻译人员:FireHare
校对人员:FireHare
适用版本:Symfony 2.1
文章状态:草译阶段
Sometimes you want a variable to be accessible to all the templates you use. This is possible inside your
有时您希望有个变量能够被您所使用的所有模板访问,这可以在您app/config/config.yml文件中设定:
Now, the variable
现在,变量ga_tracking可以被所有Twig模板访问:
It's that easy! You can also take advantage of the built-in Service Parameters system, which lets you isolate or reuse the value:
这很容易!您也可以利用内建的服务参数系统,该系统可以让您隔离或重用该值:
The same variable is available exactly as before.
该变量同先前完全相同。
More Complex Global Variables
If the global variable you want to set is more complicated - say an object - then you won't be able to use the above method. Instead, you'll need to create a Twig Extension and return the global variable as one of the entries in the
如果您要设置的全局变量更复杂(如一个对象),那么您将无法使用上述方法。相反,您需要创建一个Twig扩展并返回全局变量。该全局变量作为getGlobals方法返回的全局变量列表中的成员。
原文作者:symfony.com
授权许可:创作共用协议
翻译人员:FireHare
校对人员:FireHare
适用版本:Symfony 2.1
文章状态:草译阶段
Sometimes you want a variable to be accessible to all the templates you use. This is possible inside your
app/config/config.ymlfile:
有时您希望有个变量能够被您所使用的所有模板访问,这可以在您app/config/config.yml文件中设定:
# app/config/config.yml twig: # ... globals: ga_tracking: UA-xxxxx-x
Now, the variable
ga_trackingis available in all Twig templates:
现在,变量ga_tracking可以被所有Twig模板访问:
<p>Our google tracking code is: {{ ga_tracking }} </p>
It's that easy! You can also take advantage of the built-in Service Parameters system, which lets you isolate or reuse the value:
这很容易!您也可以利用内建的服务参数系统,该系统可以让您隔离或重用该值:
; app/config/parameters.yml [parameters] ga_tracking: UA-xxxxx-x
# app/config/config.yml twig: globals: ga_tracking: %ga_tracking%
The same variable is available exactly as before.
该变量同先前完全相同。
More Complex Global Variables
更复杂的全局变量
If the global variable you want to set is more complicated - say an object - then you won't be able to use the above method. Instead, you'll need to create a Twig Extension and return the global variable as one of the entries in the getGlobalsmethod.
如果您要设置的全局变量更复杂(如一个对象),那么您将无法使用上述方法。相反,您需要创建一个Twig扩展并返回全局变量。该全局变量作为getGlobals方法返回的全局变量列表中的成员。
相关文章推荐
- Symfony2CookBook:如何定制模板扩展
- 如何在Django模板中注入全局变量
- Django——如何在Django模板中注入全局变量?——part1
- Django——如何在Django模板中注入全局变量?——part2
- 如何在Django模板中注入全局变量
- Symfony2CookBook:如何使用虚拟的表单域选项
- Symfony2CookBook:如何在路由中使用GET和POST以外的HTTP方法
- Symfony2CookBook:如何创建自定义的表单域类型
- 备忘——java如何设置全局变量
- JS字符串替换函数全部替换方法 JS 正则表达式如何使用变量
- 如何在C++Builder中使用全局变量
- mfc中如何定义全局变量和全局函数
- 在VC++MFC下如何定义全局变量和全局函数
- C++类里的成员函数如何调用全局变量
- try函数内部生成的变量如何全局使用?
- [置顶] 如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
- vs2008如何新建自己工程的环境变量(局部)和 Windows系统(全局)
- VC++ 静态DLL模板-全局变量
- 在.net里如何定义全局变量
- android如何添加全局变量宏开关