Implement Layout for Codeigniter

Source: Internet
Author: User
In web development, some modules are usually public, such as header, footer, and menu. layout uses the public part as a template, which not only reduces the workload, but also facilitates later maintenance. (I wrote an article about ZendFramework.

In web development, some modules are usually public, such as header, footer, and menu. layout uses the public part as a template, which not only reduces the workload, but also facilitates later maintenance. (I wrote an article about Zend Framework Implementation Layout ).
The CI framework has many Layout functions. one of the following is described:
1. create the directory _ layouts under the View views directory to store the layout file.
2. libraries New extension Layout. php:

 obj =& get_instance();         $this->layout = '_layouts/' . $layout;     }       function setLayout($layout)     {       $this->layout = $layout;     }         function view($view, $data=null, $return=false)     {         $data['content_for_layout'] = $this->obj->load->view($view,$data,true);                 if($return)         {             $output = $this->obj->load->view($this->layout,$data, true);             return $output;         }         else         {             $this->obj->load->view($this->layout,$data, false);         }     } } ?>

3. note: By default, member is used as layout. Therefore, in _ layouts, create the layout view file member. php:
 This is the layout of member.
 

4. use the following in controller:
Class Welcome extends CI_Controller {public function _ construct () {parent ::__ construct (); $ this-> load-> library ('layout ');} public function test_layout () {$ data ['test'] = 'this is a PHP point-to-point test tutorial'; $ this-> layout-> view ('content ', $ data );}}

5. create content. php in views as follows:
This is content
 

The directory structure is as follows:

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.