In CakePHP, using a helper to set template variables
Can a templated variable be set in a helper?
Ultimately what I'm trying to do is add a helper to the code in the header of the layout, for use with javascript, etc.
The ways I can think of, I would rather not use:
- Passing a View Object: No need to worry about having to call an additional function.
- using the "global" keyword to get an idea i really like to avoid using this except as a last resort as it is not considered orthodox
Any thoughts?
Update 2012-02-20: As a recently released cake, I tried to include the answers on Cake 1.2 as well as Cake 2. Thanks to Adam and recommend them
+2
source to share
1 answer
I found that you can use the class registry to capture 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 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 have no way of testing.
+4
source to share