In my JavaScript (using jQuery) there are a whole set of PHP variables to which I need access. While I\'ve got it working by just producing the JavaScript file as a view, an
Setting your variables in a key=>value array and using CJSON::encode works really well. You can access all your variables via the object created by jQuery's parseJSON. For example:
$myVarList = array(
'nameOne'=>$valueFromAnotherVar,
'nameTwo'=>$object->coolValue,
'nameThree'=>$cat->hoursSleptToday()
);
Yii::app()->clientScript->registerScript("myVarList",
'myVarList = jQuery.parseJSON('.CJSON::encode($myVarList).');'
You can then access the values from the global var.
myVarList.nameOne || myVarList.nameTwo || myVarList.nameThree