CodeIgniter辅助之第三方类库third_party用法分析


Posted in PHP onJanuary 20, 2016

本文实例分析了CodeIgniter辅助之第三方类库third_party用法。分享给大家供大家参考,具体如下:

third_party用来存放系统中引入的第三方类库,类库通常提供的功能比较丰富,相应的学习成本也要高些,系统中能用到功能有限,所以建议在引入类库时进行适当的封装,让系统中更方便使用,其他人使用时只需关注扩展的方法而无法关注具体的实现。以CI集成Twig模版为例吧。

首先需要下载Twig类库,并放在third_party中,然后在libraries中进行一次封装,示例如下:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH.'third_party/Twig/Autoloader.php';
/**
 * Twig模版引擎
 *
 */
class Twig
{
  public $twig;
  public $config;
  private $data = array();
  /**
   * 读取配置文件twig.php并初始化设置
   * 
   */
  public function __construct($config)
  {
    $config_default = array(
      'cache_dir' => false,
      'debug' => false,
      'auto_reload' => true,
      'extension' => '.tpl',
    );
    $this->config = array_merge($config_default, $config);
    Twig_Autoloader::register ();
    $loader = new Twig_Loader_Filesystem ($this->config['template_dir']);
    $this->twig = new Twig_Environment ($loader, array (
        'cache' => $this->config['cache_dir'],
        'debug' => $this->config['debug'],
        'auto_reload' => $this->config['auto_reload'], 
    ) );
    $CI = & get_instance ();
    $CI->load->helper(array('url'));
    $this->twig->addFunction(new Twig_SimpleFunction('site_url', 'site_url'));
    $this->twig->addFunction(new Twig_SimpleFunction('base_url', 'base_url'));
  }
  /**
   * 给变量赋值
   * 
   * @param string|array $var
   * @param string $value
   */
  public function assign($var, $value = NULL)
  {
    if(is_array($var)) {
      foreach($val as $key => $val) {
        $this->data[$key] = $val;
      }
    } else {
      $this->data[$var] = $value;
    }
  }
  /**
   * 模版渲染
   * 
   * @param string $template 模板名
   * @param array $data 变量数组
   * @param string $return true返回 false直接输出页面
   * @return string
   */
  public function render($template, $data = array(), $return = FALSE)
  {
    $template = $this->twig->loadTemplate ( $this->getTemplateName($template) );
    $data = array_merge($this->data, $data);
    if ($return === TRUE) {
      return $template->render ( $data );
    } else {
      return $template->display ( $data );
    }
  }
  /**
   * 获取模版名
   * 
   * @param string $template
   */
  public function getTemplateName($template)
  {
    $default_ext_len = strlen($this->config['extension']);
    if(substr($template, -$default_ext_len) != $this->config['extension']) {
      $template .= $this->config['extension'];
    }
    return $template;
  }
  /**
   * 字符串渲染
   * 
   * @param string $string 需要渲染的字符串
   * @param array $data 变量数组
   * @param string $return true返回 false直接输出页面
   * @return string
   */
  public function parse($string, $data = array(), $return = FALSE)
  {
    $string = $this->twig->loadTemplate ( $string );
    $data = array_merge($this->data, $data);
    if ($return === TRUE) {
      return $string->render ( $data );
    } else {
      return $string->display ( $data );
    }
  }
}
/* End of file Twig.php */
/* Location: ./application/libraries/Twig.php */

模版的操作通常有一些配置的信息,这里通过config下的twig.php进行配置,通过CI load library的方式加载时,与类名同名的配置文件存在时,会自动以数组的方式将参数传入类的构造函数。

<?php
// 默认扩展名
$config['extension'] = ".tpl";
// 默认模版路劲
$config['template_dir'] = APPPATH . "views/";
// 缓存目录
$config['cache_dir'] = APPPATH . "cache/twig/";
// 是否开启调试模式
$config['debug'] = false;
// 自动刷新
$config['auto_reload'] = true;
/* End of file twig.php */
/* Location: ./application/config/twig.php */

