ci框架集成Smarty

  1. vendor目录放置扩展包
    新建目录/vendor/Smarty-3.1.7
    下载smarty,并将smarty中libs放到/vendor/Smarty-3.1.7下


    ci框架集成Smarty_第1张图片
    Paste_Image.png
  2. 设置该路径常量
    入口文件index.php


    ci框架集成Smarty_第2张图片
    Paste_Image.png

    Paste_Image.png
  3. 新建Csmarty.php对smarty进行封装,需要新建编译目录
    application/libraries/Csmarty.php
    application/views/templates_c
    ci框架集成Smarty_第3张图片
    Paste_Image.png
  4. Cmarty.php
require_once VENDORPATH . "Smarty-3.1.7/libs/Smarty.class.php";
class Csmarty
{
    private $smarty;
    public function __construct($templatePath = '', $param = array())
    {
        if (empty($templatePath)) {
            $templatePath = APPPATH . 'views/';
        }
        $this->smarty = new Smarty();
        $this->smarty->left_delimiter = "{{";
        $this->smarty->right_delimiter = "}}";
        $this->smarty->compile_check = true;
        $this->smarty->caching = false;
        //$this->smarty->escape_html = true;
        $this->smarty->template_dir = $templatePath;
        $this->smarty->compile_dir = $templatePath . 'templates_c' . DS;
        $this->smarty->loadFilter("variable", "htmlspecialchars");
        foreach ($param as $key => $val) {
            $this->smarty->$key = $val;
        }
    }
    public function assignRaw($var, $value)
    {
        $this->smarty->assign($var, $value);
    }
............
  1. controller调用
class Saa extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('csmarty');
    }

    public function index()
    {
        $this->csmarty->assignRaw('test','333');
        $this->csmarty->display('smarty.html');
    }
}

views/smarty.html




    
    Title


{{$test}}
11111111


你可能感兴趣的:(ci框架集成Smarty)