1.基本语法

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

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

2.注释

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

3.函数

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

例 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}

4.属性

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

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

例 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>

5.双引号里值的嵌入

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

例 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

6.变量

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

1)从PHP分配的变量

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

例 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}. OUTPUT: Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.

2)关联数组变量

Example 4-2. accessing associative array variables

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}<br>
{$Contacts.email}<br>
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}<br>
{$Contacts.phone.cell}<br> OUTPUT: 555-222-9876<br>
zaphod@slartibartfast.com<br>
555-444-3333<br>
555-111-1234<br>

最新文章

  1. java之多线程 二
  2. Mac 使用 SSH 免密连接服务器
  3. Maven学习(三)生命周期
  4. Clr Via C#读书笔记---垃圾回收机制
  5. Leetcode#68 Text Justification
  6. Codeforces Round #334 (Div. 1) C. Lieges of Legendre
  7. winform异型不规则界面设计
  8. java_监控工具jvisualvm
  9. 基于ABP框架的权限设置
  10. 表单验证控件Verify.js
  11. linux2.6.37内核接两个硬盘导致读写效率变低的问题
  12. 由Redis的hGetAll函数所引发的一次服务宕机事件
  13. 【学习总结】GirlsInAI ML-diary day-12-for循环
  14. 金融量化分析【day111】:Pandas-分组与聚合
  15. ROM、RAM、CPU、CACHE、FLASH的区别
  16. SSH密钥对登录的原理和实践
  17. centos7下安装配置redis
  18. Unity编辑器中分割线拖拽的实现
  19. Chrome插件 postman的使用方法详解!最全面的教程
  20. Linux gcc/g++链接编译顺序详解

热门文章

  1. 对java面试文章的技术漫谈的C#技术理解
  2. C# Sql 触发器
  3. oracle11g 新特性 - rman自动备份控制文件延迟
  4. netlink
  5. PHP调用WEBSERVICE接口常见问题答疑以及总结
  6. [转]VC的DDK编译环境构建
  7. 14.quartus联合modelsim仿真
  8. Android Jni变量对照表
  9. ASP.NET中Global.asax 文件是什么?
  10. NSNumber、NSValue、NSDate、NSObject