Smarty3和Smarty2的区别

Smarty3引入了许多改进,包括更一致的API结构、增强的表达式支持、新的模板标签和功能等,让模板引擎更为强大且易于使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) 增加模板继承功能
基于数据挖掘的音乐推荐系统设计实现 需要一个代码说明,不需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值