zoukankan      html  css  js  c++  java
  • smarty 教程 及 常用点

    1. 简单例子 有助回忆基本知识点

     1 define("DIR",dirname(__FILE__));
     2 require_once(DIR."/libs/Smarty.class.php");//引用smarty相对路径
     3 
     4 $smarty = new Smarty();
     5 $smarty->template_dir(DIR."/templates");//模板路径
     6 $smarty->complie_dir(DIR."/templates_c");//编译模板的路径
     7 $smarty->cache_dir(DIR."/cache");//smarty 缓存目录
     8 $smarty->caching = true; //true启用缓存  false禁止使用缓存
     9 $smarty->cache_lifetime=3600*24;/缓存页面24小时
    10 $smarty->left_delimiter="<{";
    11 $smarty->right_delimiter="}>";//左右分隔符

    以上变量均可以在Smarty.class.php中找到,可以在那里进行全局设置。 也可以对某个模板如主页index.html进行单独配置。
    12 13 $smarty->assign("xx", "xxx");//分配smarty变量,编译模板进行解析 14 $smarty->display("index.html");//显示解析后的模板

    2.重点foreach使用,多级嵌套伪代码,文章分页列表页:page   title date 分别表示:页码, 文章标题,发布时间

    index.php

    $article[0] = array("我是第1篇文章的标题"=>2011,"我是第2篇文章的标题"=>2011,"我是第3篇文章的标题"=>2011); 
    $article[1] = array("我是第4篇文章的标题"=>2012,"我是第5篇文章的标题"=>2012,"我是第6篇文章的标题"=>2012); 
    $article[2] = array("我是第7篇文章的标题"=>2013,"我是第8篇文章的标题"=>2013,"我是第9篇文章的标题"=>2013); 
    $smarty->assign("article", $article);
    $smarty->display("index.html");

    index.html

    {foreach from=$article key=page item=title_date}
    {$page}页有以下这些文章:
    
    {foreach from=$title_date key=title item=date}
    文章标题:{$title}; 发布时间:{$date}
    {/foreach}
    
    {foreachelse}
    此{$page}无文章
    {/foreach}

    问题1:

    通常,使用循环时候是为了得到value值,所以,经常看到省略写法也是正确的。{foreach from=$article item=value} 。但是,此处需要用到key值。     

    3.重点section 使用

    {section}块具有的属性值,分别为:

    1. index: 上边我们介绍的"循环下标",默认为0

    2. index_prev: 当前下标的前一个值,默认为-1

    3. index_next: 当前下标的下一个值,默认为1

    4. first: 是否为第一下循环

    5. last: 是否为最后一个循环

    6. iteration: 循环次数

    7. rownum: 当前的行号,iteration的另一个别名

    8. loop: 最后一个循环号,可用在section块后统计section的循环次数

    9. total: 循环次数,可用在section块后统计循环次数

    10. show: 在函数的声明中有它,用于判断section是否显示

    index 属性演示
    {section name=customer loop=$custid}
        {$smarty.section.customer.index} id: {$custid[customer]}<br>
        {/section}
    
        OUTPUT:
        0 id: 1000<br>
        1 id: 1001<br>
        2 id: 1002<br> 
    
    
    index_prev 属性演示
    {section name=customer loop=$custid}
        {$smarty.section.customer.index} id: {$custid[customer]}<br>
        {* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
        {if $custid[customer.index_prev] ne $custid[customer.index]}
         The customer id changed<br>
        {/if}
        {/section}
    
        OUTPUT:
        0 id: 1000<br>
         The customer id changed<br>
        1 id: 1001<br>
         The customer id changed<br>
        2 id: 1002<br>
         The customer id changed<br>
    
    
    iteration 属性演示
    iteration 不像index属性受start、step和max属性的影响,该值总是从1开始(index是从0开始的).rownum 是iteration的别名,两者等同.
    {section name=customer loop=$custid start=5 step=2}
        current loop iteration: {$smarty.section.customer.iteration}<br>
        {$smarty.section.customer.index} id: {$custid[customer]}<br>
        {* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
        {if $custid[customer.index_next] ne $custid[customer.index]}
         The customer id will change<br>
        {/if}
        {/section}
    
        OUTPUT:
        current loop iteration: 1
        5 id: 1000<br>
         The customer id will change<br>
        current loop iteration: 2
        7 id: 1001<br>
         The customer id will change<br>
        current loop iteration: 3
        9 id: 1002<br>
         The customer id will change<br>
    
    
    first 属性演示 
    如果当前循环第一次执行,first 被设置为true. 
    {section name=customer loop=$custid}
        {if $smarty.section.customer.first}
         <table>
        {/if}
    
        <tr><td>{$smarty.section.customer.index} id:
         {$custid[customer]}</td></tr>
    
        {if $smarty.section.customer.last}
         </table>
        {/if}
        {/section}
    
        OUTPUT:
        <table>
        <tr><td>0 id: 1000</td></tr>
        <tr><td>1 id: 1001</td></tr>
        <tr><td>2 id: 1002</td></tr>
        </table>
    
    
    遍历多维数组:
    {section name=customer loop=$contacts}
        name: {$contacts[customer].name}<br>
        home: {$contacts[customer].home}<br>
        cell: {$contacts[customer].cell}<br>
        e-mail: {$contacts[customer].email}<p>
    {/section}
    
    
    loop 用于显示该循环上一次循环时的索引值. 该值可以用于循环内部或循环结束后.
    {$smarty.section.customer.loop} 
    
    total 用于显示循环执行总的次数. 可以在循环中或执行结束后调用此属性. 
    {$smarty.section.customer.total}
    
    Section 循环 通过如下方式调用变量{$smarty.section.sectionname.varname}.
    View Code

    其他问题1: 关联数组索引数组对象属性   3种不同获取方式

    关联数组:

    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>

    索引数组:

    index.php:
    
    $smarty = new Smarty;
    $smarty->assign('Contacts',
     array('555-222-9876',
     'zaphod@slartibartfast.com',
     array('555-444-3333',
     '555-111-1234')));
    $smarty->display('index.tpl');
    
    index.tpl:
    
    {$Contacts[0]}<br>
    {$Contacts[1]}<br>
    {* you can print arrays of arrays as well *}
    {$Contacts[2][0]}<br>
    {$Contacts[2][1]}<br>

    对象属性获取:

    name: {$person->name}<br>
    email: {$person->email}<br>
    
    OUTPUT:
    
    name: Zaphod Beeblebrox<br>
    email: zaphod@slartibartfast.com<br>

    其他问题2:

    空白变量处理:

    {* 传统if else方法解决 *}
    
    {if $title eq ""}
        &nbsp;
    {else}
        {$title}
    {/if}
    
    
    {* 简洁方式 default *}
    
    {$title|default:"&nbsp;"}

    Smarty模板之间互相引用,值与值之间传递的方式:

     1 mainpage.tpl
     2 ------------
     3 
     4 {include file="header.tpl" title="Main Page"}
     5 {* template body goes here *}
     6 {include file="footer.tpl"}
     7 
     8 
     9 archives.tpl
    10 ------------
    11 
    12 {config_load file="archive_page.conf"}
    13 {include file="header.tpl" title=#archivePageTitle#}
    14 {* template body goes here *}
    15 {include file="footer.tpl"}
    16 
    17 
    18 header.tpl
    19 ----------
    20 <HTML>
    21 <HEAD>
    22 <TITLE>{$title|default:"BC News"}</TITLE>
    23 </HEAD>
    24 <BODY>
    25 
    26 
    27 footer.tpl
    28 ----------
    29 </BODY>
    30 </HTML>
    31 
    32 --------
    33 当这个主页[main page]被浏览的时候,‘Main Page'这个标题就会传递给头模板文件[header.tpl],并且结果会被用成为标题。当这个档案页[archives page]被浏览的时候,文件标题将会是‘Archives'。注意在这个档案页例子中,我们用了一个来自这个文件[archives_page.conf]的变量来代替一个硬性的代码变量。当然,要是变量[$title]没有初始化,我们会发现‘BC News'被输出----用那种使用默认值的变量的方法。
  • 相关阅读:
    【转载】Oracle实例和Oracle数据库(Oracle体系结构)
    【转载】Oracle 11g R2 for Win7旗舰版(64位)- 安装
    eclipse 中卸载插件的方法
    eclipse编辑jsp文件和javascript代码很卡解决办法
    ExtJS登陆页面涉及到的几个问题
    一个关于ExtJS4具体控件的详细教程
    ES6与React中this完全解惑
    for, for..in, in, for...of的区别
    Sublime Text3的react代码校验插件
    Sublime Text3中JSX支持Emmet快捷键
  • 原文地址:https://www.cnblogs.com/qunshu/p/3175651.html
Copyright © 2011-2022 走看看