美文网首页
CI框架(CI3.0.6)完美整合Smarty模板引擎(Smar

CI框架(CI3.0.6)完美整合Smarty模板引擎(Smar

作者: fizzday | 来源:发表于2016-04-24 12:35 被阅读986次

配置

1. 下载ci框架和smarty, 创建项目目录

# /var/www 为web根目录
$ cd ~/download
$ wget https://codeload.github.com/bcit-ci/CodeIgniter/zip/3.0.6
$ wget https://codeload.github.com/smarty-php/smarty/zip/v3.1.29
$ unzip CodeIgniter-3.0.6.zip
$ unzip smarty-3.1.29.zip
$ mv CodeIgniter-3.0.6 /var/www/ci_smarty
$ mv smarty-3.1.29/libs /var/www/ci_smarty/application/libraries/smarty3.1.29

2. 在 /var/www/ci_smarty/application/libraries 目录下创建 Ci_smarty.php 文件,代码如下:

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

require(APPPATH.'libraries/smarty3.1.29/Smarty.class.php');
class Ci_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');
    }
}

3. 在 /var/www/ci_smarty/application/config 目录下创建 smarty.php 文件,代码如下:

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

$config['cache_lifetime'] = 60;
$config['caching'] = true;
$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'] = '}';

4. 在 /var/www/ci_smarty/application/core 目录下创建 MY_Controller.php 文件,代码如下:

<?php
class MY_controller extends CI_Controller
{
    public function __construct()
    {

        date_default_timezone_set("PRC");

        parent::__construct();

    }

    public function assign($key,$val)
    {
        $this->ci_smarty->assign($key,$val);
    }

    public function display($html)
    {
        $this->ci_smarty->display($html);
    }
}

此时, 所有的配置工作已经完成, 接下来看如何使用:

使用方法

1. 加载smarty类

有两种方法, 如果随时会用到, 推荐使用自动加载办法:
1). 在 ci框架的配置目录 config 下的 autoload.php 中自动加载类增加 smarty 类, 相关部分代码如下:

$autoload['libraries'] = array('ci_smarty');

如果只想在用到的时候加载, 则在对应的控制器的初始化方法中加载, 或者在对应的控制器的对应的方法中加载:
2). 直接执行 ci 的加载类方法:

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

2. 使用smarty

1). 在ci默认控制器 Welcome.php 中, 修改 index 方法如下:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends MY_Controller  {

    public function index()
    {
        $test='ci 3.0.6 结合 smarty 3.1.2 配置成功';
        $this->assign('data',$test);
        $this->display('test.html');
    }
}

2). 在 views 目录, 创建 test.html 文件, 代码如下:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>smarty结合ci使用测试</title>
    </head>
    <body>
    {$data}
    </body>
</html>

3). 浏览器访问: localhost/ci_smarty, 输出:
ci 3.0.6 结合 smarty 3.1.2 配置成功


notes: 运行之后, 会在views目录生成一个编译目录template_c, 如果在配置文件中开启了cache缓存, 则会生成对应的cache目录, 具体的目录, 皆可在config/smarty.php中配置

开启smarty分配数据时的 debug

1./php/phplib/ext/smarty/
2.打开默认smarty的debug. 修改当前smarty目录 Smarty.class.php; 修改 360行, public $debugging_ctrl = 'URL'; 此处默认为 "NONE";
3.需要将debug.tpl发送到 smarty目录
URL?SMARTY_DEBUG 可以打印数据

可以弹出窗口显示分配的数据,有array() 和 json 两种 格式


更改的部分托管于github, 地址为fizzday: https://github.com/fizzday/ci_smarty
参考地址: 吐蕃赞普的新浪博客
大功告成, 完美手工~~~

相关文章

  • CI框架(CI3.0.6)完美整合Smarty模板引擎(Smar

    配置 1. 下载ci框架和smarty, 创建项目目录 2. 在 /var/www/ci_smarty/appli...

  • CI 框架整合 Smarty 模版引擎

    (本地开发环境下进行: CI 3.x + Smarty 3.x) 本地开发环境准备说明 此处不做过多的说明,你可以...

  • 2018-05-11Smarty模板引擎

    Smarty(模板引擎) 一、什么是模板引擎? Smarty是一个php模板引擎。更准确的说,它分开了逻辑程序和外...

  • Smarty模板

    PHP模板引擎 该文档使用的是Smarty 3.1.33 Released版本 什么是PHP模板引擎? php模板...

  • Smarty 模板函数

    最近工作中用到 Smarty 模板引擎,整理了一些用到的模板函数。 假设 smarty 的定界符为 {}。 模板中...

  • 关于fis框架中fis3-smarty语法总结(一)

    目录 什么是smarty fis3-smarty模板语法 基础模板框架语法html、head、style、widg...

  • smarty模板引擎

    一、模板引擎的工作原理 1、实现HTML代码和PHP代码简单分离,完全去除视图文件中的PHP标记 2、常用PHP模...

  • Smarty模板引擎

    模板引擎的作用是什么 对PHP语言熟悉的程序员就会知道有个Smarty的名词,那么这个具体是什么呢?smarty是...

  • smarty模板引擎

    1)、模板引擎概念 ❖ 模板引擎的功能是实现逻辑与显示相分离,使程序设计者可以专注于程序功能的开发,使网页设计师专...

  • Smarty模板引擎

    Smarty

网友评论

      本文标题:CI框架(CI3.0.6)完美整合Smarty模板引擎(Smar

      本文链接:https://www.haomeiwen.com/subject/dcdfrttx.html