CodeIgniter template engine instance

Source: Internet
Author: User
Tags php foreach
This article describes how to use the CodeIgniter template engine. For more information, see the CI framework.

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 fileApplication/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 viewApplication/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. skills summary:

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', 'About me');

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', 'About me');$this->template->load_main('about');

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.