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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - PHP教程 - yii,CI,yaf框架+smarty模板使用方法

yii,CI,yaf框架+smarty模板使用方法

2020-12-11 16:15彥輝 PHP教程

這篇文章主要介紹了yii,CI,yaf框架+smarty模板使用方法,結合實例形式介紹了yii,CI及yaf框架整合smaryt模板的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了yiiCIyaf框架+smarty模板使用方法。分享給大家供大家參考,具體如下:

最近折騰了框架的性能測試,其中需要測試各個模板跟smarty配合的性能,所以折騰了一桶,現總結一下。之前已經寫過kohana框架+smarty模板,這里不再重復了。

一、yii框架+smarty模板

yii是覆蓋了viewRenderer組件。

1.1,下載yii框架并解壓,下載smarty框架并解壓,將smarty/libs文件夾拷到yii框架application/protected/vendors下面,并重命名smarty。

1.2,yii配置文件main.php

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
'components'=>array(
 'viewRenderer' => array(
  'class'=>'batman.protected.extensions.SmartyViewRender',
  // 這里為Smarty支持的屬性
  'config' => array (
    'left_delimiter' => "{#",
    'right_delimiter' => "#}",
    'template_dir' => APP_DIR . "/views/",
    'config_dir' => APP_DIR . "/views/conf/",
    'debugging' => false,
    'compile_dir' => 'D:/temp/runtime',
  )
)

其中batman是我已經在index.php定義好的別名。

 
?
1
 
2
3
Yii::setPathOfAlias('batman', dirname(__FILE__));
Yii::import("batman.protected.vendors.*");
define('APP_DIR', dirname(__FILE__).'/protected/');

1.3,在protected/extensions/下面新建SmartyViewRender.php

 
?
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
<?php
class SmartyViewRender extends CApplicationComponent implements IViewRenderer {
 public $fileExtension = '.html';
 private $_smarty = null;
 public $config = array();
 public function init() {
  $smartyPath = Yii::getPathOfAlias('batman.protected.vendors.smarty');
  Yii::$classMap['Smarty'] = $smartyPath . '/Smarty.class.php';
  Yii::$classMap['Smarty_Internal_Data'] = $smartyPath . '/sysplugins/smarty_internal_data.php';
  $this->_smarty = new Smarty();
  // configure smarty
  if (is_array ( $this->config )) {
   foreach ( $this->config as $key => $value ) {
    if ($key {0} != '_') { // not setting semi-private properties
     $this->_smarty->$key = $value;
    }
   }
  }
  Yii::registerAutoloader('smartyAutoload');
 }
 public function renderFile($context, $file, $data, $return) {
   foreach ($data as $key => $value)
    $this->_smarty->assign($key, $value);
  $return = $this->_smarty->fetch($file);
  if ($return)
    return $return;
  else
    echo $return;
 }
}

1.4,驗證

新建一個HelloController.php

 
?
1
 
2
3
4
5
6
<?php
class HelloController extends Controller {
 public function actionWorld() {
  $this->render('world', array('content'=>'hello world'));
 }
}

新建一個word.html

 
?
1
 
2
3
<body>
{#$content#}
</body>

二、CI框架+smarty模板

網上很多方法,將smarty作為一個普通的library,在使用的時候,controller代碼類似于下面:

 
?
1
 
2
3
4
5
6
7
8
9
public function index()
{
  $this->load->library('smarty/Ci_smarty', '', 'smarty');
  $this->smarty->assign("title","恭喜你smarty安裝成功!");
  $this->smarty->assign("body","歡迎使用smarty模板引擎");
  $arr = array(1=>'zhang',2=>'xing',3=>'wang');
  $this->smarty->assign("myarray",$arr);
  $this->smarty->display('index_2.html');
}

這種方法跟CI自帶的使用模板的方法

復制代碼 代碼如下:
$this->load->view();

不和諧,而且要一系列的
復制代碼 代碼如下:
$this->smarty->assign();

語句,麻煩不說,還破壞了原本CI的簡潔美,所以果斷唾棄之。

 

那怎么保持CI加載view時的簡潔美呢,答案就是覆蓋Loader類的view()方法。好吧,let's begin。

2.1,條件:

到官網上現在CI框架和smarty模板。

2.2,確保CI已經能跑起來

將CI框架解壓到網站跟目錄下,先寫一個不帶smarty模板的controller輸出“hello world”。

2.3,引入smarty

將smarty解壓,將libs文件夾考到application/third_paty下面,并將libs重命名smarty,重命名取什么都ok了,這里就叫smarty吧。

2.4,覆蓋loader類的view()方法

因為view()方法在Loader類里,所以我要覆蓋Loader的view()方法。

先看看$this->load->view()是怎么工作的?CI_Controller類的構造函數里有這么一行

復制代碼 代碼如下:
$this->load =& load_class('Loader', 'core');

load_class函數會先在application/core下面找config_item('subclass_prefix').Loader.php文件,找不到再到system/core下面找Loader.php。config_item('subclass_prefix')就是在配置文件里寫的你要繼承CI核心類的子類的前綴。我使用的是默認值'MY_'。找到文件后,require該文件,然后new MY_Loader(如果application/core/MY_Loader.php存在),或者是new Loader,賦值給$this->load。

 

在application/core下面新建一個MY_Loader.php文件

 
?
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
<?php
define('DS', DIRECTORY_SEPARATOR);
class MY_Loader extends CI_Loader {
 public $smarty;
 public function __construct() {
  parent::__construct();
  require APPPATH.'third_party'.DS.'smarty'.DS.'smarty.class.php';
  $this->smarty = new Smarty ();
  // smarty 配置
  $this->smarty->template_dir= APPPATH.'views'.DS;//smarty模板文件指向ci的views文件夾
  $this->smarty->compile_dir = 'd:/temp/tpl_c/';
  $this->smarty->config_dir = APPPATH.'libraries/smarty/configs/';
  $this->smarty->cache_dir  = 'd:/temp/cache';
  $this->smarty->left_delimiter = '{#';
  $this->smarty->right_delimiter = '#}';
 }
 public function view($view, $vars = array(), $return = FALSE)
 {
  // check if view file exists
  $view .= config_item('templates_ext');
  $file = APPPATH.'views'.DS.$view;
  if (! file_exists ( $file ) || realpath ( $file ) === false) {
   exit( __FILE__.' '.__LINE__."<br/>View file {$file} does not exist, <br/>{$file} => {$view}");
  }
  // changed by simeng in order to use smarty debug
  foreach ( $vars as $key => $value ) {
   $this->smarty->assign ( $key, $value );
  }
  // render or return
  if ($return) {
   ob_start ();
  }
  $this->smarty->display ( $view );
  if ($return) {
   $res = ob_get_contents ();
   ob_end_clean ();
   return $res;
  }
 }
}

我把template_ext配置成了".html",這樣就ok了。我們來驗證一下吧。

2.5,驗證

在controller下面建一個home.php

 
?
1
 
2
3
4
5
6
7
8
9
10
class Home extends CI_Controller {
 public function index() {
  $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
  $data['title'] = "恭喜你smarty安裝成功!";
  $data['body'] = "歡迎使用smarty模板引";
  $arr = array(1=>'zhang',2=>'xing',3=>'wang');
  $data['myarray'] = $arr;
  $this->load->view('index_2', $data);
 }
}

在views下面建一個index_2.html

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <script src='<!--{$base_url}-->js/jquery.min.js' type='text/javascript' ></script>
 <link href="<!--{$base_url}-->css/login.css" rel="stylesheet" type="text/css" />
 <title>smarty安裝測試</title>
</head>
<body>
<h1>{#$title#}</h1>
<p>{#$body#}</p>
<ul>
  {#foreach from=$myarray item=v#}
  <li>{#$v#}</li>
  {#/foreach#}
</ul>
</body>
</html>

好了,可以試試你的成果了。

三、yaf框架+smarty模板

yaf是利用引導文件Bootstrap.php來加載smarty。

3.1,使用Bootstrap

在index.php中用

復制代碼 代碼如下:
$app->bootstrap()->run();

 

引入Bootstrap.php文件

3.2,在application/Bootstrap.php文件中導入smarty。

 
?
1
 
2
3
4
5
6
7
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {
 public function _initSmarty(Yaf_Dispatcher $dispatcher) {
  $smarty = new Smarty_Adapter(null, Yaf_Application::app()->getConfig()->smarty);
  Yaf_Dispatcher::getInstance()->setView($smarty);
 }
}

3.3,添加Smarty_Adapter類

將smarty解壓后放到application/library文件夾下,重命名為Smarty。在Smarty下新建Adapter.php,確保Smarty.class.php在Smarty/libs/下。Adapter.php內容:

 
?
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
Yaf_Loader::import( "Smarty/libs/Smarty.class.php");
Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php");
Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_templatelexer.php");
Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_templateparser.php");
Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_compilebase.php");
Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_write_file.php");
class Smarty_Adapter implements Yaf_View_Interface
{
 /**
  * Smarty object
  * @var Smarty
  */
 public $_smarty;
 /**
  * Constructor
  *
  * @param string $tmplPath
  * @param array $extraParams
  * @return void
  */
 public function __construct($tmplPath = null, $extraParams = array()) {
  $this->_smarty = new Smarty;
  if (null !== $tmplPath) {
   $this->setScriptPath($tmplPath);
  }
  foreach ($extraParams as $key => $value) {
   $this->_smarty->$key = $value;
  }
 }
 /**
  * Return the template engine object
  *
  * @return Smarty
  */
 public function getEngine() {
  return $this->_smarty;
 }
 /**
  * Set the path to the templates
  *
  * @param string $path The directory to set as the path.
  * @return void
  */
 public function setScriptPath($path)
 {
  if (is_readable($path)) {
   $this->_smarty->template_dir = $path;
   return;
  }
  throw new Exception('Invalid path provided');
 }
 /**
  * Retrieve the current template directory
  *
  * @return string
  */
 public function getScriptPath()
 {
  return $this->_smarty->template_dir;
 }
 /**
  * Alias for setScriptPath
  *
  * @param string $path
  * @param string $prefix Unused
  * @return void
  */
 public function setBasePath($path, $prefix = 'Zend_View')
 {
  return $this->setScriptPath($path);
 }
 /**
  * Alias for setScriptPath
  *
  * @param string $path
  * @param string $prefix Unused
  * @return void
  */
 public function addBasePath($path, $prefix = 'Zend_View')
 {
  return $this->setScriptPath($path);
 }
 /**
  * Assign a variable to the template
  *
  * @param string $key The variable name.
  * @param mixed $val The variable value.
  * @return void
  */
 public function __set($key, $val)
 {
  $this->_smarty->assign($key, $val);
 }
 /**
  * Allows testing with empty() and isset() to work
  *
  * @param string $key
  * @return boolean
  */
 public function __isset($key)
 {
  return (null !== $this->_smarty->get_template_vars($key));
 }
 /**
  * Allows unset() on object properties to work
  *
  * @param string $key
  * @return void
  */
 public function __unset($key)
 {
  $this->_smarty->clear_assign($key);
 }
 /**
  * Assign variables to the template
  *
  * Allows setting a specific key to the specified value, OR passing
  * an array of key => value pairs to set en masse.
  *
  * @see __set()
  * @param string|array $spec The assignment strategy to use (key or
  * array of key => value pairs)
  * @param mixed $value (Optional) If assigning a named variable,
  * use this as the value.
  * @return void
  */
 public function assign($spec, $value = null) {
  if (is_array($spec)) {
   $this->_smarty->assign($spec);
   return;
  }
  $this->_smarty->assign($spec, $value);
 }
 /**
  * Clear all assigned variables
  *
  * Clears all variables assigned to Zend_View either via
  * {@link assign()} or property overloading
  * ({@link __get()}/{@link __set()}).
  *
  * @return void
  */
 public function clearVars() {
  $this->_smarty->clear_all_assign();
 }
 /**
  * Processes a template and returns the output.
  *
  * @param string $name The template to process.
  * @return string The output.
  */
 public function render($name, $value = NULL) {
  return $this->_smarty->fetch($name);
 }
 public function display($name, $value = NULL) {
  echo $this->_smarty->fetch($name);
 }
}

3.4,smarty配置文件。

再來看看我們的conf/application.ini文件

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
[common]
application.directory = APP_PATH "/application"
application.dispatcher.catchException = TRUE
application.view.ext="tpl"
[smarty : common]
;configures for smarty
smarty.left_delimiter = "{#"
smarty.right_delimiter = "#}"
smarty.template_dir  = APP_PATH "/application/views/"
smarty.compile_dir  = '/data1/www/cache/'
smarty.cache_dir  = '/data1/www/cache/'
[product : smarty]

3.5,驗證

新建一個controller,添加方法:

 
?
1
 
2
3
public function twoAction() {
  $this->getView()->assign('content', 'hello World');
}

新建一個模板two.tpl

 
?
1
 
2
3
4
5
6
7
8
<html>
<head>
<title>A Smarty Adapter Example</title>
</head>
<body>
{#$content#}
</body>
</html>

希望本文所述對大家PHP程序設計有所幫助。

延伸 · 閱讀

精彩推薦
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
主站蜘蛛池模板: 久久久久国产精品午夜一区 | 久久久久久久久久久久免费 | 91视频8mav | 亚洲成人一区 | av网站免费在线观看 | 毛片久久久久久 | 三区视频| 成人国产综合 | 亚洲精品男人的天堂 | 欧美精品亚洲精品 | 亚洲一区自拍偷拍 | 国产精品二区三区 | 女人夜夜春高潮爽av片 | 国产中文字幕在线 | 国产男女做爰免费网站 | 一本一道久久精品综合 | 福利一区二区 | 午夜精品一区二区三区免费视频 | 亚洲视频欧美视频 | 激情综合婷婷 | 国产免费视频在线 | 久草热线| 亚洲午夜激情 | 国产亚洲一区二区三区在线观看 | 波多野结衣一二三四区 | 99久久99| 国产精品国产三级国产aⅴ中文 | 久久丝袜诱惑 | 超碰av在线| av成人在线观看 | 99热a | 国产毛片18片毛一级特黄日韩a | 成人在线看片 | 国产精品国产三级国产aⅴ 亚洲精品免费在线观看 | 亚洲精品国偷拍自产在线观看 | 国内精品视频一区二区三区八戒 | 91偷拍精品一区二区三区 | 欧美日韩亚洲国产精品 | 久久福利电影 | 精品免费久久久久 | 日韩中文字幕在线播放 |