Layouts usage in yii (header, footer usage)

Source: Internet
Author: User
1. Add the layout field to config/Main. php. Return array ('defaultcontroller' => 'index', 'layout '=> 'main ', 2. Create 3 files in layouts, Main. php, header, footer Main. php file content ( Index. php will be output as $ content, and render will set which template to display ) < ? PHP $ this - > Begincontent ( '// Layouts/head' ); ? > < ? PHP $ this - > Endcontent (); ? > < ? PHP Echo $ Content ; ? > < BR > Main. php -- --
< ? PHP $ this - > Begincontent ( '// Layouts/footer' ); ? > < ? PHP $ this - > Endcontent (); ? > Header. php and footer. php output Header. php --- and <br> footer --- 3. Simple output in views/index. php <Br> <? PHP echo $ post-> m;?> 4. c control $ Post-> M = 'index ---- '; $ this-> render ('index', array ('post' => $ post )); 5. Final execution result Header. php --- index ---- main. php ---- footer. php --- !!! If renderpartial is used in the controller !!! $ This-> renderpartial ('index', array ('post' => $ post); then only the index is output ---- layouts is not called Annotation: render renders layout, while renderpartial does not. About parameter passing through layouts You need to define a variable public $ A = NULL in the Controller; then assign a value in the action, and then you can call the following in layout: Code
  class  indexcontroller  extends   ccontroller {  Public   $ A  =  null  ;  Public   Fu Nction   actionindex () { post post -> M = 'index ---- ';  $ this -> A = 'Hello! ';  $ this -> render ('index',  array   ( 'post' =>  $ post , );  

call methods in layouts. Main. php
A;?>
output result: Hello!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.