1、到相应站点下载Smarty的源码包;
2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty;
3、在项目目录的libraries文件夹内新建文件Cismarty.php,里面的内容如下:
01.
<?php
02.
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
03.
04.
require_once( APPPATH . 'libraries/Smarty/Smarty.class.php' );
05.
06.
class Cismarty extends Smarty {
07.
08.
protected $ci;
09.
10.
public function __construct(){
11.
12.
$this->ci = & get_instance();
13.
14.
$this->ci->load->config('smarty');//加载smarty的配置文件
15.
16.
//获取相关的配置项
17.
$this->template_dir = $this->ci->config->item('template_dir');
18.
$this->complie_dir = $this->ci->config->item('compile_dir');
19.
$this->cache_dir = $this->ci->config->item('cache_dir');
20.
$this->config_dir = $this->ci->config->item('config_dir');
21.
$this->template_ext = $this->ci->config->item('template_ext');
22.
$this->caching = $this->ci->config->item('caching');
23.
$this->cache_lifetime = $this->ci->config->item('lefttime');
24.
}
25.
26.
}
4、在项目目录的config文件夹内新建文件smarty.php文件,里面的内容如下:
01.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
02.
$config['theme'] = 'default';
03.
$config['template_dir'] = APPPATH . 'views';
04.
$config['compile_dir'] = FCPATH . 'templates_c';
05.
$config['cache_dir'] = FCPATH . 'cache';
06.
$config['config_dir'] = FCPATH . 'configs';
07.
$config['template_ext'] = '.html';
08.
$config['caching'] = false;
09.
$config['lefttime'] = 60;
5、在入口文件所在目录新建文件夹templates_c、cache、configs;
6、在项目目录下面的config目录中找到autoload.php文件
修改这项
1.
:$autoload['libraries'] = array('Cismarty');//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载
7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下:
01.
<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
02.
03.
class Controller extends CI_Controller {
04.
05.
public function __construct() {
06.
07.
parent::__construct();
08.
09.
}
10.
11.
public function assign($key,$val) {
12.
$this->cismarty->assign($key,$val);
13.
}
14.
15.
public function display($html) {
16.
$this->cismarty->display($html);
17.
}
18.
}
//================================================================
配置完毕
//================================================================
使用方法:
在控制器中如:
01.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
02.
03.
class Welcome extends Controller {
04.
05.
public function __construct(){
06.
parent::__construct();
07.
}
08.
09.
public function index()
10.
{
11.
$data['title'] = '测试';
12.
$data['test'] = '123456789';
13.
$this->assign('test',$data);
14.
$this->assign('tmp','hello');
15.
$this->display('test.html');
16.
}
17.
}
然后再视图中:试图文件夹位于项目目录的views之下:
新建文件test.html
01.
<!DOCTYPE html>
02.
<html>
03.
<head>
04.
<meta charset="utf-8">
05.
<title>{$test['title']}</title>
06.
07.
<style type="text/css">
08.
</style>
09.
</head>
10.
<body>
11.
12.
{$test['test']|md5}
13.
<br>
14.
{$tmp}
15.
123
16.
17.
</body>
18.
</html>

282

被折叠的 条评论
为什么被折叠?



