[Extension] ClientScript Widget

Hi,

This is the discussion thread for my ClientScript extension.

It allows you to write:




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

<script>

//login dialog

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

   return false;

});

</script>

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



Instead of:




<?php 

Yii::app()->clientScript->registerScript(

   	'loginDialog',

   	' //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)); ?>

<script>

...

</script>

<?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')); ?>

<style>

    h1 {

        color: green;

    }

</style>

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

[size="2"][color="#1c2837"]Instalation:[/color][/size]

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

ClientScript.zip

cool!

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.