欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
模板引擎smarty 3中的變化

最近學(xué)習smarty的時(shí)候,下載的是官方的最新版本:smarty3X,使用之前卻沒(méi)有去讀一下它的使用手冊,而自己所學(xué)教程仍是版本2,給自己帶來(lái)了不少的麻煩.

所以,結合其他的總結資料,把smarty 3變化記錄下來(lái).

Smarty3 的API已經(jīng)被重構過(guò)以更好的面向結構話(huà)和語(yǔ)法一致性。但是Smarty2的API仍然是支持的,但是會(huì )出提示。

當然,也可以手動(dòng)disable掉這個(gè)提示,但是強烈推薦你將你的語(yǔ)法升級到適應Smarty3的語(yǔ)法

Smarty3中所有的方法命名都采用”fooBarBaz”的方式,而且,所有的Smarty屬性都含有g(shù)etters和setters,舉例:

老版本中設置Cache的路徑

1$smarty->cache_dir

現在可以這樣作:

1$smarty->setCacheDir('foo/')

并且可以通過(guò)如下方法獲?。?/p>

1$smarty->getCacheDir()

目錄結構

01index.php
02/libs/
03    Smarty.class.php   #主文件
04/libs/sysplugins/  #內部plugin
05    internal.
06/plugins/   #外部plugin,可自由擴充
07    function.mailto.php
08    modifier.escape.php2
09/templates/   #模板,可以是純php或傳統的smarty模板
10    index.tpl
11    index_view.php

簡(jiǎn)單調用

1require('Smarty.class.php');
2$smarty = new Smarty;
3$smarty->assign('foo','bar');
4$smarty->display('index.tpl');)))

區別

雖然Smarty3在模板使用起來(lái)和以前沒(méi)有區別,但是其實(shí)內部邏輯是截然不同的,卻也是能夠和2進(jìn)行兼容.

除了以下幾點(diǎn)

  1. Smarty3只能運行在PHP5環(huán)境下,不再支持PHP4.
  2. {php}標簽默認是關(guān)閉的,可以通過(guò)如下方式打開(kāi)$smarty->allow_php_tag=true
  3. 模板標簽將不支持空格,如{ $abc }在Smarty2中可以識別的,但是3里頭就不行了,必須這樣{$abc},這樣是為了能夠更好的支持javascript和css.
  4. Smarty3的API有一定的不同,但是仍然支持Smarty2

新的功能

表達式

支持更加隨意的表達式

1{$x+$y}                           輸入x和y的和
2{$foo = strlen($bar)}             變量支持PHP函數
3{assign var=foo value= $x+$y}     屬性支持表達式
4{$foo = myfunct( ($x+$y)*3 )}     函數參數支持表達式
5{$foo[$x+3]}                      數組下表支持表達式

引號中可以使用變量

1{$foo="this is message {counter}"}

可以在模板里頭定義數組

1{assign var=foo value=[1,2,3]}
2{assign var=foo value=['y'=>'yellow','b'=>'blue']}
3{assign var=foo value=[1,[9,8],3]}

簡(jiǎn)單的變量賦值

1{$foo=$bar+2}

可以給指定的數組元素賦值,如果變量存在但不是數組,會(huì )先轉換成數組,再進(jìn)行賦值

1{$foo['bar']=1}
2{$foo['bar']['blar']=1}

同上,可以給數組添加值

1{$foo[]=1}

對象的屬性支持”.”操作符

1{$foo.a.b.c}        =>  $foo['a']['b']['c']
2{$foo.a.$b.c}       =>  $foo['a'][$b]['c']
3{$foo.a.{$b+4}.c}   =>  $foo['a'][$b+4]['c']
4{$foo.a.{$b.c}}     =>  $foo['a'][$b['c']]

變量名中支持變量

1$foo         一個(gè)普通的變量
2$foo_{$bar}  變量名中包含變量
3$foo_{$x+$y} 變量名中可以支持表達式
4$foo_{$bar}_buh_{$blar}  變量名包含多個(gè)變量
5{$foo_{$x}}  如果$x是1,則輸出$foo_1

