|
接触CI没多久,,搜了下网上挂载smarty的方法只有一种,我就边查手册边搞出了一个新的方法,,请老鸟指正:原理是利用CI的创建核心库功能:
在application/libraries/ 下建立 MY_Controller.php (ci默认建立核心库以MY_ 开头,,但也可以在配置文件中更改) :
PHP复制代码
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require "Smarty/Smarty.class.php";
require "Urlmanager.php";
class MY_Controller extends Controller
{
protected $smarty = null;
public function __construct () {
parent ::Controller();
if (null == $this->smarty) {
$this->smarty = new Smarty ();
$config =& get_config ();
$this->smarty->template_dir = (!empty($config['smarty_template_dir']) ? $config['smarty_template_dir'] : BASEPATH . 'application/views/');
$this->smarty->compile_dir = (!empty($config['smarty_compile_dir']) ? $config['smarty_compile_dir'] : BASEPATH . 'cache/tpl_compile/');
$this->smarty->caching = !empty($config['smarty_caching']) ? $config['smarty_caching'] : false;
$this->smarty->cache_dir = !empty($config['smarty_cache_dir']) ? $config['smarty_cache_dir'] : BASEPATH . 'cache/tpl_cache/';
//use CI's cache folder
if (function_exists('site_url')) {
// URL helper required
$this->assign("site_url", site_url ()); // so we can get the full path to CI easily
}
}
$urlmanager = new Urlmanager ();
$this->smarty->register_object('url', $urlmanager);
$exectime = $this->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end');
$this->smarty->assign('exectime', $exectime);
}
复制代码
使用方法:index 控制器;
PHP复制代码
class Index extends MY_Controller
{
public function __construct() {
parent::__construct();
}
public function index() {
$this->smarty->display('welcome_message.html');
}
}
复制代码
所有的控制器 继承自 MY_Controller.php 在这里可以初始化 smarty,,当然还可以做一些其他你希望初始化的东东 .... |
评分
-
查看全部评分
|