使用Codeigniter与模块化扩展和Smarty

时间:2011-08-17 19:33:32

标签: codeigniter doctrine smarty

我正在研究Jasonayre bravenewignition系统,并希望添加这个项目Smarty。如何将HMVC系统和Smarty一起使用?

1 个答案:

答案 0 :(得分:1)

我几天前做过这件事,这是集成智能系统的一步。

  1. http://www.smarty.net/download
  2. 下载Smarty
  3. 将smarty文件夹放在application / third_party
  4. 在应用程序/库中创建smarty.php文件并粘贴此

    require_once(APPPATH.'third_party/Smarty/libs/Smarty.class.php');
    
    class CI_Smarty extends Smarty {
    
    protected $CI;
    private $module;
    
    public function __construct()
    {
        parent::__construct();
    
        // get CI istance
        $this->CI =& get_instance();
        // fetch the calling module, need to check for views inside it
        $this->module = $this->CI->router->fetch_module();
    
        // path
        $this->setCompileDir(APPPATH . "cache/smarty/compiled");
        $this->setCacheDir(APPPATH . "cache/smarty/cached");
        $this->setTemplateDir(APPPATH . "views/templates");
    
        // to use $this inside our views/template
        $this->assignByRef('this', $this->CI);
    
        log_message('debug', "Smarty Class Initialized");
    }
    
    public function display ($template, $cache_id = null, $compile_id = null, $parent = null) {
        // automatically add .tpl extension if there is not in $template
        if (strpos($template,'.') === FALSE) {
            $template .= '.tpl';
        }
        if ( !empty($this->module)){
            $template = APPPATH . 'modules/' . $this->module . '/views/' . $template;
        }
        // call the original smarty function
        parent::display($template, $cache_id, $compile_id, $parent);
    }
    }
    
    /* End of file Smarty.php */
    /* Location: ./application/libraries/Smarty.php */
    
  5. ps:抱歉缩进

    现在你可以使用smarty和$ this-> load-> library('smarty')或自动加载它在配置文件中添加smarty到$ autoload。

    希望这有帮助! ^^