代码改变世界

smarty学习1

2011-08-16 11:11  呦菜  阅读(424)  评论(1编辑  收藏  举报

第三章.基本语法

Table of Contents[内容列表]

Comments[注释]

Functions[函数]

Attributes[属性]

Embedding Vars in Double Quotes[双引号里值的嵌入]

Math[数学运算]

所有的smarty模板标签都被加上了定界符。默认情况下是 { },但它们是可被改变的。

例如,我们假定你在使用默认定界符.
smarty,所有定界符以外的内容都是静态输出的,或者称之为不可改变.
smarty遇到了模板标签,将尝试解释他们,然后再以恰当的方式输出 .

Comments[注释]

模板注释被*号包围,例如 {* this is a comment *}
smarty
注释不会在模板文件的最后输出中出现.
它只是模板内在的注释.

Example 3-1. Comments

3-1.注释

{* Smarty *}
{* include the header file here *}
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{* display dropdown lists *}
<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>
 

Functions

函数

每一个smarty标签输出一个变量或者调用某种函数.
在定界符内 函数(用'{'包住)和其属性(用界符包住)将被处理和输出.例如:
{funcname attr1="val" attr2="val"}.

Example 3-2. function syntax

3-2.函数语法

{config_load file="colors.conf"}
{include file="header.tpl"}
{if $highlight_name}
        Welcome, <font color="{#fontColor#}">{$name}!</font>  
{else}
        Welcome, {$name}!
{/if}

{include file="footer.tpl"}

在模板里无论是内建函数还是自定义函数都有相同的语法.
内建函数将在smarty内部工作,例如 {if} , {section} and {strip} .他们不能被修改.
自定义函数通过插件机制起作用,它们是附加函数. 只要你喜欢,可以随意修改.你也可以自行添加.
例如 {html_options} {html_select_date}

Attributes

[属性]

大多数函数都带有自己的属性以便于明确说明或者修改他们的行为.
smarty
函数的属性很像HTML中的属性.
静态数值不需要加引号,但是字符串建议使用引号.
如果用变量作属性,它们也不能加引号.

一些属性用到了布尔值(真或假).
它们不需要加引号,可以是true,on,yes或者false,off,no.

Example 3-3. function attribute syntax

3-3.函数属性语法

{include file="header.tpl"}

{include file=$includeFile}

{include file=#includeFile#}

{html_select_date display_days=yes}

<SELECT name=company>

{html_options values=$vals selected=$selected output=$output}

</SELECT>

Embedding Vars in Double Quotes

[双引号里值的嵌入]

Smarty可以识别嵌入在双引号中的变量,只要此变量只包含数字、字母、下划线和中括号[].对于其他的符号(句号、对象相关的,等等)此变量必须用两个'`'(此符号和‘ ~ '在同一个键上,一般在ESC键下面一个键上)包住。

Example 3-4. embedded quotes syntax

3-4.双引号里值的嵌入语法

SYNTAX EXAMPLES:

{func var="test $foo test"} <-- sees $foo

{func var="test $foo_bar test"} <-- sees $foo_bar

{func var="test $foo[0] test"} <-- sees $foo[0]

{func var="test $foo[bar] test"} <-- sees $foo[bar]

{func var="test $foo.bar test"} <-- sees $foo (not $foo.bar)

{func var="test `$foo.bar` test"} <-- sees $foo.bar

 

PRACTICAL EXAMPLES:

{include file="subdir/$tpl_name.tpl"} <-- will replace $tpl_name with value

{cycle values="one,two,`$smarty.config.myval`"} <-- must have backticks

Chapter 4. Variables[第四章.变量]
第四章.变量

Table of Contents[内容列表]

Variables assigned from PHP[PHP分配的变量]

Variables loaded from config files[从配置文件读取的变量]

{$smarty} reserved variable[{$smarty} 保留变量]

 

 

Smarty有几种不同类型的变量.
变量 的类型取决于它的前缀是什么符号(或者被什么符号包围)

 

Smarty的变量可以直接被输出或者作为函数属性和修饰符(modifiers)的参数,或者用于内部的条件表达式等等.
如果要输出一个变量,只要用定界符将它括起来就可以.例如:

 

{$Name} 
 
{$Contacts[row].Phone}
 

<body bgcolor="{#bgcolor#}">

Variables assigned from PHP
PHP分配的变量

Table of Contents[内容列表]

Associative arrays[关联数组]

Array indexes[数组下标]

Objects[对象]

调用从PHP分配的变量需在前加"$"符号.(译注:同php一样)
调用模板内的assign函数分配的变量也是这样.(译注:也是用$加变量名来调用)

Example 4-1. assigned variables
Smarty
手册范例 4-1.分配的变量

index.php:


$smarty = new Smarty;
$smarty->assign('firstname', 'Doug');
$smarty->assign('lastLoginDate', 'January 11th, 2001');
$smarty->display('index.tpl');

index.tpl:
 
Hello {$firstname}, glad to see you could make it.
<p>
Your last login was on {$lastLoginDate}.
 
输出结果:
 
Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.

 

 

Variables loaded from config files
从配置文件读取的变量

配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到)
第二种语法在变量作为属性值并被引号括住的时候非常有用.
(译注:举个例子 {include file="#includefile#"} 这样#includefile#将被当作字符处理,而不表示配置文件变量,
    但可以这样表示 {include file="`$smarty.config.includefile`"}不要忘了加``

Example 4-5. config variables
Smarty
手册范例 4-5.从配置文件引用的变量

 

foo.conf:
 
pageTitle = "This is mine"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"
 
index.tpl:
 
{config_load file="foo.conf"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
        <td>First</td>
        <td>Last</td>
        <td>Address</td>
</tr>
</table>
</body>
</html>
 
index.tpl: (alternate syntax)
 
{config_load file="foo.conf"}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}">
<tr bgcolor="{$smarty.config.rowBgColor}">
        <td>First</td>
        <td>Last</td>
        <td>Address</td>
</tr>
</table>
</body>
</html>
 
 
输出结果: (same for both examples)
 
<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
        <td>First</td>
        <td>Last</td>
        <td>Address</td>
</tr>
</table>
</body>
</html>

配置文件的变量只有在它们被加载以后才能使用.
这个过程将在以后 {config_load} . 的章节里说明.

{$smarty} reserved variable
{$smarty}
保留变量

Table of Contents

Request variables[页面请求变量](译注:就是get,post,server,session等变量)

{$smarty.now}

{$smarty.const}

{$smarty.capture}

{$smarty.config}

{$smarty.section}, {$smarty.foreach}

{$smarty.template}

The reserved {$smarty}

{$smarty}保留变量可以被用于访问一些特殊的模板变量.
以下是全部列表:

{* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *}

{$smarty.get.page}

 

{* display the variable "page" from a form (POST) *}

{$smarty.post.page}

 

{* display the value of the cookie "username" *}

{$smarty.cookies.username}

 

{* display the server variable "SERVER_NAME" *}

{$smarty.server.SERVER_NAME}

 

{* display the system environment variable "PATH" *}

{$smarty.env.PATH}

 

{* display the php session variable "id" *}

{$smarty.session.id}

 

{* display the variable "username" from merged get/post/cookies/server/env *}

{$smarty.request.username}

Chapter 5. Variable Modifiers [变量调节器]

Table of Contents [内容列表]

变量调节器用于变量,自定义函数和字符串。请使用‘|’符号和调节器名称应用调节器。变量调节器由赋予的参数值决定其行为。参数由符号分开。

Example 5-1. modifier example
5-1.调节器的例子

{* Uppercase the title *}
 
<h2>{$title|upper}</h2>
 
{* Truncate the topic to 40 characters use ... at the end *}
Topic: {$topic|truncate:40:"..."}
 
{* format a literal string *}
{"now"|date_format:"%Y/%m/%d"}
 
{* apply modifier to a custom function *}

{mailto|upper address="me@domain.dom"}

如果你给数组变量应用单值变量的调节,结果是数组的每个值都被调节。如果你只想要调节器用一个值调节整个数组,你必须在调节器名字前加上@符号。例如: {$articleTitle|@count}(这将会在 $articleTitle 数组里输出元素的数目)

 

This is used to capitalize the first letter of all words in a variable.
将变量里的所有单词首字大写。

Example 5-2. capitalize
5-2.首字大写

This is used to capitalize the first letter of all words in a variable.
将变量里的所有单词首字大写。

Example 5-2. capitalize
5-2.首字大写

index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle}
{$articleTitle|capitalize}
 
输出结果:
 
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.

 转自:http://wenku.baidu.com/view/e6ab4562caaedd3383c4d32b.html