xingch 发表于 2010-8-19 21:02:51

CI集成Smarty

1, 把Smarty库放在ci库的libraries文件夹中,
2,在libraries里新建一个Smarty.php用来继承原生的Smarty类,代码如下:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Smarty/Smarty.class.php");
/**
* DK_Smarty extends Smarty
* Author: Daker.W
* Create Time: 2010/08/11
**/
class DK_Smarty extends Smarty
{
var $cache= 0;
var $debug= false;

function DK_Smarty($config = array())
{
   $this->CI =& get_instance();
   
   if ( ! in_array('smarty_lang'.EXT, $this->CI->lang->is_loaded, TRUE))
   {
    $this->CI->lang->load('smarty');
   }

   $this->local_time = time();
   $this->initialize($config);
   log_message('debug', "Smarty Class Initialized");
}

function initialize($config = array(cache=>0, debug=>false))
{
   foreach ($config as $key => $val)
   {
    if (isset($this->$key))
    {
   $this->$key = $val;
    }
   }
   $this->left_delimiter= '<!--{@';
   $this->right_delimiter= '@}-->';
   
   define('TPL_PATH', APPPATH.'templates'.DIRECTORY_SEPARATOR);
   $this->template_dir   = TPL_PATH;
   $this->compile_dir   = sprintf("%stemplates_c%s", APPPATH.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
   $this->config_dir   = sprintf("%sconfig%s", APPPATH.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
   $this->cache_dir   = sprintf("%scache%s", APPPATH.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
   if(!is_dir($this->compile_dir)){
    mkdir($this->compile_dir, 0777);
   }
   if($this->caching && !is_dir($this->cache_dir)){
    mkdir($this->cache_dir, 0777);
   }
}
}//End Class


最后目录结构如下
-------system
----------libraries
------------Smarty
------------Smarty.php
3,控制器中调用

class Testsmarty extends DK_Controller {

function DK_Testsmarty()
{
   parent::DK_Controller();
}

function index()
{
   $this->load->library('smarty');
   $this->smarty->assign('test', 'wanggang');
   $this->smarty->display('test/testsmarty.html');
}
}

zhanglei_0202 发表于 2010-8-20 16:53:00

贴个图出来就更好了

eason 发表于 2010-8-31 15:20:51

DK_Controller这个类哪来的

xingch 发表于 2010-9-1 15:22:19

回复 3# eason
自己扩展原生的Controller类, 你可以改为原生的类Controller

superkamiu 发表于 2010-9-4 13:28:05

搞不明白,为什么MVC模式了,还用模板技术有什么好处?高手指点!

xingch 发表于 2010-9-7 13:53:59

回复 5# superkamiu
smarty也有view的性质, 个人比较喜欢smarty我后台用ci的View觉的代码量有点大.所以现在用smarty.
这是看喜欢怎么的VIEW书写

visvoy 发表于 2010-9-7 23:05:10

smarty对美工比较友好,尤其纯美工

mab741 发表于 2010-9-26 09:54:53

$this->left_delimiter= '<!--{@';
   $this->right_delimiter= '@}-->';   这两行报错

xingch 发表于 2011-4-29 13:53:05

回复 8# mab741


    报什么错?贴出来。
    我一直使用,没有问题。

新流星雨 发表于 2012-7-23 14:58:20

看上去容易实现,得试试去。
页: [1] 2
查看完整版本: CI集成Smarty