什么是 Smarty?
为何考虑使用?
用例和工作流
语法对比
模板继承
最佳实践
速成教程
模板变量以 $ 美元符号开头。它们可以包含数字、字母和下划线,与PHP 变量非常相似。您可以通过索引按数字或非数字方式引用数组。还可以引用对象属性和方法。
配置文件变量是 $ 美元符号语法的一个例外,而是使用围绕的 # 井号 # 进行引用,或通过 $smarty.config
变量引用。
示例 3.2 变量
{$foo} <-- displaying a simple variable (non array/object) {$foo[4]} <-- display the 5th element of a zero-indexed array {$foo.bar} <-- display the "bar" key value of an array, similar to PHP $foo['bar'] {$foo.$bar} <-- display variable key value of an array, similar to PHP $foo[$bar] {$foo->bar} <-- display the object property "bar" {$foo->bar()} <-- display the return value of object method "bar" {#foo#} <-- display the config file variable "foo" {$smarty.config.foo} <-- synonym for {#foo#} {$foo[bar]} <-- syntax only valid in a section loop, see {section} {assign var=foo value='baa'}{$foo} <-- displays "baa", see {assign} Many other combinations are allowed {$foo.bar.baz} {$foo.$bar.$baz} {$foo[4].baz} {$foo[4].$baz} {$foo.bar.baz[4]} {$foo->bar($baz,2,$bar)} <-- passing parameters {"foo"} <-- static values are allowed {* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*} {$smarty.server.SERVER_NAME} Math and embedding tags: {$x+$y} // will output the sum of x and y. {assign var=foo value=$x+$y} // in attributes {$foo[$x+3]} // as array index {$foo={counter}+3} // tags within tags {$foo="this is message {counter}"} // tags within double quoted strings Defining Arrays: {assign var=foo value=[1,2,3]} {assign var=foo value=['y'=>'yellow','b'=>'blue']} {assign var=foo value=[1,[9,8],3]} // can be nested Short variable assignment: {$foo=$bar+2} {$foo = strlen($bar)} // function in assignment {$foo = myfunct( ($x+$y)*3 )} // as function parameter {$foo.bar=1} // assign to specific array element {$foo.bar.baz=1} {$foo[]=1} // appending to an array Smarty "dot" syntax (note: embedded {} are used to address ambiguities): {$foo.a.b.c} => $foo['a']['b']['c'] {$foo.a.$b.c} => $foo['a'][$b]['c'] // with variable index {$foo.a.{$b+4}.c} => $foo['a'][$b+4]['c'] // with expression as index {$foo.a.{$b.c}} => $foo['a'][$b['c']] // with nested index PHP-like syntax, alternative to "dot" syntax: {$foo[1]} // normal access {$foo['bar']} {$foo['bar'][1]} {$foo[$x+$x]} // index may contain any expression {$foo[$bar[1]]} // nested index {$foo[section_name]} // smarty {section} access, not array access! Variable variables: $foo // normal variable $foo_{$bar} // variable name containing other variable $foo_{$x+$y} // variable name containing expressions $foo_{$bar}_buh_{$blar} // variable name with multiple segments {$foo_{$x}} // will output the variable $foo_1 if $x has a value of 1. Object chaining: {$object->method1($x)->method2($y)} Direct PHP function access: {time()}
尽管 Smarty 可以处理一些非常复杂的表达式和语法,但遵循经验法则保持模板语法最小化并专注于展示是个好习惯。如果你发现你的模板语法变得过于复杂,可能需要通过插件或修饰符将与展示无关的部分移到 PHP。
诸如 $_GET
、$_SESSION
等请求变量,都可以通过保留的 $smarty
变量使用。