CodeIgniter Template Engine Usage Example
- 2021-07-09 07:24:19
- OfStack
1. Example:
Usually, when using codeigniter, it is often loaded in this way:
$this->load->view('about', $data);
With this class library, you can load 1 view into this template:
$this->template->load('template', 'about', $data);
The view about. php is loaded into the template template file.
Step 2 Install
Download ci_template_library. zip
After decompressing, put Template. php into application/libraries application class library directory;
The application program starts and automatically loads application/config/autoload. php;
3. Create a template file application/views/template. php
The code in the template is as follows:
<html>
<body>
<div id="contents"><?= $contents ?></div>
<div id="footer">Copyright 2008</div>
</body>
</html>
$contents is what you need to insert in the controller.
4. Create a view application/views/about. php
Add the following code:
<h1>About</h1>
<p>I'm so human!</p>
Loading views in the template engine
Can be used in your controller
$this->template->load('template', 'about');
This template engine workflow:
The view is loaded into a variable, which will be 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. Summary of skills:
Advanced Tip 1: Simpler Short Tags in Templates
Example: If you need to display the title in the page.
Then views/template. php increases at the head of HTML:
<head>
<title><?= $title ?></title>
</head>
Then set it directly in the controller:
$this->template->set('title', 'About me');
Advanced Tip 2: Highlight the current navigation
Navigation is usually used in templates, and a good navigation experience should tell the user what the current location classification is.
Define your navigation items:
Introduce application/libraries/Template. php, then add:
$this->set('nav_list', array('Home', 'Photos', 'About', 'Contact'));
Update your template:
Add 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 it is necessary to add related small assistants to the auto-load configuration:
$this->template->load('template', 'about', $data);
0
Update your controller:
Add:
$this->template->load('template', 'about', $data);
1
It should be noted that:
1 If all navigation is in one controller, you can add a general navigation code to the destructor;
2 · Define the current navigation style, for example: # navigation. selected
Advanced Tip 3: Multiple Templates
The simplest way to deal with multiple templates is to define multiple new methods to replace existing content in libraries/Template. php. The second advanced technique uses custom methods:
$this->template->load('template', 'about', $data);
2
Paste the code into the controller
$this->template->set('nav', 'About');
$this->template->set('title', 'About me');
$this->template->load_main('about');