Codelgniter 3.1.0 集成 Smarty-3.1.8 完美配置

    xiaoxiao2025-11-16  3

    下载 Codelgniter 3.1.0

    下载 Smarty-3.1.8

     

    1、把下载的Smarty-3.1.8 文件中的libs改名为Smarty,放到ci3.1/application/libraries/

    2、在ci3.1/application/libraries/目录下新建 CI_Smarty.php  内容如下:

     

    <?php defined('BASEPATH') OR exit('No direct script access allowed'); // 加载Smarty require_once APPPATH.'libraries/Smarty/Smarty.class.php'; /** * CI_Smarty 自定义属于CI的Smarty * smarty版本 3.1.8 * @author */ class CI_Smarty extends Smarty { protected $ci; protected $complie_dir; protected $template_ext; public function __construct(){ parent::__construct(); // 获得CI超级对象 使得自定义类可以使用Controller类的方法 $this->ci = & get_instance(); // 加载smarty的配置文件 $this->ci->load->config('smarty'); // 获取相关的配置项 // 模板文件存放目录 $this->template_dir = $this->ci->config->item('template_dir'); // 编译文件存放目录 $this->complie_dir = $this->ci->config->item('compile_dir'); // 缓存文件存放目录 $this->cache_dir = $this->ci->config->item('cache_dir'); // Smarty配置目录 $this->config_dir = $this->ci->config->item('config_dir'); // 模板文件后缀 $this->template_ext = $this->ci->config->item('template_ext'); // 缓存开关 $this->caching = $this->ci->config->item('caching'); // 失效时间 $this->cache_lifetime = $this->ci->config->item('lefttime'); // 左边界 左定界符 $this->left_delimiter = '{{'; // 右边界 右定界符 $this->right_delimiter = '}}'; /* if(function_exists('site_url')) { // URL helper required $this->assign('site_url', site_url()); // so we can get the full path to CI easily } $this->assign('elapsed_time', $this->ci->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end')); $this->assign('memory_usage', ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2) . 'MB'); */ } }

    3、在ci3.1/application/config/目录下新建smarty.php   内容如下:

     

     

    <?php defined('BASEPATH') OR exit('No direct script access allowed'); // 模板主题配置 $config['theme'] = 'default'; // 模板文件存放路径 $config['template_dir'] = APPPATH.'views/'; // 编译文件存放路径 $config['compile_dir'] = FCPATH.'templates_c/'; // 缓存文件存放路径 $config['cache_dir'] = FCPATH.'cache/'; // 配置文件存放路径 $config['config_dir'] = FCPATH.'configs/'; // 设置模板文件后缀 $config['template_ext'] = '.html'; // 缓存开关 $config['caching'] = false; // 编译文件失效时间 $config['lefttime'] = 60;

    4、在ci3.1/目录下 新建template_c、cache、configs 三个文件夹

     

    5、在ci3.1/application/config/autoload.php 找到 $autoload['libraries'],并设置为:$autoload['libraries'] = array('CI_Smarty', 'database', 'session');

    6、在ci3.1/application/core/目录下  新建MY_Controller.php   内容如下:

     

    <?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * MY_Controller * 扩展控制器核心类 * 简化变量分配和模板加载 */ class MY_Controller extends CI_Controller { public function __construct(){ parent::__construct(); } /** * 给CI_Controller扩展Smarty的方法 * assign 分配变量 */ protected function assign($key,$val) { $this->ci_smarty->assign($key,$val); } /** * 给CI_Controller扩展Smarty的方法 * display 加载模板 */ protected function display($html) { // 检查模板目录是否存在 可有可无 if ( ! file_exists(VIEWPATH.$html)) { // Whoops, we don't have a page for that! $this->jumpNoticePage('模板文件不存在!', $_SERVER['HTTP_REFERER'], 'ERROR'); } $this->ci_smarty->display($html); } }

    可能会出现session路径问题。只要在config.php文件中找到$config['sess_save_path'] = APPPATH.'sessions/';  设置好路径即可!

     

     

    转载请注明原文地址: https://ju.6miu.com/read-1304264.html
    最新回复(0)