支持對象鏈,即是對象方法的連續調用,很像jquery

1{$object->method1($x)->method2($y)}

{for}標簽支持類(lèi)似loop一樣的循環(huán)

1{for $x=0, $y=count($foo); $x<$y; $x++}  ....  {/for}

在FOR循環(huán)中可以通過(guò)如下特殊標示符限定位置:

1$x@iteration  當前循環(huán)次數
2$x@total     總循環(huán)次數
3$x@first  循環(huán)第一次
4$x@last     循環(huán)最后一次新的foreach語(yǔ)法

新的foreach語(yǔ)法

1{foreach $myarray as $var}...{/foreach}

同樣是foreach里頭的特殊表示符,看的就明白,不翻譯了……

1$var@key            foreach $var array key
2$var@iteration      foreach current iteration count (1,2,3...)
3$var@index          foreach current index count (0,1,2...)
4$var@total          foreach $var array total
5$var@first          true on first iteration
6$var@last           true on last iteration

支持while循環(huán)

1{while $foo}...{/while}
2{while $x lt 10}...{/while}

可以直接使用PHP的函數

1{time()}

新增加了一個(gè){function}的標簽,可以定義一個(gè)可供調用的函數塊.

1{function}...{/function}

該標簽必須有一個(gè)name屬性,用來(lái)指名該函數名稱(chēng),也是調用的時(shí)候需要用到的

下面是一個(gè)實(shí)例:

01/* 定義一個(gè)函數 */
02{function name=menu level=0}
03    <ul>
04    {foreach $data as $entry}
05        {if is_array($entry)}
06            <li>{$entry@key}</li>
07            {menu data=$entry level=$level+1}
08        {else}
09            <li>{$entry}</li>
10        {/if}
11    {/foreach}
12    </ul>
13{/function}
14  
15/* 給函數傳遞的參數 */
16{$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' =>
17    ['item3-3-1','item3-3-2']],'item4']}
18  
19/* 調用那個(gè)函數 */
20{menu data=$menu}
21  
22代碼塊不緩存,可以使用{nocache}標簽默認是關(guān)閉的
23  
24{nocache} ... {/nocache}
25  
26還可以作為屬性
27  
28{$foo nocache=true}
29{$foo nocache}
30{foo bar="baz" nocache=true}
31{foo bar="baz" nocache}
32{time() nocache=true}
33{time() nocache}
34返回當前模板的方法
35$smarty.cur_template

變量作用域和存儲

在Smarty2中,所有的變量都存儲在Smarty對象中,因此所有的變量在所有模板和子方法中都可以獲取.

在Smarty3中,可以自己定義的將變量存儲在主Smarty對象中,或者用戶(hù)自己定義的對象中,甚至是用戶(hù)自己的模板對象中,而且這些對象可以通過(guò)鏈式串接起來(lái).

在鏈的末尾的對象可以獲取到對象鏈之前的對象中存儲的所有變量.

Smarty對象必須是鏈的根對象,但是對象鏈卻是可以獨立于Smarty對象存在的所有的Smarty的賦值方法都可以用在data對象或者模板對象.

除了上面說(shuō)幾個(gè)方面,全局變量還有一種特殊的存儲方式.

一個(gè)Smarty的數據對象(data Object)可以通過(guò)如下方式創(chuàng )建

1$data = $smarty->createData();    // 創(chuàng )建根數據對象
2$data->assign('foo','bar');       // 賦值操作
3$data->config_load('my.conf');      // 加載配置文件
4  
5$data = $smarty->createData($smarty);   // 以Smarty作為父對象,創(chuàng )建數據對象
6  
7$data2= $smarty->createData($data);     // 以data作為父對象,創(chuàng )建數據對象data2

創(chuàng )建一個(gè)模板對象(template object) 可以通過(guò)createTemplate方法,它的參數傳遞和fetch()/display()方法一致.

