Smarty3和Smarty2的區別

Smarty 3 API 的語法結構已經重構,使之更一致性和模塊化,雖然爲了向下兼容,仍然支持Smarty 2的語法,但會拋出一個被棄用的notice,雖然你可以屏蔽該notice,但強烈建議,在使用Smarty 3 時使用3的語法,一方面,Smarty 2的語法很可能在後面的版本中逐漸被取消,另一方面, Smarty2的語法,是對Smarty3的API的封裝,所以性能方面也會有損失。
? Smarty3與Smarty的差別之處
1) 基本上,Smarty3的方法採用駝峯式的命名方式,如 fooBarBaz
2) 所有Smarty的屬性都有get 和 set 的方法 如$smarty->cache_dir = ‘foo/’ 現在可以這樣賦值 $smarty->setCacheDir('foo/'),同樣可以通過 $smarty->getCacheDir() 來得到該屬性值
3) Smarty 3廢除了一些如 ”is*”的方法,因爲他們和現在的”get*”方法重複了
4) Smarty 3 只能在PHP5下運行,不支持PHP4.
5) {php} 標籤默認是關閉的. 使用$smarty->allow_php_tag=true.開啓
6) 被空白包圍的分隔符將不被解析,如{ foo }將不再作爲smarty標籤被解析,你必須使用{foo}

 

下面是Smarty3 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()

 

一些屬性的get 及 set方法
$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->debugging

 

Smarty3新增了不少功能和新特性,這些新特性,讓我們使用Smarty更加方便
1) Smarty3在幾乎所有地方都支持表達式,如果安全策略允許,表達式中甚至可以包含PHP函數,對象的方法及屬性,如
? {$x+$y}
? {$foo = strlen($bar)}
? {assign var=foo value= $x+$y}
? {$foo = myfunct( ($x+$y)*3 )}
? {$foo[$x+3]}
2) Smarty的標籤可以作爲其他標籤的值,如
? {$foo={counter}+3}
3) Smarty的標籤可以在雙引號中間使用,如
? {$foo="this is message {counter}"}
4) 你能夠在模板內部像在PHP中那樣使用數組,並能給數組中的單個元素賦值或給數組追加一個元素,如
? {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']=1}
? {$foo['bar']['blar']=1}
? Example: {$foo[]=1}

5) 模板變量名本身可以是一個表達式
? $foo_{$bar}
? $foo_{$x+$y}
? $foo_{$bar}_buh_{$blar}
? {$foo_{$x}}
6) 實現了對象的方法鏈,如
? {$object->method1($x)->method2($y)}
7) 增加了{for}標籤,替代{section}
? {for $x=0, $y=count($foo); $x<$y; $x++} .... {/for}
? {for $x = $start to $end step $step} ... {/for}
8) 在for循環的內部,你可以使用一面變量
? $x@iteration = number of iteration
? $x@total = total number of iterations
? $x@first = true on first iteration
? $x@last = true on last iteration
9) 像在PHP中那樣去使用foreach,如
{foreach $array as $key => $val}
{$key}=>{$val}
{/foreach}
【注意:如果使用數組下標,使用單引號括起來,避免和section的使用方法產生衝突】
10) 增加了while標籤
? {while $foo}...{/while}

{while $x lt 10}...{/while}
11) 增加了function標籤
12) 增加了{nocache}塊函數 及 nocache屬性來避免變量或函數被緩存,你也可以使用下面方法避免變量被緩存
? $smarty->assign('foo',$something,true);
13) 你可以直接使用字符串代替smarty模板,如
? $smarty->display('string:This is my template, {$foo}!');
? {include file="string:This is my template, {$foo}!"}
14) 增加模板繼承功能
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章