How to include other files in Layout file

Hi!

Iam newbie to YII. Can some one guide me the fastest way to include '_header.php', '_footer.php' file in my main layout file labelled as 'main.php'.

Thanks in advance

Personally i use the layout as the header and footer for my applications. That’s the purpose of the layout file. You can do in your layout:




....header stuff...

<?php echo $content; ?>

...footer stuff...



Will be the same as doing require for the header.php and footer.php files inside the layout.

What Vince pointed out is corret. As it currently stands you’ll find a file called main.php under the folder /protected/views/layout/ which holds all the information regarding the main layout of the site.

This main layout has the headers in it and the footer made at the bottom. I suppose if you really wanted to use include files for your header and footer, you could just replace the location of the header and footer in that main.php file with include("path/to/headerOrfooter");

To keep it simple, make two new files in the same directory as the main.php, call it header and the other footer that way you just have to go include("header"); or include("footer");

I think that’s what you’re asking unless we interpreted the question wrong.

The Yii way of doing this is to use the beginContent tag

http://www.yiiframework.com/doc/api/CBaseController#beginContent-detail

nz

Thanks for guiding. You are right, that files can be included using ‘include’ . My point is that what is the fastest method in YII to include files. Can we use ‘import’ to include files. The reason for it is, in documentation I had read that ‘import’ is faster than ‘include’ or ‘require’.

If so then ‘import’ will be used in same manner as ‘include’, ‘require’ are used ?

Thanks in advance

Import won’t actually run the content of the file though, that’s part of what makes it faster. It’s essentially adding it to the autoloading list of files to check for classes (at least that’s my understanding).

Use renderPartial:


<?php echo $this->renderPartial('_header', array('title'=>'Yii Rock!')); ?>

<?php echo $content; ?>

<?php echo $this->renderPartial('_footer', array('title'=>'Copyright 2010','param2'=>'hi')); ?>

You can’t beat require()/include() if all you want to do is include some content. Anything else is more “expensive”. Don’t make the mistake and try to find a “Yii-ish” solution for everything. If your sub-view doesn’t use parameters but only static content, why use render() or renderPartial()? It only wraps the require() call (see CBaseController.php::renderInternal()) and adds some output buffering. This will eat some valuable CPU time.

So if you want best performance for your apps, keep things easy and make use of PHP’s powerful functions where applicable.

Above is IMHO, of course. ;)

Best way like -


require_once(Yii::app()->basePath . '/extensions/folder_name/file_name.php');

The most important thing in Layout inclusion is to use double dash :

http://www.yiiframework.com/doc/api/1.1/CController#getLayoutFile-detail

So, in layout, just do :


<?php echo $this->renderPartial('//layouts/_footer');  ?>