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生成EXCEL的东东
Oct 09 PHP
一个改进的UBB类
Oct 09 PHP
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
Aug 12 PHP
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
Jun 24 PHP
php一些错误处理的方法与技巧总结
Aug 10 PHP
测试PHP连接MYSQL成功与否的代码
Aug 16 PHP
在WordPress中获取数据库字段内容和添加主题设置菜单
Jan 11 PHP
PHP.vs.JAVA
Apr 29 PHP
php实现HTML实体编号与非ASCII字符串相互转换类实例
Nov 02 PHP
Laravel Eloquent ORM 实现查询表中指定的字段
Oct 17 PHP
Laravel 集成微信用户登录和绑定的实现
Dec 27 PHP
PHP fopen中文文件名乱码问题解决方案
Oct 28 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 数据库树的遍历方法
2009/02/06 PHP
php获取中文拼音首字母类和函数分享
2014/04/24 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
2017/04/27 PHP
PHP创建XML的方法示例【基于DOMDocument类及SimpleXMLElement类】
2019/09/10 PHP
JavaScript 继承详解(二)
2009/07/13 Javascript
Javascript 继承机制实例
2009/08/12 Javascript
ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
2012/02/03 Javascript
Jquery时间验证和转换工具小例子
2013/07/01 Javascript
form.submit()不能提交表单的错误原因及解决方法
2014/10/13 Javascript
js实现的捐赠管理完整实例
2015/01/20 Javascript
Jquery数字上下滚动动态切换插件
2015/08/08 Javascript
javascript中eval解析JSON字符串
2016/02/27 Javascript
一步步教大家编写酷炫的导航栏js+css实现
2016/03/14 Javascript
JavaScript驾驭网页-CSS与DOM
2016/03/24 Javascript
浅谈JavaScript for循环 闭包
2016/06/22 Javascript
jQuery实现点击任意位置弹出层外关闭弹出层效果
2016/10/19 Javascript
浅谈vue的iview列表table render函数设置DOM属性值的方法
2017/09/30 Javascript
javascript实现动态时钟的启动和停止
2020/07/29 Javascript
Python+django实现简单的文件上传
2016/08/17 Python
简单谈谈Python中函数的可变参数
2016/09/02 Python
matplotlib简介,安装和简单实例代码
2017/12/26 Python
django使用xlwt导出excel文件实例代码
2018/02/06 Python
Python字符串的15个基本操作(小结)
2021/02/03 Python
CSS3实现div从下往上滑入滑出效果示例
2020/04/28 HTML / CSS
韩国保养品、日本药妆购物网:小三美日
2018/12/30 全球购物
城市轨道专业个人求职信范文
2013/09/23 职场文书
施工材料员岗位职责
2014/02/12 职场文书
餐厅销售主管职责范本
2014/02/19 职场文书
银行反四风对照检查材料
2014/09/29 职场文书
2014年房产经纪人工作总结
2014/12/08 职场文书
慰问信格式规范
2015/03/23 职场文书
高中化学教学反思
2016/02/22 职场文书
怎样写工作总结啊!
2019/06/18 职场文书
python如何在word中存储本地图片
2021/04/07 Python
python实现过滤敏感词
2021/05/08 Python
Spring实现内置监听器
2021/07/09 Java/Android