the "$content" in layout

Hi everybody,

can i put two different "$content" in layout?

i try to do that because i have to do a web site that contain 2 separated zones that containt dynamic content.

please refer to the joined image to see the architecture of the web site

PS : the yellow section is static and the pink section is the dynamic section

i want to put all the static section in the main layout and the dynamic section will be imported by the "$content".

how can i do that?

thanks for help :)

sorry, this is the image

:P

I think that is not possible to structure like that.

Create a view for the 2 parts like that:




<?php $this->renderPartial('_subView1');?>


[... Your static contente here...]


<?php $this->renderPartial('_subView2');?>



Like that you can achive what you want. The variable $content in the layout contain the result of the rendering of the view of the controller, you cannot change this behavior.

You should push all dynamic content, both parts, on the view of the controller, but this don’t change anything, simply this part will be in views/controllerID/mainView instead of views/layout/main.

If you use this feature in more than one controller, you can create a widget for this pourpose.

Thanx for replying, i understand your idea.

However i look into the layout column1.php generated by yiic, it contain


$this->beginContent2('//layouts/main');

.

i think i could do that by spacifying in each view a layout that containt the $content and the other dynamic part.

Is this a solution?

No, layout should be something of more static.

The left dynamic part looks like a left menu, you can create using a widget, like is CMenu in the default layout. You can create some public property in the controller for pass to this widget all data you need for make it dynamic.

Once that the left part is separated, I guess that will not be a great inconvenient to use a single view for the central part.

This is more close to the basic implementation, using layout for doing the work of views can be a bad idea.

ok i get it thanx so much :)

Not wanting to add any confusion here but…

@zaccaria I understand that this is typically how we use the layouts but the class reference here

Suggest that there might be a bit more to it than that. Usually we use column1 or column2 layout. But I think with only column1 and column2 this feature is under utilized.


$this->beginContent('path/to/view');

// ... content to be decorated

$this->endContent();

If you take a basic install

in the controller specify some custom layout.


public function actionIndex()

	{

		// renders the view file 'protected/views/site/index.php'

		// using the default layout 'protected/views/layouts/main.php'

            $this->layout = 'columnxx';

		$this->render('index');

	}

create a new layout file (columnxx)


<?php $this->beginContent('//layouts/main'); ?>

<div class="container">

	<div class="span-19">

		<div id="content">

			<?php echo $content; ?>

		</div><!-- content -->

	</div>

	<div class="span-5 last">

		<div id="sidebar">

		<?php

			$this->beginWidget('zii.widgets.CPortlet', array(

				'title'=>'Operations',

			));

			$this->widget('zii.widgets.CMenu', array(

				'items'=>$this->menu,

				'htmlOptions'=>array('class'=>'operations'),

			));

			$this->endWidget();

		?>

		</div><!-- sidebar -->

	</div>

    <div class="span-24"><?php echo Yii::app()->name ?></div>

</div>

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

We can access Yii::app()->name

The reason we use the variable $content is because of the render method.

This is in CController a class that all of our controllers inherit (typically)


public function render($view,$data=null,$return=false)

{

    $output=$this->renderPartial($view,$data,true);

    if(($layoutFile=$this->getLayoutFile($this->layout))!==false)

// this line is setting the value of $content

        $output=$this->renderFile($layoutFile,array('content'=>$output),true);

//

    $output=$this->processOutput($output);


    if($return)

        return $output;

    else

        echo $output;

}

I’m pretty sure if we create our own ‘render’ method in a controller or in the controller component we will be able to process more than one variable once we are in the layout.

One day when I have time ::) I will look into this!

doodle

Ok ,I couldn’t help myself

In sitecontroller

created this function


public function render($view,$data=null,$return=false)

{

    $output=$this->renderPartial($view,$data,true);

    if(($layoutFile=$this->getLayoutFile($this->layout))!==false)

        $output=$this->renderFile($layoutFile,array('content'=>$output,'testMessage'=>'How`z it goin buddy'),true);


    $output=$this->processOutput($output);


    if($return)

        return $output;

    else

        echo $output;

}

the index action


	public function actionIndex()

	{


            $this->layout = 'columnxx';

		$this->render('index');

	}

my custom layout ‘/views/layouts/columnxx.php


<?php $this->beginContent('//layouts/main'); ?>

<div class="container">

	<div class="span-19">

		<div id="content">

			<?php echo $content; ?>

		</div><!-- content -->

	</div>

	<div class="span-5 last">

		<div id="sidebar">

		<?php

			$this->beginWidget('zii.widgets.CPortlet', array(

				'title'=>'Operations',

			));

			$this->widget('zii.widgets.CMenu', array(

				'items'=>$this->menu,

				'htmlOptions'=>array('class'=>'operations'),

			));

			$this->endWidget();

		?>

		</div><!-- sidebar -->

	</div>

    <div class="span-24"><?php echo $testMessage ?></div>

</div>

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

@karim gioca I think this is what you are looking for to have dynamic data all over different areas of the page.

doodle

From where is the content of $test_message?

You simply did the same work of menu, in wiche there are the dynamic generated items:




                        $this->widget('zii.widgets.CMenu', array(

                                'items'=>$this->menu,

                                'htmlOptions'=>array('class'=>'operations'),

                        ));



Wich are setted as public property of the controller.

There is no need of change the render function, you can simply add a property to the controller in order to do that, is already all working.

The original poster asks

This is a method for defining $content1 $content2 etc.

There are many ways to accomplish the same thing.

I never suggested anything was not working it’s simply another approach.

doodle

got4doodle, I didn’t want to be rude or to make you angry.

Of corse there are different ways for achive the same result, but is always better to use known paths.

Agreed, sticking to convention is best so that developers can work together.

Cheers :D

doodle