首先複制一份smarty源檔案到application/libraries目錄下。 自定義smarty配置檔案友善修改:config/cismarty.php
--------------------------------------------------------------------------------
// The name of the directory where templates are located. $config['template_dir'] = APPPATH . 'views/';
// The directory where compiled templates are located //注意這裡要手動建立檔案 $config['compile_dir'] = APPPATH . 'cache/compile/';
//This tells Smarty whether or not to cache the output of the templates to the $cache_dir. $config['caching'] = 0;
// This forces Smarty to (re)compile templates on every invocation. When deploying, change this value to 0 $config['force_compile'] = 1; $config['compile_check'] = TRUE;
// The delimiter for smarty $config['left_delimiter'] = '{%'; $config['right_delimiter'] = '%}'; 以上都是smarty一些常量 然後建立類 application/libraries/CI_smarty.php
--------------------------------------------------------------------------------
<?php
require_once(APPPATH.'libraries/smarty/Smarty.class.php');
class CI_smarty extends Smarty{
private $config;
function CI_smarty(){
parent::Smarty();
require_once(APPPATH.'config/cismarty.php');
$this->config = $config;//$config 是cismarty.php中數組變量名
if(count($this->config)>0)
{
$this->initialize($this->config);
}
}
function initialize($config = array()) {
foreach ($config as $key => $val) {
if (isset($this->$key)) {
//這裡是根據自己需要擴充一些set方法
$method = 'set_'.$key;
if (method_exists($this, $method)) {
$this->$method($val);
} else {
//修改smarty源檔案預設變量,這裡是自定義cismarty數組值
$this->$key = $val;
}
}
} } } ?>
--------------------------------------------------------------------------------
然後在application/config/autoload.php 找$autoload[’libraries’] = array(’CI_smarty’); CI_smarty對應上面類。
--------------------------------------------------------------------------------
測試: 控制器中使用
$val = 'This is ci_smarty test';
$this->ci_smarty->assign_by_ref('val',$val);
$this->ci_smarty->display('test.html');
轉自:http://hi.baidu.com/pcbbt/item/2acf7b2b922a4e84af48f547
轉載于:https://www.cnblogs.com/longailili/archive/2012/09/26/2703704.html