CI框架与Smarty模板整合

第一步,把下载到的smarty模板引擎解压,然后把里面的libs文件夹改名为smarty,然后把这个文件夹整体复制到application\libraries目录下面;

第二步,在application\libraries目录下面建立一个文件,文件名可以自定义,例如创建一个ci_smarty.php文件,打开文件,在文件中写入以下代码:

<?php

if (!defined('BASEPATH'))exit('No direct script access allowed');
require_once('smarty/Smarty.class.php');

class ci_smarty extends Smarty {

    function ci_smarty() {
        parent::__construct();
        $this->template_dir = APPPATH . 'views';
        $this->compile_dir = APPPATH . 'templates_c/';
    }

}

第三步,建立一个application\templates_c文件夹,然后,打开ci\application\config\autoload.php文件,把

$autoload[‘libraries’] = array();

改成

$autoload[‘libraries’] = array('database', 'ci_smarty');

第四步,现在CI框架与Smarty模板引擎已经整合成功,下面我们测试一下效果!
1. 在application\controllers下建立一个文件名为test.php的文件,文件内容如下:

<?php

if (!defined('BASEPATH'))exit('No direct script access allowed');
class test extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }
    function index() {
        $this->ci_smarty->assign("title", "恭喜你smarty安装成功!");
        $this->ci_smarty->assign("content", "欢迎使用smarty模板引擎");
        $arr = array(1 => '欧软网络', 2 => 'www.zhosoft.com');
        $this->ci_smarty->assign("arr", $arr);
        $this->ci_smarty->display('test.html');
    }

}

2.建立html文件,在application\views目录下,建立文件名为test.html的文件,文件内容为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>smarty安装测试</title>
    </head>
    <body>
        <h1>{$title}</h1>
        <p>{$content}</p>
        <ul>
            {foreach from=$arr item=v}
            <li>{$v}</li>
            {/foreach}
        </ul>
    </body>
</html>

【错误问题解决】
1.ci Unable to load the requested class: Ci_smarty
解决办法:将步骤二中ci_smarty.php 改为 Ci_smarty.php
源码下载:http://pan.baidu.com/s/1sku2Oot (提取密码:7m47)
转载请注明出处:http://blog.csdn.net/churk2012/article/details/50697356

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值