smarty3的一些實用的新特性

BETA8 需要注意的事情
Smarty3 的API已經被重構過以更好的面向結構話和語法一致性。但是Smarty2的API仍然是支持的,但是會出提示。

當然,也可以手動disable掉這個提示,但是強烈推薦你將你的語法升級到適應Smarty3的語法

Smarty3中所有的方法命名都採用”fooBarBaz”的方式,而且,所有的Smarty屬性都含有getters和setters,舉例:

老版本中設置Cache的路徑

$smarty->cache_dir
現在可以這樣作:

$smarty->setCacheDir('foo/')
並且可以通過如下方法獲取:

$smarty->getCacheDir()
一些smarty3的api比如以isXX開頭的方法已經被取消,因爲現在已經有實現相同功能的類似getXX的方法代替了

以下是一個簡單的API列表

$smarty->fetch($template, $cache_id = null, $compile_id = null, $parent = null)
$smarty->display($template, $cache_id = null, $compile_id = null, $parent = null)
$smarty->isCached($template, $cache_id = null, $compile_id = null)
$smarty->createData($parent = null)
$smarty->createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)
$smarty->enableSecurity()
$smarty->disableSecurity()
$smarty->setTemplateDir($template_dir)
$smarty->addTemplateDir($template_dir)
$smarty->templateExists($resource_name)
$smarty->loadPlugin($plugin_name, $check = true)
$smarty->loadFilter($type, $name)
$smarty->setExceptionHandler($handler)
$smarty->addPluginsDir($plugins_dir)
$smarty->getGlobal($varname = null)
$smarty->getRegisteredObject($name)
$smarty->getDebugTemplate()
$smarty->setDebugTemplate($tpl_name)
$smarty->assign($tpl_var, $value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
$smarty->assignGlobal($varname, $value = null, $nocache = false)
$smarty->assignByRef($tpl_var, &$value, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
$smarty->append($tpl_var, $value = null, $merge = false, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
$smarty->appendByRef($tpl_var, &$value, $merge = false)
$smarty->clearAssign($tpl_var)
$smarty->clearAllAssign()
$smarty->configLoad($config_file, $sections = null)
$smarty->getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
$smarty->getConfigVariable($variable)
$smarty->getStreamVariable($variable)
$smarty->getConfigVars($varname = null)
$smarty->clearConfig($varname = null)
$smarty->getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
一些API的調用是通過它自己的對象完成的

$smarty->cache->loadResource($type = null)
$smarty->cache->clearAll($exp_time = null, $type = null)
$smarty->cache->clear($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)

$smarty->register->block($block_tag, $block_impl, $cacheable = true, $cache_attr = array())
$smarty->register->compilerFunction($compiler_tag, $compiler_impl, $cacheable = true)
$smarty->register->templateFunction($function_tag, $function_impl, $cacheable = true, $cache_attr = array())
$smarty->register->modifier($modifier_name, $modifier_impl)
$smarty->register->templateObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
$smarty->register->outputFilter($function_name)
$smarty->register->postFilter($function_name)
$smarty->register->preFilter($function_name)
$smarty->register->resource($resource_type, $function_names)
$smarty->register->variableFilter($function_name)
$smarty->register->defaultPluginHandler($function_name)
$smarty->register->defaultTemplateHandler($function_name)

$smarty->unregister->block($block_tag)
$smarty->unregister->compilerFunction($compiler_tag)
$smarty->unregister->templateFunction($function_tag)
$smarty->unregister->modifier($modifier)
$smarty->unregister->templateObject($object_name)
$smarty->unregister->outputFilter($function_name)
$smarty->unregister->postFilter($function_name)
$smarty->unregister->preFilter($function_name)
$smarty->unregister->resource($resource_type)
$smarty->unregister->variableFilter($function_name)

$smarty->utility->compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
$smarty->utility->clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
$smarty->utility->testInstall()
然後是所有的getters和setters,可以用來獲取和設置所有屬性,以下是一些例子:

$caching = $smarty->getCaching();      // get $smarty->caching
$smarty->setCaching(true);             // set $smarty->caching
$smarty->setDeprecationNotices(false); // set $smarty->deprecation_notices
$smarty->setCacheId($id);              // set $smarty->cache_id
$debugging = $smarty->getDebugging();  // get $smarty->debuggin
目錄結構
和Smarty2結構類似

index.php
/libs/
    Smarty.class.php   #主文件
/libs/sysplugins/  #內部plugin
    internal.
/plugins/   #外部plugin,可自由擴充
    function.mailto.php
    modifier.escape.php2
/templates/   #模板,可以是純php或傳統的smarty模板
    index.tpl
    index_view.php
非常多的Smarty3核心功能函數是放在sysplugins目錄底下,你不需要去修改其中的任何文件

它的插件文件則是放在/lib/plugins目錄底下,你可以在其中增加你自己的插件文件。

你仍然需要創建自己的cache/,templates/, template_c/, configs/目錄,並且要保證cache/,template_c兩個目錄具有寫權限

簡單調用
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('foo','bar');
$smarty->display('index.tpl');)))
區別
雖然Smarty3在模板使用起來和以前沒有區別,但是其實內部邏輯是截然不同的,卻也是能夠和2進行兼容

