CodeIgniter第三方类库third_party怎么用

发布时间:2021-09-16 15:22:53 作者:小新
来源:亿速云 阅读:103

小编给大家分享一下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第三方类库third_party怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 整理第三方类库
  2. CodeIgniter的优点是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

codeigniter third_party

上一篇:爬虫代理IP为什么会被封

下一篇:Symfony2中getRequest()方法怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》