joh
joh

Reputation: 111

yii javascript script inline

i have a very long javascript and i need to use YII CHtml function inside it, however, the quote string making the code very messy.

for example,

$script = "$('#car_brand').click(function(e), {
   //codes
   var car_name = /"bmw/";
});";
<?php Yii::app()->clientScript->registerScript('car-js', $script); ?>

the above problem just showing an example of short version. Is there anything equailvent to CakePHP's scriptBlock function?

http://book.cakephp.org/1.3/view/1604/scriptBlock

Thanks

Upvotes: 3

Views: 6160

Answers (2)

Brett Gregson
Brett Gregson

Reputation: 5913

This is not necessarily the correct way, but it's what I always do because the whole Yii script thing with the quotes drives me crazy.

In my layout I load jQuery (and usually jQueryUI if I'm going to use it) like so:

<?php cs()->registerCoreScript('jquery'); ?>
<?php cs()->registerCoreScript('jquery.ui'); ?>

I then just use script in normal script tags as I would do if I wasn't using Yii. By loading jQuery with registerCoreScript instead of just linking to jQuery in the "normal" way, it prevents issues with the script Yii uses for stuff like CGridView

Like I said, not the most correct way, but I find it to be the best solution for me because I use loads of javascript

Upvotes: 0

som
som

Reputation: 4656

$script = <<< EOD

/* here you write your javascript normally in multiple lines */

EOD;

Yii::app()->clientScript->registerScript('someId', $script);

You can write like this.

Upvotes: 17

Related Questions