为了加载base_url site_url等函数到模版,类与CI产生了依赖,分离开可能更好,比如在serice中进行一次封装,增加一些自定义函数等,这样其他地方、其他系统也就很方便复用该类了。

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php miniBB中文乱码问题解决方法
Nov 25 PHP
用mysql触发器自动更新memcache的实现代码
Oct 11 PHP
php判断输入不超过mysql的varchar字段的长度范围
Jun 24 PHP
基于php缓存的详解
May 15 PHP
获取URL文件名后缀
Oct 24 PHP
smarty中英文多编码字符截取乱码问题解决方法
Oct 28 PHP
PHP使用xmllint命令处理xml与html的方法
Dec 15 PHP
Apache无法自动跳转却显示目录的解决方法
Nov 30 PHP
php读取qqwry.dat ip地址定位文件的类实例代码
Nov 15 PHP
Laravel 批量更新多条数据的示例
Nov 27 PHP
使用laravel的migrate创建数据表的方法
Sep 30 PHP
ThinkPHP5.1的权限控制怎么写?分享一个AUTH权限控制
Mar 09 PHP
CodeIgniter扩展核心类实例详解
Jan 20 #PHP
CodeIgniter视图使用注意事项
Jan 20 #PHP
CodeIgniter读写分离实现方法详解
Jan 20 #PHP
PHP实现微信网页授权开发教程
Jan 19 #PHP
CodeIgniter配置之SESSION用法实例分析
Jan 19 #PHP
CodeIgniter配置之routes.php用法实例分析
Jan 19 #PHP
CodeIgniter配置之config.php用法实例分析
Jan 19 #PHP
You might like
ThinkPHP 防止表单重复提交的方法
2011/08/08 PHP
php中curl、fsocket、file_get_content三个函数的使用比较
2014/05/09 PHP
PHP中4种常用的抓取网络数据方法
2015/06/04 PHP
php实现统计目录文件大小的函数
2015/12/25 PHP
PHP使用Memcache时模拟命名空间及缓存失效问题的解决
2016/02/27 PHP
PHP框架Laravel中实现supervisor执行异步进程的方法
2017/06/07 PHP
如何通过View::first使用Laravel Blade的动态模板详解
2017/09/21 PHP
php连接MSsql server的五种方法总结
2018/03/04 PHP
取得传值的函数
2006/10/27 Javascript
Apply an AutoFormat to an Excel Spreadsheet
2007/06/12 Javascript
EXT窗口Window及对话框MessageBox
2011/01/27 Javascript
浅谈javascript中return语句
2015/07/15 Javascript
JS简单限制textarea内输入字符数量的方法
2015/10/14 Javascript
JavaScript中获取Radio被选中的值
2015/11/11 Javascript
JavaScript实现设计模式中的单例模式的一些技巧总结
2016/05/17 Javascript
简单实现js间歇或无缝滚动效果
2016/06/29 Javascript
Javascript 判断两个IP是否在同一网段实例代码
2016/11/28 Javascript
node作为中间服务层如何发送请求(发送请求的实现方法详解)
2018/01/02 Javascript
js实现各浏览器全屏代码实例
2018/07/03 Javascript
配置node服务器并且链接微信公众号接口配置步骤详解
2019/06/21 Javascript
Python时区设置方法与pytz查询时区教程
2013/11/27 Python
解决python文件字符串转列表时遇到空行的问题
2017/07/09 Python
对python过滤器和lambda函数的用法详解
2019/01/21 Python
pyqt5利用pyqtDesigner实现登录界面
2019/03/28 Python
django框架实现模板中获取request 的各种信息示例
2019/07/01 Python
pytorch下大型数据集(大型图片)的导入方式
2020/01/08 Python
css3绘制百度的小度熊
2018/10/29 HTML / CSS
friso美素佳儿官方海外旗舰店:荷兰原产原罐
2017/07/03 全球购物
SKECHERS官方旗舰店:美国舒适运动休闲品牌
2017/12/22 全球购物
北美主要的汽车零部件零售商:AutoShack.com
2019/02/23 全球购物
伦敦香水公司:The London Perfume Company
2019/11/13 全球购物
财务经理岗位职责
2013/11/09 职场文书
治安消防安全责任书
2014/07/23 职场文书
我们的节日中秋活动方案
2014/08/19 职场文书
法制教育主题班会
2015/08/13 职场文书
javascript代码简写的几种常用方式汇总
2021/08/23 Javascript