Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top