将smarty模版引擎整合到CI框架中。
- 下载:ci,smarty
- 配署ci 在这里就不多说了……
- 1. 将下载好的smarty包的lib文件上传到ci中的application/libraries 文件中,将取名称修改为smarty,在libraries文件新建cismarty.php文件,内容如下:
-
- if (!defined(‘BASEPATH‘)) exit("no direct script access allowd");
- require_once(APPPATH.‘libraries/smarty/Smarty.class.php‘);
- class cismarty extends Smarty{
- protected $ci;
- function __construct(){
- parent::__construct();
- $this->ci = & get_instance();
- $this->ci->load->config(‘smarty‘);
- $this->cache_lifetime = $this->ci->config->item(‘cache_lifetime‘);
- $this->caching = $this->ci->config->item(‘caching‘);
- $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(‘cache_dir‘);
- $this->use_sub_dirs = $this->ci->config->item(‘use_sub_dirs‘);
- $this->left_delimiter = $this->ci->config->item(‘left_delimiter‘);
- $this->right_delimiter = $this->ci->config->item(‘right_delimiter‘);
- }
- }
- ?>
- 2. 在config下新建smarty.php配置文件
- <?php
- if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);
- $config[‘cache_lifetime‘] = 30*24*3600;
- $config[‘caching‘] = false;
- $config[‘template_dir‘] = APPPATH.‘views‘;
- $config[‘compile_dir‘] = APPPATH.‘views/template_c‘;
- $config[‘cache_dir‘] = APPPATH.‘views/cache‘;
- $config[‘use_sub_dirs‘] = true;
- $config[‘left_delimiter‘] = ‘<{‘;
- $config[‘right_delimiter‘] = ‘}>‘;
- ?>
- 3. 在CI里重载smarty的 assign 和 display方法
- 在框架根目录下core/目录下新建控制器继承CI基类,MY_Controller
- <?php
- if (!defined(‘BASEPATH‘)) exit(‘No direct access allowed.‘);
- class MY_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);
- }
- }
- 4. 修改Config文件下的autoload.php 自动加载类文件
- $autoload[‘libraries‘] = array(‘cismarty‘);
- 到此配置已完成.
- 5. 下面测试
- a. 新建控制器admin_welcome.php
- class Admin_welcome extends MY_Controller{
- function __construct(){
- parent::__construct();
- }
- public function index(){
- $data[‘title‘] = ‘标题‘;
- $data[‘num‘] = ‘123456789‘;
- $this->cismarty->assign(‘data‘,$data);
- $this->cismarty->display(‘test.html‘);
- }
- }
- Views 下新建test.html
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>smarty配置测试</title>
- </head>
- <body>
- <{$data.title}>
- </body>
- </html>
将smarty模版引擎整合到CI框架中
原文:http://www.cnblogs.com/doone/p/4220659.html