Yii Framework Forum: Multiple layouts: how does it work? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Multiple layouts: how does it work? Rate Topic: -----

#1 User is offline   toMeloos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 09

Posted 07 May 2010 - 04:16 AM

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

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 07 May 2010 - 04:28 AM

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
Find more about me.... btw. Do you know your WAN IP?
2

#3 User is offline   toMeloos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 09

Posted 07 May 2010 - 05:33 AM

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

Thanks for the explaination!
1

#4 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 08 May 2010 - 11:11 AM

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

root
|
 `-themes
    |
    `-lake
       |
       `-views
          |
          `-layouts
             |
             +-main.php
             `-column1.php


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

column1.php:
<?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(); ?>


main.php:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/main.css" />
</head>

<body>
	<div id="header">
		<?php $this->widget('widgets.WDbMenu',array(
			'id'=>'mainmenu',
			'menu'=>'MainMenu',
			'showTitle'=>false,
		)); ?>
	</div>

	<div id="canvas">
		<?php echo $content; ?>
	</div><!-- canvas -->

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

1

#5 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 248
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 08 May 2010 - 02:28 PM

Ummm..

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

doodle
Check out myYii powered website
0

#6 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 08 May 2010 - 06:21 PM

View Postgot 2 doodle, on 08 May 2010 - 02:28 PM, said:

Ummm..

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

doodle

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

Thanks.
0

#7 User is offline   toMeloos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 09

Posted 10 May 2010 - 12:11 PM

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!
0

#8 User is offline   jaxyeh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 23-February 10

Posted 10 May 2010 - 12:37 PM

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:
$this->beginContent('application.views.layouts.main');
to
$this->beginContent();


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

http://code.google.c.../detail?id=1043

Please don't forget to rank this, too.

-Jason
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users