codeigniter是一个比较不错的轻量级的php开发web frame,我将ci结合smarty模板解释器使用,现在ci配置smarty的方法给出:
①下载smarty的源码包并且解压
②将smarty源码包中的libs核心类库重命名为Smarty并且经文件夹copy到ci的项目目录下的libraries下,同时在libraries文件夹下建立Cismarty.php文件。
③在Cismarty.php文件中写入如下的code
<?php
if(!defined('BASEPATH'))exit('no dir');
require_once (APPPATH.'libraries/Smarty/Smarty.class.php');
class Cismarty extends Smarty
{
protected $ci;
function __construct()
{
parent::__construct();
$this->ci = &get_instance();
$this->template_dir = $this->ci->config->item('template_dir');
$this->compile_dir = $this->ci->config->item('compile_dir');
$this->cache_dir = $this->ci->config->item('caching_dir');
$this->config_dir = $this->ci->config->item('config_dir');
$this->template_ext = $this->ci->config->item('template_ext');
$this->caching = $this->ci->config->item('caching');
$this->cache_lifetime = $this->ci->config->item('cache_lifetime');
}
}
④在项目目录下的cofig文件夹建立smarty.php文件,并且写入如下代码:
<?php
if (! defined('BASEPATH')) exit('no direct base');
$config['template_dir'] = APPPATH.'views';
$config['compile_dir'] = FCPATH.'template_c';
$config['caching_dir'] = FCPATH.'cache';
$config['config_dir'] = FCPATH.'config';
$config['caching'] = false;
$config['cache_lifetime'] = '60';
$config['template_ext'] = '.html';
⑤在项目目录下的config文件夹中的autoload.php文件中写入以下代码:
$autoload['config'] = array('smarty');$autoload['libraries'] = array('cismarty');
至此配置完毕,以下给出简单的测试程序
class Start extends CI_Controller { function __construct() { parent::__construct(); $this->load->database(); // $this->load->library('cismarty'); } function smarty() { $title = 'title'; $this->cismarty->assign('title',$title); $this->cismarty->display("test.html"); } }
现在自己在application下的views下建立一个test.html文件,传一个数据过去,如果配置正确那么可以看到测试页面和数据。