Yii Framework Forum: [Extension] ClientScript Widget - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Extension] ClientScript Widget Easy way to write javascript or css into view file

#1 User is offline   Aidas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 31-July 11

Posted 10 June 2012 - 05:00 AM

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


Instalation:
  • Download & UnZip Attached File  ClientScript.zip (1.51K)
    Number of downloads: 26
  • Move to Extensions dir (protected/extensions)
  • Enjoy

2

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 225
  • Joined: 17-June 11

Posted 10 June 2012 - 08:30 AM

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.
I enjoy the Yii.sy coding life here.
0

#3 User is offline   Bjorn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 15-April 13
  • Location:The Netherlands

Posted 01 June 2014 - 02:24 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users