CodeIgniter template engine Use instance _php instance

Source: Internet
Author: User
Tags autoload php foreach codeigniter

One, example:

It is often used in this way when using CodeIgniter:

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

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

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

This loads the view about.php into the template template file.

Second, the installation

Download Ci_template_library.zip
After decompression, the template.php is placed in the Application/libraries Application class library directory;
The application starts to load application/config/autoload.php automatically;

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.

Four, create a view application/views/about.php
Add the following code:

 
 

Load a view in 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 and the variable 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_inst Ance ();
 $this->set (' contents ', $this->ci->load->view ($view, $view _data, TRUE)); 
 return $this->ci->load->view ($template, $this->template_data, $return);

v. Skills Summary :

Advanced Tip 1: Simpler short tags in a template

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

 
 

Then set it directly in the controller:

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

Advanced Tip 2: Highlight current navigation

Navigation is usually used in templates, where a good navigation should tell the user where the current location is sorted.

Define your navigation items:

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

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

Update your Template:

Increase 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, and you need to add the relevant small helper to the automatic load configuration:

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

To update your controller:

Increase:

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

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

Advanced Tips 3: Multiple templates

The simplest way to handle multiple templates is to define several 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.