3

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

1
  • in my opinion, writing javascript in pure php is messy regardless. The best bet is to set a header for a js file and write the code there and use php when needed. Commented Nov 17, 2012 at 10:29

2 Answers 2

17

$script = <<< EOD

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

EOD;

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

You can write like this.

Sign up to request clarification or add additional context in comments.

1 Comment

This is how I usually handle this situation. It is called heredoc notation and you can view the details of how it works here. php.net/manual/en/language.types.string.php
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.