该方法直接将smarty库插入到codeigniter框架中,属于一种劳永逸的方法;
1.将smarty2.6.26包(其他版本的我没试过)下载,将包里的libs目录解压到框架系统目录(system)下的libraries目录下并改名为Smarty(便于辨识);
像这样:system\libraries\Smarty;
2.新建system\libraries\Smarty.php文件(首字母要大写);
将以下代码写入:
<?php if(!defined('BASEPATH')) EXIT('No direct script asscess allowed'); require_once( FCPATH.SYSDIR.'/libraries/Smarty/Smarty.class.php' ); class CI_Smarty extends Smarty{ protected $ci; public function __construct(){ $this->ci = & get_instance(); //相关的配置项 $this->template_dir = FCPATH."/smarty/templates";//模板目录 $this->compile_dir = FCPATH."/smarty/compiled";//模板编译目录 $this->cache_dir = FCPATH."/smarty/cache";//模板缓存目录 $this->config_dir = FCPATH."/smarty/config";//模板配置目录,可有可无 $this->template_ext = ".html"; $this->caching = false; $this->cache_lifetime = 60; } }
该代码中包含对smarty引擎的配置信息(亲测可以用),之后修改配置信息都在这里改;
3.按照你上面配置的信息新建以下目录:
站点根目录/smarty/templates
站点根目录/smarty/compiled
站点根目录/smarty/cache
站点根目录/smarty/config
4.在application/config/autoload.php中找到下面这个位置并写入该值:
$autoload['libraries'] = array("smarty");
5.在控制器(不用改控制器的继承)这样写调用smarty的方法:
$this->smarty->assign('addtion_path',base_url()."addtion/");//举个例子 $this->smarty->display('main.html');//举个例子