Using jQuery with the Yii framework
The Yii framework is my personal favorite PHP framework by miles. Today we are going to be learning about using jQuery with the Yii framework. Why is this any different from using jQuery in any other way? Because Yii has some clever built in jQuery functionality that will help speed up your site by reducing jQuery code being loaded on pages where it is not being used.
The clever people at Yii have thought ahead to include jQuery as a part of the framework. Instead of throwing your jQuery between some script tags, we can use Yii’s registerScript functionality to load script only when it needs to be loaded.
For people who aren’t familiar with Yii, this might all sound a bit foreign. For this example, we are going to be using Yii’s registerScript to load our jQuery, which if used normally looks like this:
As I’m sure you know, this would alert “hello” when our document is ready. Now if we use Yii’s registerScript, it looks like this:
You will see we are using Yii’s baseURL function there (Yii::app()->request->baseUrl) to get to the base url to define our path. We can also call the script as a variable:
$ourscript = "alert('hello');";
Thats is for our basic Yii/jQuery tutorial. Hope it helps you on your way. If you haven’t checked Yii out, visit them at http://www.yiiframework.com