ci框架集成Smarty

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


    Paste_Image.png
  2. 设置该路径常量
    入口文件index.php


    Paste_Image.png

    Paste_Image.png
  3. 新建Csmarty.php对smarty进行封装,需要新建编译目录
    application/libraries/Csmarty.php
    application/views/templates_c
    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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{$test}}
11111111
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容