This article describes how to use the CodeIgniter template engine. For more information, see
This article describes how to use the CodeIgniter template engine. For more information, see
I. Example:
Codeigniter is usually used for loading:
$ This-> load-> view ('about', $ data );
Through this class library, you can load a view into this template:
$ This-> template-> load ('template', 'about', $ data );
Load view about. php to the template file.
Ii. Installation
Download ci_template_library.zip
Decompress the package and put Template. php In the application/libraries application class library directory;
Automatically load application/config/autoload. php when the application starts;
3. Create a template file application/views/template. php
The code in the template is as follows:
<? = $ Contents?>
Copyright 2008
$ Contents shows the content to be inserted in the controller.
4. Create a view application/views/about. php
Add the following code:
About
I'm so human!
Load view in template engine
Can be used in your Controller
$ This-> template-> load ('template', 'about ');
Workflow of this template engine:
The view is loaded into a variable, which is loaded into the template.
Var $ template_data = array (); function set ($ name, $ value) {$ this-> template_data [$ name] = $ value ;} function load ($ template = '', $ view ='', $ view_data = array (), $ return = FALSE) {$ this-> CI = & get_instance (); $ this-> set ('contents', $ this-> CI-> load-> view ($ view, $ view_data, TRUE )); return $ this-> CI-> load-> view ($ template, $ this-> template_data, $ return );}
5. tips:
Tip 1: simple short tag in the template
Example: If you need to display the title on the page.
Add the following in the HTML header views/template. php:
<? = $ Title?>
Then, directly set in the controller:
$ This-> template-> set ('title', 'aboutme ');
Tip 2: highlight the current navigation
Navigation is usually used in a template. A good experience navigation should tell the user what the current location category is.
Define your navigation project:
Introduce application/libraries/Template. php and add the following to the Controller:
$ This-> set ('nav _ list', array ('home ', 'photos', 'about', 'Contact '));
Update your template:
Add the following in application/views/template. php:
<? Php foreach ($ nav_list as $ I => $ nav_item):?>
- <? = Anchor ($ nav_item, $ nav_item)?>
<? Php endforeach?>
The anchor function is used here. You need to add the related Assistant to the automatic loading Configuration:
$ Autoload ['helper '] = array ('url ');
Update your controller:
Added:
$ This-> template-> set ('nav', 'about ');
Note:
1. If all navigation is in a controller, you can add General navigation code to the destructor;
2. Define the style of the current navigation, for example: # navigation. selected
Advanced Tip 3: Multiple templates
The simplest way to process multiple templates is to define multiple new methods in libraries/Template. php to replace existing content. The second advanced technique is to use a custom method:
Function load_main ($ view = '', $ view_data = array (), $ return = FALSE) {$ this-> set ('nav _ list', array ('home ', 'photos', 'about', 'contact'); $ this-> load ('template', $ view, $ view_data, $ return );}
Paste the code into the Controller
$ This-> template-> set ('nav', 'about'); $ this-> template-> set ('title', 'aboutme '); $ this-> template-> load_main ('about ');