首頁 後端開發 php教程 CodeIgniter輔助之第三方類庫third_party用法分析

CodeIgniter輔助之第三方類庫third_party用法分析

Jul 29, 2016 am 09:05 AM
config string template this twig

本文實例分析了CodeIgniter輔助之第三方類別庫third_party用法。分享給大家供大家參考,具體如下:

third_party用來存放系統中引入的第三方類庫,類庫通常提供的功能比較豐富,相應的學習成本也要高些,系統中能用到功能有限,所以建議在引入類別庫時進行適當的封裝,讓系統中更方便使用,其他人使用時只需關注擴展的方法而無法關注具體的實現。以CI整合Twig模版為例吧。

首先需要下載Twig類別庫,並放在third_party中,然後在libraries中進行一次封裝,示例如下:

<&#63;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相關內容有興趣的讀者可查看本站專題:《codeigniter入門教程》和《CI(CodeIgniter)框架進階教學》

希望本文所述對大家基於CodeIgniter框架的PHP程式設計有所幫助。

以上就介紹了CodeIgniter輔助之第三方類庫third_party用法分析,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

使用java的String.valueOf()函數將基本資料型別轉換為字串 使用java的String.valueOf()函數將基本資料型別轉換為字串 Jul 24, 2023 pm 07:55 PM

使用java的String.valueOf()函數將基本資料型別轉換為字串

怎麼把char數組轉string 怎麼把char數組轉string Jun 09, 2023 am 10:04 AM

怎麼把char數組轉string

如何在CakePHP中使用Twig? 如何在CakePHP中使用Twig? Jun 05, 2023 pm 07:51 PM

如何在CakePHP中使用Twig?

如何在PHP中使用Twig模板引擎進行Web開發 如何在PHP中使用Twig模板引擎進行Web開發 Jun 25, 2023 pm 04:03 PM

如何在PHP中使用Twig模板引擎進行Web開發

使用java的String.replace()函數替換字串中的字元(字串) 使用java的String.replace()函數替換字串中的字元(字串) Jul 25, 2023 pm 05:16 PM

使用java的String.replace()函數替換字串中的字元(字串)

2w字 詳解 String,yyds 2w字 詳解 String,yyds Aug 24, 2023 pm 03:56 PM

2w字 詳解 String,yyds

Java String中的split方法如何使用 Java String中的split方法如何使用 May 02, 2023 am 09:37 AM

Java String中的split方法如何使用

使用java的String.length()函數取得字串的長度 使用java的String.length()函數取得字串的長度 Jul 25, 2023 am 09:09 AM

使用java的String.length()函數取得字串的長度

See all articles