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生成静态页
Nov 25 PHP
再次研究下cache_lite
Feb 14 PHP
PHP实现AES256加密算法实例
Sep 22 PHP
Codeigniter通过SimpleXML将xml转换成对象的方法
Mar 19 PHP
smarty模板判断数组为空的方法
Jun 10 PHP
discuz图片顺序混乱解决方案
Jul 29 PHP
php实现递归的三种基本方式
Jul 04 PHP
WordPress中邮件的一些修改和自定义技巧
Dec 15 PHP
Yii的Srbac插件用法详解
Jul 14 PHP
深入浅析Yii admin的权限控制
Aug 31 PHP
ThinkPHP整合datatables实现服务端分页的示例代码
Feb 10 PHP
laravel 解决crontab不执行的问题
Oct 22 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
PHP 导出Excel示例分享
2014/08/18 PHP
php自动加载方式集合
2016/04/04 PHP
PDO::getAttribute讲解
2019/01/28 PHP
PHP数组基本用法与知识点总结
2020/06/02 PHP
js表数据排序 sort table data
2009/02/18 Javascript
基于JQuery实现异步刷新的代码(转载)
2011/03/29 Javascript
javascript数字时钟示例分享
2014/04/23 Javascript
JS闭包、作用域链、垃圾回收、内存泄露相关知识小结
2016/05/16 Javascript
Angularjs中controller的三种写法分享
2016/09/21 Javascript
vue.js学习笔记之绑定style样式和class列表
2016/10/31 Javascript
微信小程序 弹窗自定义实例代码
2017/03/08 Javascript
详解Vue方法与事件
2017/03/09 Javascript
Angular.js ng-file-upload结合springMVC的使用教程
2017/07/10 Javascript
使用重写url机制实现验证码换一张功能
2017/08/01 Javascript
jQuery实现可兼容IE6的遮罩功能详解
2017/09/19 jQuery
webpack打包node.js后端项目的方法
2018/03/10 Javascript
你可能不知道的CORS跨域资源共享
2019/03/13 Javascript
layui实现数据表格table分页功能(ajax异步)
2019/07/27 Javascript
vue实现页面滚动到底部刷新
2019/08/16 Javascript
解决vue axios跨域 Request Method: OPTIONS问题(预检请求)
2020/08/14 Javascript
js+css3实现简单时钟特效
2020/09/13 Javascript
ant design vue嵌套表格及表格内部编辑的用法说明
2020/10/28 Javascript
浅析Python的Django框架中的Memcached
2015/07/23 Python
Python聚类算法之DBSACN实例分析
2015/11/20 Python
python扫描proxy并获取可用代理ip的实例
2017/08/07 Python
python 接口_从协议到抽象基类详解
2017/08/24 Python
Numpy数据类型转换astype,dtype的方法
2018/06/09 Python
Python XML转Json之XML2Dict的使用方法
2019/01/15 Python
Python中函数参数匹配模型详解
2019/06/09 Python
Python替换NumPy数组中大于某个值的所有元素实例
2020/06/08 Python
Python pymsql模块的使用
2020/09/07 Python
《蓝色的树叶》教学反思
2014/02/24 职场文书
党的群众路线教育实践活动整改落实情况自查报告
2014/10/28 职场文书
家长对孩子的寄语
2015/02/26 职场文书
留学推荐信怎么写
2015/03/26 职场文书
慈善献爱心倡议书
2015/04/27 职场文书