Use layout to enrich the display elements of the view layer in the CI framework-PHP Tutorial

Source: Internet
Author: User
The CI framework uses layout to enrich the display elements of the view layer. Generally, during website development, a template or layout contains the appearance and layout of the entire website, which is generally divided into header, content, footer may even include a template or layout During website development. it contains the appearance and layout of the entire website, which is generally divided into header, content, some footer may even include menus. The content part varies with different pages and functions. This will ensure the consistency of the website style and reduce the workload. Although the convenient Codeigniter does not provide a built-in processing method, we still need to expand it ourselves.

First, save the following code to application/libraries/Layout. php.

 obj =& get_instance();         $this->layout = $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);         }     } } ?>

Load it in the relevant controller:

$this->load->library('layout');

You can output the page below:

$ Data ["page_title"] = ""; $ data ["Keywords"] = "Internet, technology "; $ data ["Description"] = "focus on Internet technology"; $ this-> layout-> view ('support', $ data );

Our template or layout file (layout_main.php) is roughly as follows (simplified, there are many fixed website elements in actual application ):

      
  
     

The View loaded in the controller, for example, "support ". We only need to focus on the support content. It will be filled in the template or $ content_for_layout part of the layout file and output together.

If our template or layout doesn't want to be called layout_main.php, for example, template. php; then we need to specify the template name when loading the layout library, and specify the second parameter (without the php suffix ):

$this->load->library('layout','template');

The layout template is also called layout. it contains the appearance and layout of the entire website. it is generally divided into header, content, footer, and even package...

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.