Demo example of integrated Smarty engine implemented by CodeIgniter framework


An example of this article describes the integrated Smarty engine implemented by CodeIgniter framework. To share with you for your reference, as follows:

Smarty’s template mechanism is very powerful. Generally, CI framework does not need to integrate other template tags, because PHP itself is a kind of tag, which is easy to use. CodeIgniter integrates Smarty tutorials (I use the latest versions) as follows:

Step 1: download the latest version of CodeIgniter: CodeIgniter framework source code
Step 2: download the latest version of Smarty: Smarty engine source code
Step 3: specific configuration

I have uploaded my integrated code, and can download and read those who are interested. CodeIgniter framework integrates Smarty engine demo.

1. Preparation

Copy smart to application / libraries, and then create templates, templates_c, config, and cache in the root directory. The structure is as follows:

2. Modify entry file

Add in the entry file index.php:

define('ROOT', dirname(__FILE__));

3. New ci_smarty.php

Create ci_smarty.php in the libraries file, and write the following code:

* =======================================
* Created by PK Technology.
* Author: ZhiHua_W
* Date: 2016/10/31 0031
*Time: 9:16 am
*Project: CI integration
*Power: CI framework integration smart
* =======================================
defined('BASEPATH') OR exit('No direct script access allowed');
require(APPPATH . 'libraries/smarty/Smarty.class.php');
class CI_Smarty extends Smarty
    public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '')
      if (is_array($template_dir)) {
        foreach ($template_dir as $key => $value) {
          $this->$key = $value;
      } else {
        //Root is the root directory of the web application defined by CodeIgniter in the entry file index.php
        $this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
        $this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
        $this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
        $this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';

4. Use in controller

Write the usage method in the controller welcome.php. The code is as follows:

defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller
     * Welcome constructor.
     *Write constructor, introduce CI ﹣ Smarty class file
    public function __construct()
     *Smart test function
    public function test()
      $this->ci_smarty->assign('test', 'smarty');

5. Create template attempt

Create the test.tpl file in the Templates folder, and write the following code:

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  < title > CodeIgniter integrate Smarty test < / Title >
This is the {$test} test

6、 Visit

So far, we have completed the integration and visited:http://localhost/Codeigniter_Smarty/index.php/Welcome/testYou can see the test results.

For more information about CodeIgniter, readers who are interested in CodeIgniter can view the following topics: CodeIgniter tutorial, CI (CodeIgniter) framework advanced tutorial, PHP excellent development framework summary, ThinkPHP tutorial, ThinkPHP common methods summary, Zend Introduction to framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation and summary of common database operation skills in PHP

I hope that this article will be helpful for you to Design PHP program based on CodeIgniter framework.

Recommended Today

The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]