php smart模板,vaphp整合smart模板有关问题
vaphp整合smart模板問題
目前正在做一個小項目,選擇Ci框架也是第一次使用,發現CI沒有內置的模版引擎,傳統的php、html混寫有點不習慣咯被慣壞了,決定將smarty模版引擎整合到CI框架中。
步驟如下:
下載:ci,smarty
配署ci?在這里就不多說了……
1.??將下載好的smarty包的lib文件上傳到ci中的libraries?文件中,將取名稱修改為smarty,在libraries文件新建cismarty.php文件,內容如下:
if?(!defined('BASEPATH'))?exit("no?direct?script?access?allowd");
//以下是加載smarty的類文件
require_once(APPPATH.'libraries/smarty/Smarty.class.php');
//定義cismarty類,繼承smarty類
class?cismarty?extends?Smarty{
//定義一個受保護的變量,
protected?$ci;
function?__construct(){
parent::__construct();
//引用實例化CI,這里主要是將smarty的配置文件寫到ci中,以方便程序管理
$this->ci?=?&?get_instance();
//加載ci的新建的smarty配置文件
$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配置文件
$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
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');
到此配置已完成.
第3步要在core文件夾下建MY_Controller,但是vsphp創建的CI框架里面沒有這個文件夾,我自己建了個core文件夾在里面創建了MY_Controller。運行的時候報錯找不到MY_Controller類。然后我在用到這個類的地方引用這個文件就好了。但是這樣太麻煩了每個文件都要引用。有沒有別的處理方法。
------解決方案--------------------
查找包含?__autoload?或?spl_autoload_register?字樣的文件
修改相關代碼
------解決方案--------------------
沒定義一個類?你讓他繼承這個MY_Controller類就可以了啊
------解決方案--------------------
在index文件中?默認就加載這個文件
總結
以上是生活随笔為你收集整理的php smart模板,vaphp整合smart模板有关问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php搜索文件名,php实现按文件名搜索
- 下一篇: tp5index.php怎么放外面,tp