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应用程序来获取Web服务器的状态信息
Oct 09 PHP
打造计数器DIY三步曲(上)
Oct 09 PHP
jq的get传参数在utf-8中乱码问题的解决php版
Jul 23 PHP
mysql 性能的检查和优化方法
Jun 21 PHP
php获取操作系统语言代码
Nov 04 PHP
header导出Excel应用示例
Jan 24 PHP
PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
Oct 20 PHP
PHP处理Oracle的CLOB实例
Nov 03 PHP
PHP图形操作之Jpgraph学习笔记
Dec 25 PHP
基于PHP实现等比压缩图片大小
Mar 04 PHP
YII中Ueditor富文本编辑器文件和图片上传的配置图文教程
Mar 15 PHP
php + WebUploader实现图片批量上传功能
May 06 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模板技术[转]
2007/01/04 PHP
优化使用mysql存储session的php代码
2008/01/10 PHP
PHPLog php 程序调试追踪工具
2009/09/09 PHP
数据库中排序的对比及使用条件详解
2012/02/23 PHP
深入php var_dump()函数的详解
2013/06/05 PHP
PHP转换IP地址到真实地址的方法详解
2013/06/09 PHP
PHP数字和字符串ID互转函数(类似优酷ID)
2014/06/30 PHP
ThinkPHP之foreach标签使用概述
2014/06/30 PHP
php上传excel表格并获取数据
2017/04/27 PHP
利用PHP实现开心消消乐的算法示例
2017/10/12 PHP
javascript自适应宽度的瀑布流实现思路
2013/02/20 Javascript
Node.js中使用计时器定时执行函数详解
2014/08/15 Javascript
Javascript学习笔记之函数篇(四):arguments 对象
2014/11/23 Javascript
JavaScript模板引擎用法实例
2015/07/10 Javascript
JavaScript利用HTML DOM进行文档操作的方法
2016/03/28 Javascript
使用do...while的方法输入一个月中所有的周日(实例代码)
2016/07/22 Javascript
Vue组件实例间的直接访问实现代码
2017/08/20 Javascript
Angular 5.x 学习笔记之Router(路由)应用
2018/04/08 Javascript
JavaScript数据结构与算法之二叉树插入节点、生成二叉树示例
2019/02/21 Javascript
vue el-table实现自定义表头
2019/12/11 Javascript
python在windows下实现ping操作并接收返回信息的方法
2015/03/20 Python
Python 含参构造函数实例详解
2017/05/25 Python
OpenCV-Python实现轮廓检测实例分析
2018/01/05 Python
python的set处理二维数组转一维数组的方法示例
2019/05/31 Python
python多维数组分位数的求取方式
2020/03/03 Python
使用Python防止SQL注入攻击的实现示例
2020/05/21 Python
Pytorch转tflite方式
2020/05/25 Python
HTML5表格_动力节点Java学院整理
2017/07/11 HTML / CSS
Keds官方网站:购买帆布运动鞋和经典皮鞋
2016/11/12 全球购物
应届毕业生自荐信
2014/05/28 职场文书
小学课外活动总结
2014/07/09 职场文书
党员学习中共十八大报告思想汇报
2014/09/15 职场文书
优秀少先队员事迹材料
2014/12/24 职场文书
运动会开幕词
2015/01/28 职场文书
财务工作个人总结
2015/02/27 职场文书
Pygame Draw绘图函数的具体使用
2021/11/17 Python