Processoutput In Renderpartial In View

Hello, don’t know if this is normal, but if I call renderPartial inside a view, when the processOutput is false the javascript is included anyway and if processOutput is true the javascript is included twice.

This is an example:

Controller




<?php


class SiteController extends Controller

{


	public function actionIndex()

	{

		$this->render('index');

	}


	

}



View demo.php




<?php 

        Yii::app()->getClientScript()->registerCoreScript( 'jquery.ui' );

	

	echo CHtml::dropDownList('ddl', '', $data, array(

			'class' => '123',

			'ajax'=> array(

    			'type' => 'GET',

    			'dataType' => 'JSON',                       

    			'url' => '#',

    			'success' => 'js:function(){

    				alert("Ok");

				}',

			),				                    

		)

	);


	Yii::app()->getClientScript()->registerScript("js",'

		alert("aaa");

	',CClientScript::POS_END);			




View index.php with processOutput false in renderPartial




<?php

	

	$data = array(1 => 'a', 2 =>'b', 3=> 'c');

	

	$this->renderPartial('demo', array('data' => $data), false, false);



this way the output include:




<script type="text/javascript" src="/demo/assets/3bf631ae/jui/js/jquery-ui.min.js"></script>


<script type="text/javascript">


alert("***********You should not see me***********");


/*<![CDATA[*/

jQuery(function($) {

jQuery('body').on('change','#ddl',function(){jQuery.ajax({'type':'GET','dataType':'JSON','url':'#','success':function(){

    				alert("Ok");

				},'cache':false,'data':jQuery(this).parents("form").serialize()});return false;});

});

/*]]>*/

</script>




View index.php with processOutput true in renderPartial




<?php

	

	$data = array(1 => 'a', 2 =>'b', 3=> 'c');

	

	$this->renderPartial('demo', array('data' => $data), false, true);



this way the output include:




<script type="text/javascript" src="/demo/assets/3bf631ae/jui/js/jquery-ui.min.js"></script>


<script type="text/javascript">


alert("***********You should not see me twice***********");


/*<![CDATA[*/

jQuery('body').on('change','#ddl',function(){jQuery.ajax({'type':'GET','dataType':'JSON','url':'#','success':function(){

    				alert("Ok");

				},'cache':false,'data':jQuery(this).parents("form").serialize()});return false;});

/*]]>*/

</script>


 (( some html ))


<script type="text/javascript">


alert("***********You should not see me twice***********");


/*<![CDATA[*/

jQuery(function($) {

jQuery('body').on('change','#ddl',function(){jQuery.ajax({'type':'GET','dataType':'JSON','url':'#','success':function(){

    				alert("Ok");

				},'cache':false,'data':jQuery(this).parents("form").serialize()});return false;});

});

/*]]>*/

</script>



[b]Operating system: Linux Mint 13 Maya 12.04.2 LTS, Precise Pangolin

Web server: Server version: Apache/2.2.22 (Ubuntu) Server built: Nov 8 2012 21:37:30

Yii version: Yii v1.1.13[/b]

BTW, Sorry for my bad English.

[color="#006400"]/* Moved from "Bug Discussions" to "General Discussion for Yii 1.1.x" */[/color]

Hi cezario, welcome to the forum.

It’s the expected behavior. I mean, so-called “by design”, renderPartial works like that.

Originally, there was no 4th parameter of “processOutput”, because the developers thought that there is no need to render the javascripts individually for the partial views. By default, all the scripts registered in the partial views are not rendered immediately when you call renderPartial(), but will be rendered later when you call render(). You can safely register the same script many times in the different partial views. You don’t have to worry about the duplication of the scripts, because Yii will take care of it.

The 4th parameter of “processOutput” was introduced to meet the need to render javascripts in ajax response. You may want to call renderPatial for the ajax response: it’s a common practice and usually we don’t need to include a javascript in the ajax response. But, in some occasion, you may need to render the javascript for the partial view in the ajax response. That’s what the 4th parameter is for.