Smarty学习
1 基本语法
1.1 注释
{* this is a comment *}
1.2 函数
{funcname attr1="val" attr2="val"}.
1.3 双引号
{include file="subdir/$tpl_name.tpl"} <-- will replace $tpl_name with value {cycle values="one,two,`$smarty.config.myval`"} <-- must have backticks 1.4 数学计算 可以直接进行, {$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"} {assign var="foo" value="`$foo+$bar`"} 2 变量 2.1 简单变量 .php中assign的变量可以直接如下方式调用: Hello {$firstname}, glad to see you could make it. 也可以如下方式进行打印: {$Name} {$Contacts[row].Phone}
2.2 关联数组
利用关键字arry来定义关联数组。
index.php:
$smarty = new Smarty;
$smarty->assign('Contacts',
array('fax' => '555-222-9876',
'email' => 'zaphod@slartibartfast.com',
'phone' => array('home' => '555-444-3333',
'cell' => '555-111-1234')));
$smarty->display('index.tpl');
index.tpl:
{$Contacts.fax}
{$Contacts.email}
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}
{$Contacts.phone.cell}
输出结果:
555-222-9876
zaphod@slartibartfast.com
555-444-3333
555-111-1234
也可以利用关联数组的索引来访问:
{$Contacts[0]}
{$Contacts[1]}
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}
{$Contacts[2][1]}
2.3 从配置文件中读取的变量
{config_load file="foo.conf"}
这种方式导入的配置文件中的变量可以通过如下两种方式调用:
利用$smarty.config.调用或者{#pageTitle#}
2.4 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}
当前时间
{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
{$smarty.const._MY_CONST_VAL}
{$smarty.capture}
{$smarty.config}
{$smarty.section}, {$smarty.foreach}
{$smarty.template}
3 变量调节器
举例
{* Uppercase the title *}
{$title|upper}
{* 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}:返回article数组的个数。
而{$articleTitle|count}标示对数组中的每个元素进行count操作。
Capitalize:首字大写
{$articleTitle|count_characters}:不计算空格计数
{$articleTitle|count_characters:true}:计算空格计数
{$articleTitle|cat:" yesterday."}:将yesterday链接到变量后面。
count_paragraphs[计算段数]
count_sentences[计算句数]
count_words[计算词数]
{$smarty.now|date_format:"%A, %B %e, %Y"},具体含义参考strftime()
default[默认值]:变量为空时的默认值
escape[编码],html,htmlall,url,quotes,hex,hexentity,javascript
举例:
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' < > *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
indent[缩进]
lower
nl2br
regex_replace
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}
replace
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
spacify——插空
在字符串的每个字符之间插入空格或者其他的字符(串)
{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
string_format
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
参考sprintf.
strip
{$articleTitle|strip}
{$articleTitle|strip:" "}:将空格替换为
strip_tags
去除html标签
truncate
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
默认情况下,smarty会截取到一个词的末尾。
如果你想要精确的截取多少个字符,把第三个参数改为"true"
upper
大写
wordwrap
行宽约束
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"
\n"}
{$articleTitle|wordwrap:30:"\n":true}
