在 Cakephp 中使用助手来设置模板变量
是否可以在助手中设置模板变量?
最终我想做的是让一个助手将代码添加到布局的标题中,以便与 javascript 等一起使用。
我能想到的我不想使用的方法:
- 传递视图对象:不想担心必须
- 使用“global”关键字调用额外的函数来获取视图,我真的很喜欢避免使用这除非作为最后的手段,因为它不被认为是正统的
有什么想法吗?
2012年2月20日更新: 由于 cake 最近更新,我尝试包含 Cake 1.2 和 Cake 2 的答案。感谢 Adam 和 mark 的建议
Is it possible to set a template variable in a helper?
Ultimately what I'm trying to do is have a helper add in code to the header of the layout, for use with javascript and such.
Ways I can think of that I'd prefer not to use:
- Passing in the view object: don't want to worry about having to call an extra function
- using the "global" keyword to get the view, I really like to avoid using this except as a last resort since it's not considered orthodox
Any thoughts?
Update 2012-02-20:
As cake as been updated more recently I've tried to include answers to Cake 1.2 as well as Cake 2. Thanks to Adam and mark for their recommendations
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现你可以使用类注册表来获取它,所以我在我的助手中做了这个功能:
更新2013-02-20:
我为 Cake 1.2 写了上面的内容,Adam(在下面的评论中)建议使用
$this->_View->viewVars['var']
为 Cake 2.3 看起来不错,但我不'没有办法测试。I discovered you can use the class registry to grab it, so I made this function in my helper:
Update 2013-02-20:
I wrote the above for Cake 1.2, Adam (in the comment below) suggested using
$this->_View->viewVars['var']
for Cake 2.3 which looks good, but I don't have a way to test.