国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - CodeIgniter輔助之第三方類庫third_party用法分析

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

2020-12-17 15:55老彭 PHP教程

這篇文章主要介紹了CodeIgniter輔助之第三方類庫third_party用法,以CI集成Twig模版為例分析了CodeIgniter集成第三方類庫的實現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?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直接輸出頁面
   * @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直接輸出頁面
   * @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進(jìn)行配置,通過CI load library的方式加載時,與類名同名的配置文件存在時,會自動以數(shù)組的方式將參數(shù)傳入類的構(gòu)造函數(shù)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// 默認(rèn)擴(kuò)展名
$config['extension'] = ".tpl";
// 默認(rèn)模版路勁
$config['template_dir'] = APPPATH . "views/";
// 緩存目錄
$config['cache_dir'] = APPPATH . "cache/twig/";
// 是否開啟調(diào)試模式
$config['debug'] = false;
// 自動刷新
$config['auto_reload'] = true;
/* End of file twig.php */
/* Location: ./application/config/twig.php */

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

希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 成人免费视频网站在线观看 | 日韩精品一区二区在线视频 | 自拍偷拍在线视频 | 成人精品一区二区三区中文字幕 | 99热视| 日韩精品一区二区三区中文字幕 | 天天操天天干天天爽 | 91亚洲国产成人久久精品网站 | 日韩福利视频 | 亚洲精品在线视频观看 | 亚洲欧美日韩在线一区 | 国产精品成人在线视频 | 亚洲精品自拍 | 亚洲一区二区三区四区五区中文 | 亚洲国产精品久久人人爱 | 毛片网页| 久久只有精品 | 欧美日韩中文字幕 | 大胆裸体gogo毛片免费看 | 久久久久久国产精品mv | 精品免费久久久久久久苍 | 九九九久久国产免费 | 国产一区视频网站 | 自拍三区 | 日韩城人网站 | 欧美激情五月 | 91久久久久久久久久久久久久久久 | 综合久| 中文字幕在线观看一区二区三区 | 欧美精品在线一区 | 久草热8精品视频在线观看 久久亚洲精品中文字幕 | 桃色一区 | 草草浮力影院 | 国产成人精品电影 | 成人在线精品 | 欧美色欧美亚洲另类七区 | 欧美激情在线观看 | 久久精品视频网站 | 毛片在线一区二区观看精品 | 久久一二区 | 一区中文|