zoukankan      html  css  js  c++  java
  • Smarty模板学习

    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>
  • 相关阅读:
    Windows 配置 allure report 环境
    ruby 异常处理 begin rescue end
    ruby 安装 mysql2 命令
    Linux 新建文件/文件夹,删除文件文件夹,查找文件 打开文件
    Ubuntu 共享 转载
    单元测试框架 unittest 的运行方法if __name__ == '__main__': unittest.main()
    Python 字典和json的本质区别(个人理解)

    接口自动化大致流程。
    iOS开发UI篇—CAlayer(自定义layer)
  • 原文地址:https://www.cnblogs.com/weekend001/p/3730584.html
Copyright © 2011-2022 走看看