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