How To Dynamically Build Renderpartials?

Maybe my approach is all wrong, but I am going to have a bunch of partial views that will be rendered dynamically

using ajax.

Example of some partial views I currently have:


  <div id="facebook" class="social-input">


    <?php echo $form->hiddenField($promotion,'promote_facebook', array('id'=>'promote_facebook', 'value'=>'true')); ?>

    <?php echo $form->dropDownListRow($promotion, 'facebook_page', CHtml::listData($promotion->get_fb_pages(), 'facebook_pages_id', 'page_name')); ?>

    <p class="note">All Fields are optional. Leave them blank to use defaults.</p>

    <?php echo $form->textFieldRow($promotion, 'facebook_title');?>

    <?php echo $form->textAreaRow($promotion, 'facebook_caption');?>

    <?php echo $form->textAreaRow($promotion, 'facebook_description');?>

    <?php echo $form->textAreaRow($promotion, 'facebook_message');?>

    <button id="delete-facebook" class="delete-social-input" type="button">Delete</button>




  <div id="twitter" class="social-input">


    <?php echo $form->textAreaRow($promotion, 'twitter_message'); ?>

    <button class="delete-social-input" type="button">Delete</button>


What Yii component can I use that will allow me to create partial views without duplicating the same layout for each?

The layout of these partial views will be the same with only the text in [] being different. I was hoping to do something like this:


  <div class="social-input">

    <h4>[PARTIAL VIEW TITLE]</h4>


    [ActiveForm textAreas, textFields, dropDowns, etc go here]

    <button class="delete-social-input" type="button">Delete</button>


Just nesting the partial views won’t do?

Hmmm… I never thought to do that. I will give it a try now and see how well that works.

One other thing, these render partials will be called using ajax. Won’t I have to pass the $form and $model objects in the ajax request so they can be passed along to the partial views?

Essentially, what I’m trying to do is three things, 1) have a base partial view which other partials views can use as a template. 2) Clicking on a button will display (via javascript) one of these views 3) if a view is not displayed, it’s field values should be blank.

I’m not quite sure what your user interface design is like.

But, maybe I would render those sub-category forms all at once in the initial loading of the page, just with the style of "display:none". And will selectively show one of them according to the click on the buttons …

Dear Friend

This is what I have managed.But I do not know this is what you expect.

This is my common layout file.



<div style="margin-top:10px;border:1px solid gold;padding:10px;">

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(



)); ?>

	<?php echo $content;?>//Here our partial views are placed.

<div class="row buttons">

<?php echo CHtml::submitButton('Save'); ?>


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

</div><!-- form -->


Here I have used two partial views. For example, from User and brand models.

_user.php for User.

<?php echo $this->beginContent('layout',array('model'=>$model));?>

	<div class="row">

		<?php echo $form->labelEx($model,'username'); ?>

		<?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>128)); ?>



	<div class="row">

		<?php echo $form->labelEx($model,'email'); ?>

		<?php echo $form->textField($model,'email'); ?>


<?php echo $this->endContent(); ?>	

_brand.php for Brand.

<?php echo $this->beginContent('layout',array('model'=>$model));?>

	<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>256)); ?>


<?php echo $this->endContent(); ?>

This is the main view file where ajax calls are made.



echo CHtml::ajaxLink(CHtml::encode('form1'),array('test/forms','view'=>1),array(




echo "</br>";

echo CHtml::ajaxLink(CHtml::encode('form2'),array('test/forms','view'=>2),array(





<div id="test"></div>



public function actionForms() {

	$user=new User;

	$brand=new Brand;





           {//DO something here}


           {//Do something here}



	if(isset($_GET['view'])) {

	   if($_GET['view'] ==1) 






	else $this->render('forms');                    


This is easily done, if we use CHtml static functions.

It is becomes difficult, when we use CActiveForm.

We have to properly begin and close the widget tags.

If you have any nicer solution than this, kindly share with us.


I ended up using a combination of softark and seenivasan. I ended up rendering all of the sub-form pieces on the page and used JS to toggle their display. I also needed a way to know which sub-forms the user was filling out so I added a hidden flag for each that gets set when it is displayed. Then I created a main.php layout for each sub-form to share. I also turned all of this into a widget. The reason I did this is because I find it neatly organizes my code by keep everything together.

The structure is like this












MyWidget.php class file loads in the JS file on init() and run() displays the socialIcons followed by the different sub-forms.

This seems to work for now.