除了以下幾點

1.Smarty3只能運行在PHP5環境下,不再支持PHP4
2.{php}標籤默認是關閉的,可以通過如下方式打開 
$smarty->allow_php_tag=true

3.模板標籤將不支持空格,如{ $abc }在Smarty2中可以識別的,但是3裏頭就不行了,必須這樣{$abc},這樣是爲了能夠更好的支持javascript和css,但是你仍然可以通過設置來支持原來的形式 
$smarty->auto_literal = false;

4.Smarty3的API有一定的不同,但是仍然支持Smarty2
詞法特性
Smarty3 採用一個詞法分析器來進行模板的解析和編譯,基於這種方式,它可以支持一些語法擴展來讓生活變得更加美好!

比如模板內部的數學計算,直觀,簡短的函數參數選項,以及無窮的函數遞歸,更準確的錯誤處理等等

新的功能
表達式
支持更加隨意的表達式

{$x+$y}                           輸入x和y的和
{$foo = strlen($bar)}             變量支持PHP函數
{assign var=foo value= $x+$y}     屬性支持表達式
{$foo = myfunct( ($x+$y)*3 )}     函數參數支持表達式
{$foo[$x+3]}                      數組下表支持表達式
引號中可以使用變量

{$foo="this is message {counter}"}
可以在模板裏頭定義數組

{assign var=foo value=[1,2,3]}
{assign var=foo value=['y'=>'yellow','b'=>'blue']}
{assign var=foo value=[1,[9,8],3]}
簡單的變量賦值

{$foo=$bar+2}
可以給指定的數組元素賦值,如果變量存在但不是數組,會先轉換成數組,再進行賦值

{$foo['bar']=1}
{$foo['bar']['blar']=1}
同上,可以給數組添加值

{$foo[]=1}
對象的屬性支持”.”操作符

{$foo.a.b.c}        =>  $foo['a']['b']['c']
{$foo.a.$b.c}       =>  $foo['a'][$b]['c']
{$foo.a.{$b+4}.c}   =>  $foo['a'][$b+4]['c']
{$foo.a.{$b.c}}     =>  $foo['a'][$b['c']]
變量名中支持變量

$foo         一個普通的變量
$foo_{$bar}  變量名中包含變量
$foo_{$x+$y} 變量名中可以支持表達式
$foo_{$bar}_buh_{$blar}  變量名包含多個變量
{$foo_{$x}}  如果$x是1,則輸出$foo_1
支持對象鏈,即是對象方法的連續調用,很像jquery

{$object->method1($x)->method2($y)}
{for}標籤支持類似loop一樣的循環

{for $x=0, $y=count($foo); $x<$y; $x++}  ....  {/for}
在FOR循環中可以通過如下特殊標示符限定位置:

$x@iteration  當前循環次數
$x@total     總循環次數
$x@first  循環第一次
$x@last     循環最後一次
新的foreach語法

{foreach $myarray as $var}...{/foreach}
同樣是foreach裏頭的特殊表示符,看的就明白,不翻譯了……

$var@key            foreach $var array key
$var@iteration      foreach current iteration count (1,2,3...)
$var@index          foreach current index count (0,1,2...)
$var@total          foreach $var array total
$var@first          true on first iteration
$var@last           true on last iteration
支持while循環

{while $foo}...{/while}
{while $x lt 10}...{/while}
可以直接使用PHP的函數

{time()}
新增加了一個{function}的標籤,可以定義一個可供調用的函數塊(我喜歡這功能,哈哈!)

{function}...{/function}
該標籤必須有一個name屬性,用來指名該函數名稱,也是調用的時候需要用到的

下面是一個例子

/* 定義一個函數 */
{function name=menu level=0}
    <ul>
    {foreach $data as $entry}
        {if is_array($entry)}
            <li>{$entry@key}</li>
            {menu data=$entry level=$level+1}
        {else}
            <li>{$entry}</li>
        {/if}
    {/foreach}
    </ul>
{/function}

/* 給函數傳遞的參數 */
{$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' =>
    ['item3-3-1','item3-3-2']],'item4']}

/* 調用那個函數 */
{menu data=$menu}
{function}功能函數必須有一個name屬性,並且可以擁有任意多個的其它屬性

