溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

CodeIgniter第三方類庫(kù)third_party怎么用

發(fā)布時(shí)間:2021-09-16 15:22:53 來(lái)源:億速云 閱讀:114 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下CodeIgniter第三方類庫(kù)third_party怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體如下:

third_party用來(lái)存放系統(tǒng)中引入的第三方類庫(kù),類庫(kù)通常提供的功能比較豐富,相應(yīng)的學(xué)習(xí)成本也要高些,系統(tǒng)中能用到功能有限,所以建議在引入類庫(kù)時(shí)進(jìn)行適當(dāng)?shù)姆庋b,讓系統(tǒng)中更方便使用,其他人使用時(shí)只需關(guān)注擴(kuò)展的方法而無(wú)法關(guān)注具體的實(shí)現(xiàn)。以CI集成Twig模版為例吧。

首先需要下載Twig類庫(kù),并放在third_party中,然后在libraries中進(jìn)行一次封裝,示例如下:

<?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并初始化設(shè)置
   * 
   */
  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 變量數(shù)組
   * @param string $return true返回 false直接輸出頁(yè)面
   * @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 變量數(shù)組
   * @param string $return true返回 false直接輸出頁(yè)面
   * @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 */

模版的操作通常有一些配置的信息,這里通過(guò)config下的twig.php進(jìn)行配置,通過(guò)CI load library的方式加載時(shí),與類名同名的配置文件存在時(shí),會(huì)自動(dòng)以數(shù)組的方式將參數(shù)傳入類的構(gòu)造函數(shù)。

<?php
// 默認(rèn)擴(kuò)展名
$config['extension'] = ".tpl";
// 默認(rèn)模版路勁
$config['template_dir'] = APPPATH . "views/";
// 緩存目錄
$config['cache_dir'] = APPPATH . "cache/twig/";
// 是否開(kāi)啟調(diào)試模式
$config['debug'] = false;
// 自動(dòng)刷新
$config['auto_reload'] = true;
/* End of file twig.php */
/* Location: ./application/config/twig.php */

為了加載base_url site_url等函數(shù)到模版,類與CI產(chǎn)生了依賴,分離開(kāi)可能更好,比如在serice中進(jìn)行一次封裝,增加一些自定義函數(shù)等,這樣其他地方、其他系統(tǒng)也就很方便復(fù)用該類了。

以上是“CodeIgniter第三方類庫(kù)third_party怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI