1、到相应站点下载Smarty的源码包;
2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty;
3、在项目目录的libraries文件夹内新建文件Cismarty.php,里面的内容如下:
<?php
if(!defined(‘BASEPATH’)) EXIT(‘No direct script asscess allowed’);require_once( APPPATH . ‘libraries/Smarty/Smarty.class.php’ );
class Cismarty extends Smarty {
protected $ci;
public function __construct(){
$this->ci = & get_instance();
$this->ci->load->config(‘smarty’);//加载smarty的配置文件
//获取相关的配置项
$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->template_ext = $this->ci->config->item(‘template_ext’);
$this->caching = $this->ci->config->item(‘caching’);
$this->cache_lifetime = $this->ci->config->item(‘lefttime’);
}}
4、在项目目录的config文件夹内新建文件smarty.php文件,里面的内容如下:
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
$config[‘theme’] = ‘default’;
$config[‘template_dir’] = APPPATH . ‘views’;
$config[‘compile_dir’] = FCPATH . ‘templates_c’;
$config[‘cache_dir’] = FCPATH . ‘cache’;
$config[‘config_dir’] = FCPATH . ‘configs’;
$config[‘template_ext’] = ‘.html’;
$config[‘caching’] = false;
$config[‘lefttime’] = 60;
5、在入口文件所在目录新建文件夹templates_c、cache、configs;
6、在项目目录下面的config目录中找到autoload.php文件
修改这项
$autoload[‘libraries’] = array(‘Cismarty’);//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载
7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下:
<?php if (!defined(‘BASEPATH’)) exit(‘No direct access allowed.’);
class 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);
}
}
//================================================================
配置完毕
//================================================================
使用方法:
在控制器中如:
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
class Welcome extends Controller {
public function __construct(){
parent::__construct();
}public function index()
{
$data[‘title’] = ‘测试’;
$data[‘test’] = ‘123456789’;
$this->assign(‘test’,$data);
$this->assign(‘tmp’,’hello’);
$this->display(‘test.html’);
}
}
然后再视图中:试图文件夹位于项目目录的views之下:
新建文件test.html
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>{$test[‘title’]}</title><style type=”text/css”>
</style>
</head>
<body>{$test[‘test’]|md5}
<br>
{$tmp}
123</body>
</html>
- 本文固定链接: https://www.cnmiss.cn/?p=261
- 转载请注明: admin 于 我的博客 – 技术分享 发表
《CodeIgniter 2.02 中配置 smarty》有 1 条评论