代碼塊不緩存,可以使用{nocache}標籤默認是關閉的

{nocache} ... {/nocache}
還可以作爲屬性

{$foo nocache=true}
{$foo nocache}
{foo bar="baz" nocache=true}
{foo bar="baz" nocache}
{time() nocache=true}
{time() nocache}
返回當前模板的方法

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

在Smarty3中,可以自己定義的將變量存儲在主Smarty對象中,或者用戶自己定義的對象中,甚至是用戶自己的模板對象中

而且這些對象可以通過鏈式串接起來。

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

Smarty對象必須是鏈的根對象,但是對象鏈卻是可以獨立於Smarty對象存在的

所有的Smarty的賦值方法都可以用在data對象或者模板對象

除了上面說幾個方面,全局變量還有一種特殊的存儲方式

一個Smarty的數據對象(data Object)可以通過如下方式創建

$data = $smarty->createData();    // 創建根數據對象
$data->assign('foo','bar');       // 賦值操作
$data->config_load('my.conf');      // 加載配置文件

$data = $smarty->createData($smarty);   // 以Smarty作爲父對象,創建數據對象

$data2= $smarty->createData($data);     // 以data作爲父對象,創建數據對象data2
創建一個模板對象(template object) 可以通過createTemplate方法,它的參數傳遞和fetch()/display()方法一致

函數定義方式

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

$tpl = $smarty->createTemplate('mytpl.tpl'); // 創建一個模板對象,沒有父對象
$tpl->assign('foo','bar');                   // directly assign variables
$tpl->config_load('my.conf');

$tpl = $smarty->createTemplate('mytpl.tpl',$smarty); // 以Smarty爲父對象,創建模板對象
fetch()/display() 兩個方法將隱式的創建一個模板對象

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

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

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

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

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

{assign var=foo value='bar'}       // no scope is specified, the default 'local'
{$foo='bar'}                       // same, local scope
{assign var=foo value='bar' scope='local'} // same, local scope
{assign var=foo value='bar' scope='parent'} // Values will be available to the parent object
{$foo='bar' scope='parent'}                 // (normally the calling template)
{assign var=foo value='bar' scope='root'}   // Values will be exported up to the root object, so they can
{$foo='bar' scope='root'}                   // be seen from all templates using the same root.
{assign var=foo value='bar' scope='global'} // Values will be exported to global variable storage,
{$foo='bar' scope='global'}
擴展
Smarty3的擴展都是繼承至Smarty – Internal – PluginBase的類

所有的擴展都包含一個Smarty對象實例的$this->smarty屬性

模板繼承
你可以在模板中寫{block} … {/block}快,並且這些塊可以在子模板中進行覆蓋

parent.tpl:

<html>
    <head>
        <title>{block name='title'}My site name{/block}</title>
    </head>
    <body>
        <h1>{block name='page-title'}Default page title{/block}</h1>
        <div id="content">
            {block name='content'}
            Default content
            {/block}
        </div>
    </body>
</html>
child.tpl:

{extends file='parent.tpl'}

{block name='title'}
    Child title
{/block}
grandchild.tpl:

{extends file='child.tpl'}

{block name='title'}Home - {$smarty.block.parent}{/block}
{block name='page-title'}My home{/block}
{block name='content'}
    {foreach $images as $img}
        <img src="{$img.url}" alt="{$img.description}" />
    {/foreach}
{/block}
可以通過extends標籤來指定被繼承的模板,並在子模板中通過重寫父模板的同名block塊,達到覆蓋的目的

同時,可以通過{$smarty.block,parent}獲取到父block的內容

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

<html>
    <head>
        <title>Home - Child title</title>
    </head>
    <body>
        <h1>My home</h1>
        <div id="content">
            <img src="/example.jpg" alt="image" />
            <img src="/example2.jpg" alt="image" />
            <img src="/example3.jpg" alt="image" />
        </div>
    </body>
</html>
注意,在子模板中,所有在{block} … {/block}之外的內容都將被忽略

這種繼承支持多文件,多重繼承,意味着可以無線的繼承下去

還可通過{block}的append和prepend屬性來插入父模板結構中

PHP 流
待補充…

變量過濾
待補充…

PHP 模板
對於那些希望在模板中純粹寫PHP的人員來說,Smarty提供了一個php的選項,純PHP和有以下幾個不同的地方:

1.PHP模板將不進行編譯,直接被引擎調用
2.PHP模板將不具備任何安全屬性
3.Smarty默認不開啓PHP模板,可以$smarty->allow_php_templates=true來打開
如果你想使用php模板,只需要使用php資源類型

$smarty->display('php:foo.php');
你還可以在模板裏頭混合着用

{include file="php:foo.php"}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章