Smarty3手册 联系客服

发布时间 : 星期五 文章Smarty3手册更新完毕开始阅读80abdced5ef7ba0d4a733b3f

Smarty3.0 帮助手册

这里大概解读一下附带的README文件

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=\ 可以在模板里头定义数组 {assign var=foo value=[1,2,3]} {assign var=foo value=['y'=>'yellow','b'=>'blue']} {assign var=foo value=[1,[9,8],3]} 简单的变量赋值