I am new to YII2
, so this can be very basic question.
I have set up initial application. I have setup theme for my YII2
application /themes/standard
Now, There is a default layout file themes/standard/layouts/main.php
- This has the html code for header and footer
I want to separate the header code into themes/standard/layouts/header.php
and footer into another file
I tried something like below code in main.php
<?php $this->render("header"); ?>
tried this as well
<?php $this->render("//layouts/header"); ?>
But It doesn't render the content. I don't want to absolute path since I have theming Can you people help with this one?
In order to have Nested Layouts
, You can use beginContent()
and endContent()
like below(In your main.php
layout for example):
<?php $this->beginContent('@app/views/layouts/header.php'); ?>
<!-- You may need to put some content here -->
<?php $this->endContent(); ?>
Everything between begin
and end
will be replaced wit $content
in header.php
.
As of Yii2
's official example:
Sometimes you may want to nest one layout in another. For example, in different sections of a Web site, you want to use different layouts, while all these layouts share the same basic layout that generates the overall
HTML5
page structure. You can achieve this goal by callingbeginContent()
andendContent()
in the child layouts like the following:
<?php $this->beginContent('@app/views/layouts/base.php'); ?>
...child layout content here...
<?php $this->endContent(); ?>
As shown above, the child layout content should be enclosed within
beginContent()
andendContent()
. The parameter passed tobeginContent()
specifies what is the parent layout. It can be either a layout file or alias. Using the above approach, you can nest layouts in more than one levels.
http://www.yiiframework.com/doc-2.0/guide-structure-views.html#nested-layouts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With