[CodeIgniter][Smarty]CI配置smarty, http://blog.xhbin.com/archives/987 |
[CodeIgniter][Smarty]CI配置smarty, http://blog.xhbin.com/archives/987 |
2011-11-28 12:46:16, Mon
Post
#1
|
|
猫猫猫 Group: Power Cat Posts: 626 Joined: 2006-12-8 Member No.: 2 |
1、到相应站点下载Smarty的源码包;
2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty; 3、在项目目录的libraries文件夹内新建文件cismarty.php,里面的内容如下: 代码 <?php if(!defined('BASEPATH')) EXIT('No direct script asscess allowed'); ?> <?php define('SMARTY_DIR', APPPATH . 'libraries/Smarty/'); require_once( SMARTY_DIR . 'Smarty.class.php' ); class Cismarty { private $ci; private $smarty; function __construct() { $this->ci = & get_instance(); $this->ci->load->config('cismarty');//load Smarty config file in CI $this->smarty = new Smarty; $this->smarty->template_dir = APPPATH . 'views'; $this->smarty->compile_dir = APPPATH . 'compile'; $this->smarty->config_dir = APPPATH . 'config'; $this->smarty->cache_dir = APPPATH . 'cache'; //set Smarty config options $this->smarty->template_dir = $this->ci->config->item('template_dir'); $this->smarty->compile_dir = $this->ci->config->item('compile_dir'); $this->smarty->cache_dir = $this->ci->config->item('cache_dir'); $this->smarty->config_dir = $this->ci->config->item('config_dir'); $this->smarty->caching = $this->ci->config->item('caching'); $this->smarty->cache_lifetime = $this->ci->config->item('lefttime'); } function __destruct() { if (isset($this->smarty)) { unset($this->smarty); } } public function assign(&$key,&$val) { $this->smarty->assign(&$key,&$val); } public function display(&$html) { $this->smarty->display(&$html); } } ?> 4、在项目目录的config文件夹内新建文件cismarty.php文件,里面的内容如下: 代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?> <?php $config['theme'] = 'default'; $config['template_dir'] = APPPATH . 'views'; $config['config_dir'] = APPPATH . 'config'; $config['compile_dir'] = APPPATH . 'compile'; $config['cache_dir'] = APPPATH . 'cache'; $config['caching'] = false; $config['lefttime'] = 60; ?> 5、在项目目录新建文件夹 compile,并将 cache 和 compile 目录设置为 php 可写的权限; 6、在项目目录下面的config目录中找到autoload.php文件 修改这项 代码 :$autoload['libraries'] = array('cismarty');//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载 7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下: 代码 <?php if (!defined('BASEPATH')) exit('No direct access allowed.'); ?> <?php 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); } } ?> //================================================================ 配置完毕 //================================================================ 使用方法: 在控制器目录中添加 test.php: 代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?> <?php class Test 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> |
|
|
Lo-Fi Version | Time is now: 2024-10-31 19:38 |