在codeigniter框架的开发使用中,为了更方便的使用CI框架的MVC结构,在前端的页面渲染中可以引入强大的smarty模板引擎,smarty模板引擎相对于ci框架自带的模板引擎更加强大,应用起来也更加灵活,比如对于数组的处理更加方便使用等。
由于ci3框架并不自带smarty模板引擎,所以我们需要将其手动集成到ci框架中,操作步骤如下:
修改涉及到的文件概览
- 新增Smarty文件夹
- 新增smarty.php文件
- 新增MY_Smarty.php文件
- 新增或修改MY_Controller.php文件
- 修改autoload.php文件
- 新增templates文件夹,用于存放smarty模板
- 修改welcome.php控制器文件,用于测试smarty模板引擎是否安装成功
具体操作步骤
- 下载ci框架并搭建起初始化的网站。下载地址:https://codeigniter.org.cn/
- 下载smarty引擎。下载地址:https://www.smarty.net/
- 首先将下载的smarty-3.1.33.zip文件解压
- 在ci框架的application/libraries目录下新建Smarty文件夹
- 将smarty-3.1.33/libs目录下的所有文件复制到新建的Smarty文件夹中
在application/libraries目录下新建MY_Smarty.php,代码如下:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * 将smarty3.1.33整合到ci框架 * Date: 2019/07/06 * Time: 19:50 * Description: Smarty.php */ require(APPPATH.'libraries/Smarty/Smarty.class.php'); class MY_Smarty extends Smarty { protected $ci; public function __construct() { parent::__construct(); $this->ci =& get_instance(); $this->ci->load->config('smarty'); //加载smarty的配置文件 $this->cache_lifetime = $this->ci->config->item('cache_lifetime'); $this->caching = $this->ci->config->item('caching'); $this->config_dir = $this->ci->config->item('config_dir'); $this->template_dir = $this->ci->config->item('template_dir'); $this->compile_dir = $this->ci->config->item('compile_dir'); $this->cache_dir = $this->ci->config->item('cache_dir'); $this->use_sub_dirs = $this->ci->config->item('use_sub_dirs'); $this->left_delimiter = $this->ci->config->item('left_delimiter'); $this->right_delimiter = $this->ci->config->item('right_delimiter'); } }
在application/config文件夹中新建smarty.php,代码如下:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * Date: 2019/07/06 * Time: 20:10 * Description: smarty.php */ $config['cache_lifetime'] = 60; $config['caching'] = false; $config['template_dir'] = APPPATH . 'views/templates'; $config['compile_dir'] = APPPATH . 'views/templates_c'; $config['cache_dir'] = APPPATH . 'views/cache'; $config['config_dir'] = APPPATH . 'views/config'; $config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录) $config['left_delimiter'] = '{'; $config['right_delimiter'] = '}';
在application/core目录下新建MY_Controller.php文件,代码如下:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * Date: 2019/07/06 * Time: 19:00 * Description: MY_Controller.php */ class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); } public function assign($key, $val) { $this->my_smarty->assign($key, $val); } public function display($html) { $this->my_smarty->display($html); } }
修改application/config/autoload.php文件中的
$autoload['libraries']
参数$autoload['libraries'] = array('MY_Smarty');
修改默认的Welcome.php控制器,将index方法修改如下:
$check='ci 3.1.10 + smarty 3.1.33 配置成功!'; $this->assign('check',$check); $this->display('index.html');
在application/views/templates下新建index.html模板文件,代码如下:
{$check}
smarty集成后的检验
此时可以在浏览器中访问搭建的网站网址,当出现ci 3.1.10 + smarty 3.1.33 配置成功!
字样并没有任何报错信息时,则代表smarty模板引擎已经成功集成到了ci框架中!
版权属于:奇妙数据
本文链接:https://blog.mioshu.com/archives/482.html
转载声明:本文为原创内容,著作权归 奇妙数据 所有,转载时请注明出处!