Ci框架整合smarty模板引擎

 

 

Ci框架整合smarty模板引擎

 

备注:下载smarty时,最好选择2.6版本,其他测试有坑,ci可以是2.2或其他

大体思路:将smarty封装成ci框架的一个类,然后重新配置一下smarty,这样ci框架就可以调用smarty模板引擎了

1.   将下载好的smarty包的lib文件上传到ci中的libraries 文件中,将取名称修改为smarty,在libraries文件新建cismarty.php文件,内容如下:

#require_once  记得要导入该smarty类文件

#$this->ci = & get_instance();  获取ci的超全局对象

#$this->ci->load->config('smarty');  加载配置文件

<?php
       if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
       require_once( APPPATH .'libraries/smarty/libs/Smarty.class.php' );
       class Cismarty extends Smarty{
              protected $ci;
              public function __construct(){

                  $this->ci = & get_instance();
                  $this->ci->load->config('smarty');
                  parent::__construct();

                 $this->template_dir   = $this->ci->config->item('template_dir');
                 $this->complie_dir    = $this->ci->config->item('compile_dir');
                 $this->cache_dir      = $this->ci->config->item('cache_dir');
                 $this->config_dir     = $this->ci->config->item('config_dir');
                 $this->caching        = $this->ci->config->item('caching');
                 $this->cache_lifetime = $this->ci->config->item('lefttime');
              }
       }

2.   在config下新建smarty.php配置文件

#第一个操作的代码:$this->ci->load->config('smarty');将会调用到该配置文件

#这里的配置就是smarty里的配置

<?php
       if ( ! defined('BASEPATH')) exit('No direct script access allowed');
       $config['theme']             = 'default';
       $config['template_dir']      = APPPATH . 'views';
       $config['compile_dir']       = APPPATH . 'views/templates_c';
       $config['cache_dir']         = APPPATH . 'views/cache';
       $config['config_dir']        = APPPATH . 'views/configs';
       $config['caching']           = false;
       $config['lefttime']          = 60;
       $config['left_delimiter']    =     '<{';
       $config['right_delimiter']   =     '}>';        

3.   在CI里重载smarty的 assign 和 display方法

#cismarty  一定要全部小写,不然ci找不到。之前被坑在这里了

#assign  该函数里面要调用smarty类的assign方法才行

#display   该函数里面要调用smarty类的display方法才行

<?php
       if (!defined('BASEPATH')) exit('No direct access allowed.');
       class MY_Controller extends CI_Controller {
           public function __construct() {
               parent::__construct();
           }

           public function assign($key,$val) {
               $this->cismarty->assign($key,$val);
           }

           public function display($html) {
               $this->cismarty->display($html);
           }
       }

4.   修改Config文件下的autoload.php 自动加载类文件

#$autoload['libraries'] = array('cismarty ');  实现自动加载smarty类

$autoload['libraries'] = array('cismarty ');

5.   下面测试

a.   新建控制器admin_welcome.php

#assign  调用MY_Controller类里的assign方法,然后间接调用smarty的assign方法

#display  调用MY_Controller类里的display方法,然后间接调用smarty的display方法

<?php
class Welcome extends MY_Controller {
public function index(){ $data = "datadatadata"; $this->assign("data", $data); $this->display('test.tpl'); } }

Views 下新建test.tpl

#$data  控制器传来的值

<html>
       <head>
       </head>
       <body>
              aaaaaaaaaaaaaaaaaaaaa
              {$data}
       </body>
</html>



原文地址:https://www.cnblogs.com/sustudy/p/3910803.html