CI框架整合Smarty

配置

下载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

在 /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');
    }
}

在 /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_c';
$config['cache_dir'] = APPPATH . 'cache/views';
$config['config_dir'] = APPPATH . 'views/config';
$config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter'] = '{';
$config['right_delimiter'] = '}';

在 /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($varname,$var)
    {
        $this->ci_smarty->assign($varname,$var);
    }

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

使用方法

加载smarty类

有两种方法, 如果随时会用到, 推荐使用自动加载办法:

  1. 在 ci框架的配置目录 config 下的 autoload.php 中自动加载类增加 smarty 类
$autoload['libraries'] = array('ci_smarty');
  1. 直接执行 ci 的加载类方法:
$this->load->library("Ci_smarty");

使用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');
    }
}
  1. 在 views 目录, 创建 test.html 文件
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>smarty结合ci使用测试</title>
    </head>
    <body>
    {$data}
    </body>
</html>
  1. 浏览器访问: localhost/ci_smarty, 输出:ci 3.0.6 结合 smarty 3.1.2 配置成功

smarty手册 https://www.smarty.net/docs/zh_CN/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值