函數定義方式

1function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)

舉例

1$tpl = $smarty->createTemplate('mytpl.tpl'); // 創(chuàng )建一個(gè)模板對象,沒(méi)有父對象
2$tpl->assign('foo','bar');                   // directly assign variables
3$tpl->config_load('my.conf');
4  
5$tpl = $smarty->createTemplate('mytpl.tpl',$smarty); // 以Smarty為父對象,創(chuàng )建模板對象

fetch()/display() 兩個(gè)方法將隱式的創(chuàng )建一個(gè)模板對象

如果不指定父對象,則默認父對象將指向Smarty對象

如果一個(gè)模板是通過(guò)include方式調用的,則子模板的父對象將指向引用它的模板對象

所有當前模板變量和父對象的模板對象都是可以獲取的,但是如果是通過(guò){assign}或者{$foo=…}這樣的方法創(chuàng )建或者修改變量

則它的作用域將只停留在當前模板對象

Smarty3中,在賦值變量的時(shí)候可以指定它的作用域,有4個(gè)值local,parent,root,global

1{assign var=foo value='bar'}       // no scope is specified, the default 'local'
2{$foo='bar'}                       // same, local scope
3{assign var=foo value='bar' scope='local'} // same, local scope
4{assign var=foo value='bar' scope='parent'} // Values will be available to the parent object
5{$foo='bar' scope='parent'}                 // (normally the calling template)
6{assign var=foo value='bar' scope='root'}   // Values will be exported up to the root object, so they can
7{$foo='bar' scope='root'}                   // be seen from all templates using the same root.
8{assign var=foo value='bar' scope='global'} // Values will be exported to global variable storage,
9{$foo='bar' scope='global'}

擴展

Smarty3的擴展都是繼承至Smarty – Internal – PluginBase的類(lèi)

所有的擴展都包含一個(gè)Smarty對象實(shí)例的$this->smarty屬性
模板繼承

你可以在模板中寫(xiě){block} … {/block}快,并且這些塊可以在子模板中進(jìn)行覆蓋

01parent.tpl:
02<html>
03    <head>
04        <title>{block name='title'}My site name{/block}</title>
05    </head>
06    <body>
07        <h1>{block name='page-title'}Default page title{/block}</h1>
08        <div id="content">
09            {block name='content'}
10            Default content
11            {/block}
12        </div>
13    </body>
14</html>
1child.tpl:
2  
3{extends file='parent.tpl'}
4  
5{block name='title'}
6    Child title
7{/block}
01grandchild.tpl:
02  
03{extends file='child.tpl'}
04  
05{block name='title'}Home - {$smarty.block.parent}{/block}
06{block name='page-title'}My home{/block}
07{block name='content'}
08    {foreach $images as $img}
09        <img src=" alt="{$img.description}" />
10    {/foreach}
11{/block}

可以通過(guò)extends標簽來(lái)指定被繼承的模板,并在子模板中通過(guò)重寫(xiě)父模板的同名block塊,達到覆蓋的目的

同時(shí),可以通過(guò){$smarty.block,parent}獲取到父block的內容

上面的grandchild.tpl將生成如下內容

1Home - Child title
2<h1>My home</h1>
3<div id="content"><img src="/example.jpg" alt="image" />
4<img src="/example2.jpg" alt="image" />
5<img src="/example3.jpg" alt="image" /></div>

注意,在子模板中,所有在{block} … {/block}之外的內容都將被忽略,這種繼承支持多文件,多重繼承,意味著(zhù)可以無(wú)線(xiàn)的繼承下去.還可通過(guò){block}的append和prepend屬性來(lái)插入父模板結構中.

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
PHP之Smarty模板引擎
smarty實(shí)例教程
PHP教程(39)smarty上
Velocity.js
菜鳥(niǎo)學(xué)PHP之Smarty入門(mén)(組圖)
ECSHOP退換貨插件安裝使用教程
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久