1.   阿里云服务器1888元代金券领取
  2.   本网站所有源码包解压密码:www.youhutong.com
登录  帮助问答

CI框架整合Smarty模板(Smarty版本2.6.3.1)的最佳方法

小川 技术文章
郑重声明:
  本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,否则后果请用户自负,下载后请24小时内删除。 本站所有内容均不能保证其完整性,不提供任何技术支持,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用。

一.下载Smarty模板引擎包.把Smarty搬迁到CI中去.

下载地址1:http://www.smarty.net/download [官网有时会比较卡]

下载地址2:https://pan.baidu.com/s/1Z04vu63tzRBFbi3efxKYCA [百度云]

1.下载好了之后.解压复制整个lib文件夹.放到CI框架项目/libraries文件夹下,并重命名为:Smarty文件夹.

2.然后在Smarty文件夹下新建文件名为:Cismarty.php.里面的代码为:

<?php   
// +----------------------------------------------------------------------
// |        Smarty模板控制器      Author:xiaochuan     28126649@qq.com
// +----------------------------------------------------------------------
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');   
require_once( APPPATH . 'libraries/Smarty/Smarty.class.php' );   
class Cismarty extends Smarty {   
    protected $ci;   
    protected $complie_dir;
    protected $template_ext;
    public function  __construct(){   
        parent::__construct();
        $this->ci = & get_instance();   
        //加载smarty的配置文件
        $this->ci->load->config('smarty'); 
        //获取相关的配置项   
        $this->template_dir   = $this->ci->config->item('template_dir');   
        $this->complie_dir    = $this->ci->config->item('compile_dir');   
        $this->cache_dir      = $this->ci->config->item('cache_dir');   
        $this->config_dir     = $this->ci->config->item('config_dir');   
        $this->template_ext   = $this->ci->config->item('template_ext');   
        $this->caching        = $this->ci->config->item('caching');   
        $this->cache_lifetime = $this->ci->config->item('lefttime');  
        //获取模板标签配置项
        $this->left_delimiter  = $this->ci->config->item('left_delimiter');
        $this->right_delimiter = $this->ci->config->item('right_delimiter'); 
    }   
}

二.配置,让Smarty正常运行在CI框架中.

1.我们上面已经新建了一个专门调用Smarty模板引擎的PHP文件了.现在我们只需要让程序每次运行时都自动的去运行该文件就好了.那我们去配置一下:在项目目录config文件夹下找到config.php.然后在$autoload['libraries']后加上Smarty/Cismarty.

//配置Cismarty.php文件为预加载
$autoload['libraries'] = array('Smarty/Cismarty');

//下面就可以正常使用了.用法如下:
$this->cismarty->assign('name',$data);
$this->cismarty->display('模板名');

三.为了方便使用Smarty,自己又简化了一下上面的用法.

1.在我们的核心类里加两个方法.代码如下:

/**
 * 模板赋值
 * author: xiaochuan
 * @access public
 * @param 
 * @return 
 */
public function assign($key,$val) 
{   
    $this->cismarty->assign($key,$val);   
}   
  
/**
 * 调用模板
 * author: xiaochuan
 * @access public
 * @param 
 * @return 
 */
public function display($html=null) 
{   
    if(empty($html)) $html = $this->router->method;
    $this->cismarty->display($this->router->class.'/'.$html.$this->config->item('template_ext'));   
}

好处:如果模板名跟方法名同名我们就不用在display后写模板名了.最后用法变成了.

//最终用法:
$this->assign('name',$data);
$this->display();

是不是方便很多了.


 浏览器启用弹出窗口过滤功能,将无法跳转到下载页。在浏览器地址栏右边符号提示处点击允许就可以了!

转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/36.html


  1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
  2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
  3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
  4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意
( 1 )个小伙伴在吐槽
    登录帐号  如果已经登录请刷新! 发表我的评论
    表情