大碗茶 发表于 2009-6-14 21:48:13

第一次接触CI,发现一个挂载smarty的新方法

接触CI没多久,,搜了下网上挂载smarty的方法只有一种,我就边查手册边搞出了一个新的方法,,请老鸟指正:原理是利用CI的创建核心库功能:
在application/libraries/ 下建立 MY_Controller.php (ci默认建立核心库以MY_ 开头,,但也可以在配置文件中更改) :


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 控制器;

class Index extends MY_Controller
{
    public function __construct() {
      parent::__construct();
    }

    public function index() {
      $this->smarty->display('welcome_message.html');
    }
}


所有的控制器 继承自MY_Controller.php在这里可以初始化 smarty,,当然还可以做一些其他你希望初始化的东东 ....

Hex 发表于 2009-6-15 10:25:53

哈哈,这个方法好!

大碗茶 发表于 2009-6-15 12:30:10

嘿嘿,原创的哦,,网上如有雷同,纯属巧合....

yslubin 发表于 2009-8-9 16:41:40

require "Urlmanager.php";这个文件在哪?

CIwlhgbs2007 发表于 2009-8-11 23:59:08

:victory::victory:

redpower8 发表于 2009-9-26 22:37:06

Urlmanager.php";这个文件在哪?

zhenphp 发表于 2011-2-16 17:52:55

包含文件在哪里?擦
页: [1]
查看完整版本: 第一次接触CI,发现一个挂载smarty的新方法