搜索墙首页  编程技术  java  asp  ajax  php  c/c#/c++  数据库  oracle  mysql  db2  操作系统  windows  linux  股吧
首页 > 编程 > PHP
 1         
1楼  qbq 2010-04-02

例5: 使用模板内置流程控制语句进行一行多单元格内容输出, 也就是在视觉上smarty每记输出几条记录:
  ================================================
  example5.tpl
  ================================================
  <html>
    <head><title>一行输出多条记录</title></head>
    <body>
       <table>
    <tr>
            {section name=loop loop=$News step=1}
            {if $smarty.section.loop.index % 4 == 0}
               </tr>
               <tr>
            {/if}
       <td>{$News[loop].newsID}</td>
       <td>{$News[loop].newsTitle}</td>
         {/section}
       </tr>
      </table>
    </body>
  </html>

  ====================================================
  example5.php
  ====================================================
<?php
     /*********************************************
      *
      * 文件名: example5.php
      * 作 用: 显示实例程序5
      *
      * 作 者: 大师兄
      * Email: [email]teacherli@163.com[/email]
      *
      *********************************************/

  
  require_once ("./comm/Smarty.class.php");

  $smarty = new Smarty();
  $smarty->template_dir = './templates/';
  $smarty->compile_dir = './templates_c/';
  $smarty->config_dir = './configs/';
  $smarty->cache_dir = './cache/';
  $smarty->caching = false;

  $array[]= array("newsID"=>"001", "newsTitle"=>"第1条新闻");
  $array[]= array("newsID"=>"002", "newsTitle"=>"第2条新闻");
  $array[]= array("newsID"=>"003", "newsTitle"=>"第3条新闻");
  $array[]= array("newsID"=>"004", "newsTitle"=>"第4条新闻");
  $array[]= array("newsID"=>"005", "newsTitle"=>"第5条新闻");
  $array[]= array("newsID"=>"006", "newsTitle"=>"第6条新闻");
  $array[]= array("newsID"=>"007", "newsTitle"=>"第7条新闻");
  $array[]= array("newsID"=>"008", "newsTitle"=>"第8条新闻");


  $smarty->assign("News", $array);

  $smarty->display("example5.tpl");
?>

==================================================
example5.php输出内容:
==================================================
   <html>
    <head><title>一行输出多条记录</title></head>
    <body>
       <table>
    <tr>
                
                           </tr>
               <tr>
                   <td>001</td>
       <td>第1条新闻</td>
             
                   <td>002</td>
       <td>第2条新闻</td>
             
                   <td>003</td>
       <td>第3条新闻</td>
             
                   <td>004</td>
       <td>第4条新闻</td>
             
               </tr>
               <tr>
                   <td>005</td>
       <td>第5条新闻</td>
             
                   <td>006</td>
       <td>第6条新闻</td>
             
                   <td>007</td>
       <td>第7条新闻</td>
             
                   <td>008</td>
       <td>第8条新闻</td>
                </tr>
      </table>
    </body>
  </html>

     说明:本来还可以优化,使得第一行不输出一个空行的<tr> </tr>,但是学习程序,简单为好,先就这么用了. 在这里说明一下:
         {section name=loop loop=$News step=1}
            {if $smarty.section.loop.index % 4 == 0}
               </tr>
               <tr>
            {/if}
               <td>{$News[loop].newsID}</td>
               <td>{$News[loop].newsTitle}</td>
          {/section}

     {section}{/section}指的是一个循环部分,在下一节会有详细的介绍,我们主要来看看这一句:
         {if $smarty.section.loop.index % 4 == 0}
     $smarty.section.loop指出$smarty的实例中的section段有一个叫loop的部分, 它有一个属性叫index, 它的表示当前循环的索引值,从0开始递增,我们把它%4后与0相比较,也就是说,如果当前的索引值是4的倍数,它就输出一个</tr><tr>,否则执行下面的部分,
     很简单的就解决了一个在程序上实现起来很麻烦的事情.这里我仅演示的是如何使用{if}语句功能,实现这个功能在Smarty的模板中还有一个非常方便的办法:{cycle},使用的例子如下所示:
         =================================
         {section name=rows loop=$data}
       <tr bgcolor="{cycle values="#D4D0C8,#EEEEEE"}">
         <td>{$data[rows]}</td>
       </tr>
      {/section}
         =================================


 1         
您的发言将按有关规定都会存档,您须为所发表后果负责,请您遵纪守法并注意语言文明。
标题:smarty实例教程---模板设计部分(2)
热门关注
标题回复点击
三 php与XML、XSLT、Mysql的结合运用,下载篇0455
php 学习的连接整理0384
无法加载 mcrypt 扩展,请检查您的 PHP 配置。7290
源码安装PHP5.3.00272
php 日记(资料来源:internet )1266
eclipse 插件net.sf.simpletest.eclipse 无法装入类1264
为Fedora Linux 上PHP添加OCI 支持0260
php & apache & mysql安装配置软件与文档0252
云动力发布Magento最新版本简体中文语言包0249
Warning: mysql_num_rows(): supplied argument3240
搜索墙@2009 www.pkwall.com all rights reserved QQ:276471788 [京ICP备09111534号]
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源