Yii Framework Forum: How to include other files in Layout file - Yii Framework Forum

Jump to content

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

How to include other files in Layout file Rate Topic: -----

#1 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 14 April 2010 - 07:26 AM

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
0

#2 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 14 April 2010 - 08:48 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#3 User is offline   whoopass 

  • Advanced Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 119
  • Joined: 27-May 09
  • Location:Ajax, Ontario, Canada

Posted 14 April 2010 - 08:57 AM

View PostBlue Sapphire, on 14 April 2010 - 07:26 AM, said:

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


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

#4 User is offline   notzippy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 06-October 08

Posted 14 April 2010 - 10:55 AM

The Yii way of doing this is to use the beginContent tag
http://www.yiiframew...nContent-detail

nz
0

#5 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 14 April 2010 - 11:53 AM

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
0

#6 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 14 April 2010 - 12:49 PM

View PostBlue Sapphire, on 14 April 2010 - 11:53 AM, said:

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).
0

#7 User is offline   phpdevmd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 16-April 09

Posted 14 April 2010 - 01:58 PM

View PostBlue Sapphire, on 14 April 2010 - 07:26 AM, said:

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


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')); ?>

0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 April 2010 - 03:03 AM

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. ;)
1

#9 User is offline   Rohit Suthar 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 219
  • Joined: 12-May 14
  • Location:Mumbai, India

Posted 11 July 2014 - 05:56 AM

Best way like -

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

Thanks & regards,

ROHIT SUTHAR
Mumbai(India)

Follow me - @rohisuthar

www.yiiframework.us
0

#10 User is offline   Louis Gac 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 13-June 14

Posted 11 July 2014 - 06:29 AM

The most important thing in Layout inclusion is to use double dash :
 
http://www.yiiframew...youtFile-detail

Quote

absolute view within the application: the view name starts with double slashes '//'. In this case, the view will be searched for under the application's view path. This syntax has been available since version 1.1.3.


So, in layout, just do :

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

0

Share this topic:


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

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