• smarty模版foreach和section


    1,先写一个简单的模版赋值

    index.php  页面

    <?php
    $root=str_replace("\\","/",dirname(__FILE__));
    define("ROOT",$root."/");
    define("DOMAIN","http://localhost/testSmarty");
    require(ROOT."smarty/Smarty.class.php");//引入smarty类
    $smarty=new Smarty();
    $smarty->template_dir=ROOT."demo/templates/";//设置模版路径
    $smarty->compile_dir=ROOT."demo/templates_c/";//设置编译路径
    $smarty->use_sub_dirs=false;
    $smarty->left_delimiter="<{";//设置定界符
    $smarty->right_delimiter="}>";
    $smarty->caching="1";
    $smarty->cache_dir=ROOT."demo/caches/";//缓存路径
    $smarty->cache_lifetime=3*24*(60*60);//0
    
    $smarty->assign("title", "这是文章的title3,将为你展示smarty模板技术");
    
    $smarty->assign("content", "这是文章的内容,将通过详细的安装使用步骤为你展示smarty模板技术");
    
    $smarty->display("index.html");
    ?>

    demo/templates/index.html  页面

    这是标题:<{$title}>
    <br/>
    这是内容:<{$content}>

    2,写一个数组操作

    <?php
    $root=str_replace("\\","/",dirname(__FILE__));
    define("ROOT",$root."/");
    define("DOMAIN","http://localhost/testSmarty");
    
    require(ROOT."smarty/Smarty.class.php");
    $smarty=new Smarty();
    $smarty->template_dir=ROOT."demo/templates/";
    $smarty->compile_dir=ROOT."demo/templates_c/";
    $smarty->use_sub_dirs=false;
    $smarty->left_delimiter="<{";
    $smarty->right_delimiter="}>";
    $smarty->caching="1";
    $smarty->cache_dir=ROOT."demo/caches/";
    $smarty->cache_lifetime=3*24*(60*60);//0
    
    $array=array("111112","2222222","33333333");
    $smarty->assign("onearray",$array);
    $smarty->display("array.html");
    ?>

    模版文件:array.html

    <{foreach from=$onearray  item=item key=key2}>
    下标:<{$key2}>=>值:<{$item}><br/>
    <{/foreach}>
    <hr/>
    
    <{section name=n loop=$onearray}>
    值:<{$onearray[n]}><br/>
    <{/section}>

    3,关联数组

    $arr = array(
             array('id'=>"a",'title'=>'title1'),
             array('id'=>"b",'title'=>'title2'),
             array('id'=>"c",'title'=>'title3')
         );
    $smarty->assign("onearray",$arr);
    
    $smarty->display("section.html");

    模版文件: section.html

    <{section name=n loop=$onearray}>
        <{if $smarty.section.n.first}>
        这是我的第一次循环<br/>
        <{/if}>
        key为:<{$onearray[n].id}>
        值为:<{$onearray[n].title}><br/>
        <{if $smarty.section.n.last}>
        这是我的最后一次循环
        <{/if}>
    
    <{/section}>
    <hr/>
    <{section name=sn loop=$onearray}>
         <{if $smarty.section.sn.first}>
             <table>
             <th>id</th>
             <th>title</th>
        <{/if}>
         <tr>
             <td><{$onearray[sn].id}></td>
             <td><{$onearray[sn].title}></td>
         </tr>
         <{if $smarty.section.sn.last}>
             </table>
         <{/if}>
    <{sectionelse}>
         there is no news.
     <{/section}>
        

    可以看出,无论是索引还是关联数组用起来都是很方便。下面介绍下section中各个属性的说明:

    1、section中的属性

    name:(必选) 是section循环的名称只是标示循环唯一的名字没有特别意义,前面没有$符号;

    loop: (必选)是在php声明中的变量名称,用来标示是循环哪一个数组(即要循环数组名)需要使用$;

    start: (可选)循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起. 例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5. 非法值(超过了循环数组的下限)将被自动调整为最接近的合法值.

    step: (可选)如其它语言的循环,是一个步长,如果为负数,则倒序循环;

    max:(可选)循环的最大下标,如果是1则只循环1次,如果为2则循环2次;

    show:(可选)默认为true即显示。如果设置了{sectionelse}。表示如果数组没有内容的时候显示这部分的内容;如果show为false则显示这部分。如果没有设置{sectionelse}则不输出该数组。

    2、smarty中section中的变量

    index:用于显示当前循环的索引,从0开始(如果指定了start属性,那么由该值开始),每次加1(如果指定了step属性,那么由该值决定).如果没有指定step和start属性,此值的作用和iteration类似,只不过从0开始而已.

    index_prev:用于显示上一个循环索引值. 循环开始时,此值为-1.

    index_next:用于显示下一个循环索引值. 循环执行到最后一次时,此值仍然比当前索引值大1(如果指定了step,取决于此值).

    iteration:用于显示循环的次数.iteration 不像index属性受start、step和max属性的影响,该值总是从1开始(index是从0开始的).rownum 是iteration的别名,两者等同.

    first:如果当前循环第一次执行,first 被设置为true.

    last:如果当前循环执行到最后一次,last 被设置为true.

    rownum:用于显示循环的次数. 该属性是iteration的别名,两者等同.

    loop:用于显示该循环上一次循环时的索引值. 该值可以用于循环内部或循环结束后.

    show:是 section 的参数. show 取值为布尔值 true 或 false. 如果设置为 false,该循环将不显示. 如果指定了 sectionelse 子句,该字句是否显示也取决于该值.

    total:用于显示循环执行总的次数. 可以在循环中或执行结束后调用此属性. 

    千里之行,始于足下。改变现在,就是改变未来。改变未来,从现在开始。 个人网站:http://www.wangkongming.cn
  • 相关阅读:
    基于udp简单聊天的系统
    网络编程_tcp与dup协议简单应用
    logging_modules
    linux内核配置与编译
    linux内核简介
    对于国嵌上学期《一跃进入C大门》Mini2440的代码修正
    通过按键玩中断
    MMU功能解析、深入剖析、配置与使用
    C与汇编混合编程
    一跃进入C大门
  • 原文地址:https://www.cnblogs.com/wangkongming/p/3003149.html
Copyright © 2020-2023  润新知