Multiple layouts: how does it work?

I’ve been looking at the default code generated by yiic. It creates a 1-column and a 2-column layout.

The default column2.php uses beginContent() and endContent(). Inbetween these two functions there is some code that is to replace a part of the code in main.php. My attempts to alter these files have not been very successful. I can’t figure out how it determines what to replace - and what not to replace.

How does this work? Are there certain div classes or id’s it looks for? it replaces some parts of the code in the container div, but others remain… I’m confused ;)

In main.php the line <?php echo $content; ?>

echoes the content of the column1.php or column2.php, so they do not replace part of the code in main.php their content is just echoed inside main.php

Now why didn’t I think of that? :huh:

Thanks for the explaination!

I’ve just been trying to achieve this with layouts in a theme directory













Unfortunately only the requested view is displayed, without any rendering: all html in column1 and main are ignored.

Does anyone have any ideas?


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

	<div id="content-top"></div>

	<div id="content-middle">

		<?php echo $content; ?>

	</div><!-- .middle -->

	<div id="content-bottom"></div>

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


<!DOCTYPE html>

<html xmlns="" xml:lang="en" lang="en">


	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/main.css" />



	<div id="header">

		<?php $this->widget('widgets.WDbMenu',array(




		)); ?>


	<div id="canvas">

		<?php echo $content; ?>

	</div><!-- canvas -->

	<div id="footer"></div>




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

Shouldn’t it be

<?php $this->beginContent('application.views.layouts.main'); ?>

// or

<?php $this->beginContent(path to view); ?>

This is as much a question as an answer, the layouts are a little confusing. Can there only be one $content sent to the page?

I end up doing a lot of data access from the views, and I would rather be doing it from the controller. I’m not sure I am being clear here.

When you define $this->layout that seems to be the end of the line so if $this->layout is column2 because the layout view column2.php starts with <?php $this->beginContent(‘application.views.layouts.main’); ?> Yii knows to look for that layout, at least that’s how I understand what’s going on.


Uh, oh, of course… I need to tell where to find the main layout…


I ran into a new issue:

I’m using a theme. This means my /protected/views is as empty as possible. It does not even contain a layouts folder. Everything is in /themes/themeName/views, including the layouts folder. I’ve also built a module, and it looks for its layout in /protected/modules/moduleName/views/layouts. I don’t want that because it doesn’t need its own layout and I want to reuse code as much as possible.

How do I make it look for the layout in the views of the theme folder in stead of within the module views?

The least code duplication I’ve managed to achieve so far is by creating the column1.php (and others) file and refer it to the themed main.php like this:

<?php $this->beginContent('webroot.themes.'.Yii::app()->theme->name.'.views.layouts.main'); ?>

This variable declaration doesn’t seem to work in a controller:

public $layout='webroot.themes.'.Yii::app()->theme->name.'.views.layouts.column1';

This variable declaration would work in a controller, but causes hard-coding the theme which is bad:

public $layout='webroot.themes.themeName.views.layouts.column1';

Would appreciate some advice on this one. Thanks!

Just wanted you guys know that there is known bug issue on Yii 1.1.2 for Custom Theme…

in views/layouts/column1.php and views/layouts/column2.php, change:




You can check out more details about this bug issue on Yii’s Google Code, here is the link:

Please don’t forget to rank this, too.