CodeIgniter template engine uses instance _ php instance

Source: Internet
Author: User
Tags html header php foreach codeigniter
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 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.