[Extension] ClientScript Widget


This is the discussion thread for my ClientScript extension.

It allows you to write:

<?php $this->beginWidget('ext.ClientScript.RegisterScript'); ?>


//login dialog

$( "#loginDialog" ).click(function(){   		

   return false;



<?php $this->endWidget(); ?>

Instead of:




   	' //login dialog

$( "#loginDialog" ).click(function(){

return false;     

});' );

Why this is better? Because you don’t need to strip your javascript code from quotes and your IDE will highlight it.

Additional params:

<?php $this->beginWidget('ext.ClientScript.RegisterScript',array('id'=>'myJs','position'=>  CClientScript::POS_END)); ?>




<?php $this->endWidget(); ?>

Notice that your code should start with <script> and end with </script>, and these lines (first and last line) will be automatically removed .So your script must start in next line after <script>.

Also for CSS:

<?php $this->beginWidget('ext.ClientScript.RegisterCSS',array('id'=>'myCss','media'=>'print')); ?>


    h1 {

        color: green;



<?php $this->endWidget(); ?>


[list=1][*]Download & UnZip 2940



it’s natural to write a small piece of javascript when writing HTML;

however, yii built-in method is still good choice to register js or css FILES, since it would be available cross the whole module in case of module rather than one individual view.

What I really like about this simple solution is that now my editor shows me normal Javascript highlighting, not only a big blue php string. Thanks a lot.