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!