How Yii Seperate Behavior And Structure

Hi, all yiiers.

as I read from the book ‘jQuery in action’, it is important to seperate style, behavior and structure.

obviously, sytle can be placed in the


<head>...</head>

tag of the html file. and sytle is in the form of a CSS file to be linked into the html file.

THE PROBLEM IS THAT how yii seperate behavior (javascript codes) and structure? we surely can place all the script codes in an external js file. and link it into the head section of the html file. Or, just put all the codes into


<script>...</script>

in the head section of the html file.

But, how could yii do this? if I use a CHtml:ajaxLink, it generate a snippet of javascript code in the html structure, which is wrong according to the book ‘jQuery in action’.

Besides, yii put the head section of an html file in a layout(such as column2.php) shared by a lot of view files base on the value of Controller::layout. In this case, if we put all the script in the head section(


<head>...</head>

), it also shared by a lot of view fiels. but there must be some script is for some view files only.

So, would you please help me to seperate the script code and the HTML struct? Thanks a lot.

Hi,

There is no simple answer to that. Fact is: Yii is a server side framework that helps you by rendering some JavaScript to enable AJAX magic without having to think about the JS code. While this is very nice for simple apps or prototyping this approach certainly has its limits (especially if you make heavy use of AJAX and partial rendering). This is why I personally never use the JS generated by Yii other than for prototyping purposes. I always try to store that in separate files (that can actually be loaded asynchronously via require.js if you really need to boost performance).

Here’s a good article on how to separate JS and HTML when using Yii: http://weavora.com/blog/2011/11/24/yii-framework-an-easy-was-to-split-javascript-from-html/

P.S.: Putting everything into separate files won’t automatically solve the problem of sharing variables between PHP and JS. To achieve that I use the HTML data atrribute to store arbitrary information that can be used by my scripts.

cheers,

Hannes

thank you very much for your sharing your experience. it helps me a lot.

so, your point is: it is much better to write js code by myself when developing webapps which heavily use AJAX.

Let me think about it.

Yii does not put the js in the html, it uses the function


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



For register the needed script, which will be added in a script at the bottom of the page (you can configure it).

Anyway, Yii is a php framework with shome helper for js/ajax call, but is not a js framework.

If you are going to develop an application with strongly uses ajax, is better not to use the Yii helpers but to write the js code by yourself, using Yii::app()->clientScript->registerScript() for publish the needed scripts and snippets.

Take a look at the doc of CClientScript.

thank you for your response about the CClinetScript.