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');
}
}
贴个图出来就更好了 DK_Controller这个类哪来的 回复 3# eason
自己扩展原生的Controller类, 你可以改为原生的类Controller 搞不明白,为什么MVC模式了,还用模板技术有什么好处?高手指点! 回复 5# superkamiu
smarty也有view的性质, 个人比较喜欢smarty我后台用ci的View觉的代码量有点大.所以现在用smarty.
这是看喜欢怎么的VIEW书写 smarty对美工比较友好,尤其纯美工 $this->left_delimiter= '<!--{@';
$this->right_delimiter= '@}-->'; 这两行报错 回复 8# mab741
报什么错?贴出来。
我一直使用,没有问题。 看上去容易实现,得试试去。
页:
[1]
2