• smarty中ifelse、foreach以及获取数组中键值名的一个实例


    <{if empty($history)}>
      <tr>
        <td colspan="6">Not any records!</td>
      </tr>
    <{else}>
      <{foreach from=$history item=item}>
        <tr>
          <td><{$item.id}></td>
          <td>
            <div style="max-700px;overflow:hidden;text-overflow:ellipsis; ">
            <a href="#">
            Keywords:<{$item.keywords}><br>
            Size:<{$item.size}><br>
            <{foreach from=$item.options key=key item=option}>
              <{$key}>:<{$option}><br>
            <{/foreach}>
          </td>
          <td><{$item.num}></td>
          <td><{$item.create_time}></td>
          <td><{$item.last_process}></td>
          <td>
            <span class="label 
              <{if $item.status eq 'new'}>
                    label-primary
              <{else}>
                    label-success
              <{/if}>">
              <{$item.status}>
            </span>
          </td>
          <td>
            <a href="#" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>Delete</a>
            <a href="#" class="btn btn-primary btn-xs"><i class="fa fa-file-text"></i>Report</a>
          </td>
        </tr>
      <{/foreach}>
    <{/if}>

    这是我在项目开发中使用的smarty的一个例子,所以扣下来以作记录。以下为讲解:

    分四种颜色,对应两种语句(ifelse,foreach),其中$history为二维数组:

    1、黄绿色、橙色,if else语句的使用,及empty()函数,eq的使用

    2、蓝色、红色,foreach语句的使用,其中from和item必须的两个参数,from表示循环访问的数组,item当前元素的变量名。还有两个参数,name和key,name用于访问foreach属性的foreach循环的变量名,key就是数组中元素对应的键值名。

  • 相关阅读:
    HDU 4628 Pieces
    HDU 2983 Integer Transmission
    HDU 1820 Little Bishops
    CodeForces 165E Compatible Numbers
    CodeForces 11D A Simple Task
    HDU 4804 Campus Design
    HDU 3182 Hamburger Magi
    Linux的用户和组
    Linux文件/目录权限及归属
    Vim使用介绍
  • 原文地址:https://www.cnblogs.com/yxpblog/p/4677496.html
Copyright © 2020-2023  润新知