参考:http://hi.baidu.com/audislee/blog/item/930a101bb4fa3b098618bfb1.html

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循环的内部,你可以使用一面变量
?  = number of iteration
?  = total number of iterations
?  = true on first iteration
?  = 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) 增加模板继承功能

转载于:https://www.cnblogs.com/chyong168/archive/2011/11/20/2256074.html

smarty2和smarty3相关推荐

  1. Smarty2至Smarty3升级指南

    Smarty2和Smarty3在实现上差不多,但是在smarty2升级到smarty3的时候还是要注意些许不同. 下面是已知的和smarty2不兼容的地方 == 语法 == Smarty 3 API有 ...

  2. smarty2 php,Smarty2至Smarty3升级指南.

    Smarty2和Smarty3在告终上差不多,然而在smarty2升级到smarty3的时候还是要当心些许不同. 下面是已知的和smarty2不接受的地方 == 语法 == Smarty 3 API有 ...

  3. php smarty thinkphp,用新版的thinkphp3.2.3和新版的smarty3.1.29

    用新版的thinkphp3.2.3和新版的smarty3.1.29,配置smarty的自定义function插件,这样,在thinkphp的模板中就可以加载自定义的小功能块了. 先作个介绍,smart ...

  4. Smarty3——foreach

    foreach and  foreachelse篇 foreach用于遍历数组,可以是非关联数组,与section相比要简单些,在smarty3中可以接受没有名称的属性,也可以使用smarty2有名称 ...

  5. Smarty-3.1.12配置

    下载好后Smarty-stable-3.1.12解压缩会得到一个 Smarty-3.1.12 文件夹,里面有两个主要文件夹demo和libsdemo文件夹为示例文件夹,里面包含默认文件夹结构,是我们要 ...

  6. Smarty3 配置

    2019独角兽企业重金招聘Python工程师标准>>> 一.文件结构 二.配置文件 init.inc.php /** file: init.inc.php Smarty对象的实例化及 ...

  7. smarty课程---smarty3的安装和使用

    smarty课程---smarty3的安装和使用 一.总结 一句话总结:smarty 是什么,就不多说了,用过php,接触过php的人都对smarty 再熟悉不过了.它是一个很强大的代码分离软件,作为 ...

  8. ecshop小京东的模板切换到smarty3.1.3之去掉原生的php语法

    在小京东的模板中,随处可以看到 <?php$GLOBALS['smarty']->assign('index_image',get_advlist('首页-分类ID'.$GLOBALS[' ...

  9. ecshop小京东模板切换到smarty3.13之模板函数,用模板函数替换楼层

    ecshop小京东的模板中用到了楼层的概念,看下了模板每个楼层都写了一下,用几个楼层就写了几段,个人感觉这样的有点冗余,不如做个函数,直接循环一下 有两种方法,第一种是:写个函数来实现楼层的显示,第二 ...

最新文章

  1. 树莓派3 64linux,树莓派3 model b安装64位debian+qt5.9
  2. VC中实现GCC的2个比较常用的位运算函数
  3. getrealdays oracle_oracle 日期函数介绍
  4. 阿里为什么建议给MVC三层架构多加一层Manager层?
  5. MySQL条件运算符的使用
  6. LeetCode 399. 除法求值(图的DFS搜索)
  7. python-DBSCAN密度聚类
  8. vSphere 故障排除之工具篇
  9. java历save_日历【savejava吧】_百度贴吧
  10. 【Qt/C++异常笔记】间接寻址级别不同
  11. LeetCode 1429. First Unique Number
  12. 超详细的WMS仓储管理系统介绍——补货篇
  13. 性能测试流程-性能测试2
  14. oracle序列号、
  15. 【电路理论】KCL、KVL、线性直流电路各大方法、定理详解
  16. 原生革命--跨平台开发技术解析
  17. 信息系统安全等级保护基本要求——技术要求
  18. 功能测试(黑盒测试)
  19. 压缩包文件解压文件是需要密码
  20. linux进程及网络报告,linux网络和进程管理简述

热门文章

  1. 雷蛇键盘灯光配置文件_指尖的流光溢彩,雷蛇黑寡妇蜘蛛V3竞技版开箱
  2. 会了这些命令,还怕搞不定思科设备?
  3. 网工协议基础(1) OSI七层模型
  4. mysql 主键 uniqo_优衣库某处SQL注入可导致移动平台被劫持
  5. 2020 云原生技术 7 大领域趋势全预测
  6. 我的世界java有三叉戟杀手吗_我的世界-三叉戟竟能这么用 这样得怪物头颅长见识了!...
  7. 手机控制linux电脑,通过Amora用symbian手机控制linux系统的电脑
  8. 何不用python_人生几何,何不Python当歌
  9. python加载包出错_python导入Pmw包运行提示一下错误
  10. 亲测可用:Anaconda Windows Error:[Error 2]或者系统找不到指定文件