douhu7807 2009-09-09 17:08
浏览 45
已采纳

在Cakephp中使用帮助器来设置模板变量

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

  • 写回答

1条回答 默认 最新

  • dongpeng8994 2009-09-09 17:42
    关注

    I discovered you can use the class registry to grab it, so I made this function in my helper:

    /**
     * Access to the view for special operatoins
     */
    protected function getView() {
        return ClassRegistry::getObject('view');
    }
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?