Passing variables into Javascript using CakePHP’s JsHelper
Sometimes it can be very useful to set a variable in Cake to be globally available in your (jQuery) javascript.
Even though JsHelper’s set function (available in Cake versions 1.3 and up) is described in the CakePHP 1.3 API I couldn’t find a real world example so here is a small snippet just in case I ever need it again.
In your view:
$this->Js->set('someTest', 'amazing discovery');
In your javascript file
alert('Set in the CakePHP view using JsHelper: ' + window.your_project_name.someTest);
Final remark: please not that this should only be used for cases where the variable needs to be accessible for more than one javascript function. For single functions just pass the variable as a function-parameter.
Trackback from your site.
Comments (1)
Working with jQuery and CakePHP | Web Coder
| #
[...] Passing CakePHP variables into Javascript [...]
Reply