ci版本:2.1.4 // 此时的最新版本
smarty版本:smarty-2.6.26 // 因为我之前用这个版本,为了照顾自己的使用习惯,这里没有使用最新的smaty版本,大家理解了扩展原理,可以选择自己想用的smatry版本。
1、到相应站点下载smarty的源码包; // 我这里用的是 smarty-2.6.26
2、将源码包里面的libs文件夹copy到ci的项目目录下面的libraries文件夹下,并重命名为smarty-2.6.26;//
3、在项目目录的libraries文件夹内新建文件cismarty.php,里面的内容如下:
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文件,里面的内容如下:
5、在入口文件所在目录新建文件夹templates_c、cache、configs;
6、在项目目录下面的config目录中找到autoload.php文件
修改这项
$autoload['libraries'] = array('Cismarty');//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载
7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下: // 扩展核心控制类cismarty->assign($key,$val); } public function display($html) { $this->cismarty->display($html); } }配置完毕
------------------------------------------------------------------------------------------------------------------------------------------------------
使用方法实例:
在控制器中如:load->view('welcome_message'); $data['title'] = '标题'; $data['num'] = '123456789'; //$this->cismarty->assign('data',$data); // 亦可 $this->assign('data',$data); $this->assign('tmp','hello'); //$this->cismarty->display('test.html'); // 亦可 $this->display('test.html'); } }然后再视图中:试图文件夹位于项目目录的views之下:
新建文件test.html
UQ云商B2B2C系统下载UQCMS云商是一款B2B2C电子商务软件 ,非常适合初创的创业者,个人及中小型企业。程序采用PHP+MYSQL,模板采用smarty模板,二次开发,简单方便,无需学习其他框架就可以自行模板设计。永久免费使用,操作简单,安全稳定。支持PC+WAP+微信三种浏览方式,支持微信公众号。
{ $test.title} // 原文是{$test['title']} ,是错误的写法,也有可能是Smarty版本的原因 {$test.num|md5} // 原文这里也写错了
{$tmp}本文地址:http://www.php.cn/php-weizijiaocheng-377484.html
学编程就来 PHP中文网 www.php.cn










