CodeIgniter Template engine Usage examples

Source: Internet
Author: User
Keywords CodeIgniter template engine
Tags autoload html header php foreach codeigniter
This article mainly introduces the use of CodeIgniter template engine example, the need for friends can refer to the following

One, example:

This is often used when using CodeIgniter to load:

$this->load->view (' about ', $data);

With this class library, you can load a view into this template:

$this->template->load (' template ', ' about ', $data);

The view about.php is loaded into the template file.

Second, installation

Download Ci_template_library.zip
After decompression, put the template.php into the Application/libraries Application class library directory;
Application launches auto-load application/config/autoload.php;

third, create a template file application/views/template.php
The code in the template is as follows:


$contents is what you want to insert in the controller.

Iv. Creating a view application/views/about.php
Add the following code:


Loading views into the template engine
In your controller, you can use the

$this->template->load (' template ', ' about ');

This template engine workflow:

The view is loaded into a variable that is loaded into the template

var $template _data = Array (); function set ($name, $value) {$this->template_data[$name] = $value;} function load ($template = ", $view =", $view _da TA = 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);}

v. Summary of skills :

Advanced Tip 1: Simpler short marks in templates

Example: If you need to display the title in the page.
Then in the HTML header views/template.php added:


Then set directly in the controller:

$this->template->set (' title ', ' About me ');

Advanced Tip 2: Highlight current navigation

Navigation is often used in templates, and an experienced navigation should tell the user what the current location classification is.

To define your navigation items:

Introduce the application/libraries/template.php and add it to the controller:

$this->set (' nav_list ', Array (' Home ', ' Photos ', ' about ', ' contact ');

To update your template:

Added in application/views/template.php:

<ul class= "Navigation" > <?php foreach ($nav _list as $i + $nav _item):?> <li class= "<?= ($nav = = $nav _ Item? ' Selected ': '?> ' > <?= anchor ($nav _item, $nav _item)?> </li> <?php Endforeach?></ul>

The anchor function is used here to add the associated small helper to the auto-load configuration:

$autoload [' helper '] = array (' URL ');

To update your controller:

Increase:

$this->template->set (' nav ', ' about ');

Need to note:
1 · If all the navigation is in one controller, you can add the common navigation code in the destructor;
2 · Define the style of the current navigation, for example: #navigation. Selected

Advanced Tip 3: Multi-template

The simplest way to handle multiple templates is to define multiple new methods in libraries/template.php to replace existing content, and the second advanced technique uses 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.