php ci 模板引擎,CI 框架整合 Smarty 模版引擎

该文档详细介绍了在MAC环境下,使用XAMPP搭建CI3.x框架,并整合Smarty3.x模板引擎的步骤。包括下载CI和Smarty,配置文件夹、配置文件,创建自定义控制器和模板文件,最后通过测试确保整合成功。
摘要由CSDN通过智能技术生成

(本地开发环境下进行: CI 3.x + Smarty 3.x)

本地开发环境准备说明

此处不做过多的说明,你可以下载一个本地集成开发环境(如:XAMPP 或者 wampserver)安装使用;本文档是基于 MAC、 XAMPP 环境进行介绍。

一、搭建CI框架环境

1、下载 CI 3.x 版本

2、解压、拷贝、初始访问

解压文件,并将其中的文件夹 application、 system、 和文件 index.php 拷贝到你的web根目录下,然后就可以使用CI框架了。至于其他的文件夹和文件,根据你的心情选择是否要一起拷贝。

b545d9274292

copy_floder_file.png

然后访问你的web进行测试:http://localhost/xxx/ ,如果得到如下画面,表示CI框架环境搭建成功。

b545d9274292

ci_welcome.png

二、整合 Smarty 模版引擎

1、下载 Smarty 3.x 版本

2、解压

3、在 application/third_party/ 文件夹下创建文件夹 smarty-3.1.30 ,并将解压好的Smarty库中的libs文件夹复制到 smarty-3.1.30 文件夹中

4、在application/config下创建smarty.php(其中的路径和定界符均可以自由定义使用),代码如下:

if (!defined('BASEPATH')) exit('No direct script access allowed');

$config['cache_lifetime'] = 60;

$config['caching'] = false;

$config['template_dir'] = APPPATH . 'views';

$config['compile_dir'] = APPPATH . 'views/template_c';

$config['cache_dir'] = APPPATH . 'views/cache';

$config['config_dir'] = APPPATH . 'views/config';

$config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录)

$config['left_delimiter'] = '

$config['right_delimiter'] = '}>';

5、在application/libraries下创建一个Ci_smarty.php(注意:文件名可以随意保存,但接下来的步骤会用到此文件,所以请区分大小写),代码如下:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH .'third_party/smarty-3.1.30/libs/SmartyBC.class.php');

class Ci_smarty extends SmartyBC {

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');

}

}

6、在application/core下新建一个MY_Controller.php ,代码如下:

class MY_Controller extends CI_Controller {

public function __construct() {

parent::__construct();

$this->load->library("Ci_smarty");

}

public function assign($key, $val) {

$this->ci_smarty->assign($key, $val);

}

public function display($html) {

$this->ci_smarty->display($html);

}

}

7、测试 Smarty 的整合

在 application/controller/ 目录下创建一个控制器文件 Test.php, 代码示例如下:

/**

* Created by PhpStorm.

* User: joker

* Date: 2017/11/12

* Time: 15:15

*/

class Test extends MY_Controller

{

public function __construct(){

parent::__construct();

}

public function index(){

$data = [];

$data['describe'] = '测试CI框架整合Smarty模版引擎!!!';

$this->assign('data', $data);

$this->display(APPPATH . 'views/templates/test.tpl');

}

}

在 application/views/templates/ 目录下创建一个控制器文件 test.tpl, 代码示例如下:

测试smarty整合

在 application/config/routes.php 文件中添加控制器的访问路由,则可以进行访问测试;如果得到如下的界面,则整合成功:

b545d9274292

ci_smarty_success.png

至此,CI框架整合Smarty模版引擎完毕。

【如若文档有错误,欢迎大家不吝赐教。本文档是集网上各位大神的资源进行整合的,具体资源来源已经忘记了,如果发现有侵权等行为,请联系我,我将对应处理,谢谢~~~】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值