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:

_facebook.php


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

    <h4>FaceBook</h4>

    <?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>



_twitter.php


  

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

    <h4>Twitter</h4>

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

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

  </div>



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>

  </div>



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.

layout.php




<h2>COMMON LAYOUT</h2>


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




<div class="form">


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

        'action'=>array('test/forms'),

	'id'=>get_class($model),

)); ?>


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




<div class="row buttons">

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

</div>


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




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


</div><!--layoutStyle-->



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>

	


	<div class="row">

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

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

	</div>





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

	</div>


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




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

forms.php




<?php

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

 

'update'=>"#test"

));


echo "</br>";


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

 

'update'=>"#test"

));


?>

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



Controller

Test.php




public function actionForms() {

	$user=new User;

	$brand=new Brand;

	$form=$this->beginWidget('CActiveForm');

	$this->endWidget();

	

	if(isset($_POST['User']))

           {//DO something here}


	if(isset($_POST['Brand']))

           {//Do something here}

	

	

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

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

		$this->renderPartial('_user',array('model'=>$user,'form'=>$form),false,true); 

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

		$this->renderPartial('_brand',array('model'=>$brand,'form'=>$form),false,true); 

		                 }

						    

	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.

Regards.

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/

  assets/

      js/

        mywidget.js

  views/

      input/

        main.php

        _form1.php

        _form2.php

      icons.php

  MyWidget.php



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.