Smarty3和Smarty2的区别

来源:互联网 发布:钓炸天软件生成器下载 编辑:程序博客网 时间:2024/05/02 05:05
Smarty 3 API 的语法结构已经重构,使之更一致性和模块化,虽然为了向下兼容,仍然支持Smarty 2的语法,但会抛出一个被弃用的notice,虽然你可以屏蔽该notice,但强烈建议,在使用Smarty 3 时使用3的语法,一方面,Smarty 2的语法很可能在后面的版本中逐渐被取消,另一方面, Smarty2的语法,是对Smarty3的API的封装,所以性能方面也会有损失。? Smarty3与Smarty的差别之处1) 基本上,Smarty3的方法采用驼峰式的命名方式,如 fooBarBaz2) 所有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 iteration9) 像在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) 增加模板继承功能