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作的文本留言本的例子(六)
Oct 09 PHP
PHP实现采集程序原理和简单示例代码
Mar 18 PHP
PHP 根据IP地址控制访问的代码
Apr 22 PHP
php 函数使用方法与函数定义方法
May 09 PHP
Base64在线编码解码实现代码 演示与下载
Jan 08 PHP
让CodeIgniter的ellipsize()支持中文截断的方法
Jun 12 PHP
laravel安装和配置教程
Oct 29 PHP
phpnow php探针环境检测代码
Nov 04 PHP
变量在 PHP7 内部的实现(一)
Dec 21 PHP
PHP实现的登录页面信息提示功能示例
Jul 24 PHP
PHP的JSON封装、转变及输出操作示例
Sep 27 PHP
Laravel find in set排序实例
Oct 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
绿山咖啡和蓝山咖啡
2021/03/04 新手入门
PHP 关于访问控制的和运算符优先级介绍
2013/07/08 PHP
php递归删除目录与文件的方法
2015/01/30 PHP
PHP实现获取文件后缀名的几种常用方法
2015/08/08 PHP
php-fpm开启状态统计的方法详解
2017/06/23 PHP
使用Git实现Laravel项目的自动化部署
2019/11/24 PHP
解决 FireFox 下[使用event很麻烦] 的问题.
2006/08/22 Javascript
点击下载链接 弹出页面实现代码
2009/10/01 Javascript
JQuery打造PHP的AJAX表单提交实例
2009/11/03 Javascript
js 利用image对象实现图片的预加载提高访问速度
2013/03/29 Javascript
IE下window.onresize 多次调用与死循环bug处理方法介绍
2013/11/12 Javascript
jquery控制表单输入框显示默认值的方法
2015/05/22 Javascript
Bootstrap的popover(弹出框)2秒后定时消失的实现代码
2017/02/27 Javascript
详解vue+css3做交互特效的方法
2017/11/20 Javascript
extract-text-webpack-plugin用法详解
2019/02/14 Javascript
layer iframe 设置关闭按钮的方法
2019/09/12 Javascript
关于layui导航栏不展示下拉列表的解决方法
2019/09/25 Javascript
浅析python 内置字符串处理函数的使用方法
2014/06/11 Python
Python实现保证只能运行一个脚本实例
2015/06/24 Python
python获取局域网占带宽最大3个ip的方法
2015/07/09 Python
Python中序列的修改、散列与切片详解
2017/08/27 Python
Python cookbook(数据结构与算法)对切片命名清除索引的方法
2018/03/13 Python
Python生成MD5值的两种方法实例分析
2019/04/26 Python
Python函数装饰器原理与用法详解
2019/08/16 Python
浅谈Python 递归算法指归
2019/08/22 Python
python requests证书问题解决
2019/09/05 Python
使用Django xadmin 实现修改时间选择器为不可输入状态
2020/03/30 Python
python文件编写好后如何实践
2020/07/07 Python
Python用Jira库来操作Jira
2020/12/28 Python
CSS3 创建网页动画实现弹跳球动效果
2018/10/30 HTML / CSS
心理学培训心得体会
2016/01/22 职场文书
2019旅游导游工作总结
2019/06/27 职场文书
spring项目中切面及AOP的使用方法
2021/06/26 Java/Android
redis sentinel监控高可用集群实现的配置步骤
2022/04/01 Redis
人物搭配车车超萌联名预备中 【咒术迴战】 ⨯ 【天竺鼠车车】 展开合作
2022/04/11 日漫
MYSQL中文乱码问题的解决方案
2022/06/14 MySQL