A-A+

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

2021年07月06日 我爱编程 暂无评论

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

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

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

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. require APPPATH.'third_party/Twig/Autoloader.php';
  3. /**
  4.  * Twig模版引擎
  5.  *
  6.  */
  7. class Twig
  8. {
  9.   public $twig;
  10.   public $config;
  11.   private $data = array();
  12.   /**
  13.    * 读取配置文件twig.php并初始化设置
  14.    * 
  15.    */
  16.   public function __construct($config)
  17.   {
  18.     $config_default = array(
  19.       'cache_dir' => false,
  20.       'debug' => false,
  21.       'auto_reload' => true,
  22.       'extension' => '.tpl',
  23.     );
  24.     $this->config = array_merge($config_default$config);
  25.     Twig_Autoloader::register ();
  26.     $loader = new Twig_Loader_Filesystem ($this->config['template_dir']);
  27.     $this->twig = new Twig_Environment ($loaderarray (
  28.         'cache' => $this->config['cache_dir'],
  29.         'debug' => $this->config['debug'],
  30.         'auto_reload' => $this->config['auto_reload'],
  31.     ) );
  32.     $CI = & get_instance ();
  33.     $CI->load->helper(array('url'));
  34.     $this->twig->addFunction(new Twig_SimpleFunction('site_url''site_url'));
  35.     $this->twig->addFunction(new Twig_SimpleFunction('base_url''base_url'));
  36.   }
  37.   /**
  38.    * 给变量赋值
  39.    * 
  40.    * @param string|array $var
  41.    * @param string $value
  42.    */
  43.   public function assign($var$value = NULL)
  44.   {
  45.     if(is_array($var)) {
  46.       foreach($val as $key => $val) {
  47.         $this->data[$key] = $val;
  48.       }
  49.     } else {
  50.       $this->data[$var] = $value;
  51.     }
  52.   }
  53.   /**
  54.    * 模版渲染
  55.    * 
  56.    * @param string $template 模板名
  57.    * @param array $data 变量数组
  58.    * @param string $return true返回 false直接输出页面
  59.    * @return string
  60.    */
  61.   public function render($template$data = array(), $return = FALSE)
  62.   {
  63.     $template = $this->twig->loadTemplate ( $this->getTemplateName($template) );
  64.     $data = array_merge($this->data, $data);
  65.     if ($return === TRUE) {
  66.       return $template->render ( $data );
  67.     } else {
  68.       return $template->display ( $data );
  69.     }
  70.   }
  71.   /**
  72.    * 获取模版名
  73.    * 
  74.    * @param string $template
  75.    */
  76.   public function getTemplateName($template)
  77.   {
  78.     $default_ext_len = strlen($this->config['extension']);
  79.     if(substr($template, -$default_ext_len) != $this->config['extension']) {
  80.       $template .= $this->config['extension'];
  81.     }
  82.     return $template;
  83.   }
  84.   /**
  85.    * 字符串渲染
  86.    * 
  87.    * @param string $string 需要渲染的字符串
  88.    * @param array $data 变量数组
  89.    * @param string $return true返回 false直接输出页面
  90.    * @return string
  91.    */
  92.   public function parse($string$data = array(), $return = FALSE)
  93.   {
  94.     $string = $this->twig->loadTemplate ( $string );
  95.     $data = array_merge($this->data, $data);
  96.     if ($return === TRUE) {
  97.       return $string->render ( $data );
  98.     } else {
  99.       return $string->display ( $data );
  100.     }
  101.   }
  102. }
  103. /* End of file Twig.php */
  104. /* Location: ./application/libraries/Twig.php */

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

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

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

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录