美文网首页
ci框架集成Smarty

ci框架集成Smarty

作者: iscona | 来源:发表于2017-02-21 14:50 被阅读0次
    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>
    

    相关文章

      网友评论

          本文标题:ci框架集成Smarty

          本文链接:https://www.haomeiwen.com/subject/guvywttx.html