The DoYouHaoBaby PHP Framework中文开发文档手册
Copyright(c) DoYouHaoBaby.NET All Rights Reserved.
[ Iterate迭代器 ]

如果我们需要在模板中使用迭代器,那么通过iterate标签可以很方便地输出。


1:最简单的一个迭代器

我们在模板中写下如下的代码,生成迭代器:

  1. <php>   
  2.      $arrList = array(   
  3.          'title'=>'The DoYouHaoBaby PHP Framework',   
  4.          'time'=>'2010-2011',   
  5.          'copyright'=>'The DoYouHaoBaby Company !',   
  6.      );   
  7.      $oMyIterator = new DArrayIterator( $arrList );   
  8. </php>   
  9.   
  10. <iterate iterator='oMyIterator'>   
  11.     迭代啊 - The DoYouHaoBaby Framework !<br>   
  12. </iterate>  


程序运行后的编译PHP 代码如下:

  1. <?php !defined('DYHB_PATH') && exit; /* DoYouHaoBaby Framework 模板缓存文件 生成时间:2002-01-12 22:36:28  */ ?>   
  2. <?php    
  3.      $arrList = array(   
  4.          'title'=>'The DoYouHaoBaby PHP Framework',   
  5.          'time'=>'2010-2011',   
  6.          'copyright'=>'The DoYouHaoBaby Company !',   
  7.      );   
  8.      $oMyIterator = new DArrayIterator( $arrList );   
  9.  ?>   
  10.   
  11. <?php $oMyIterator->first() ;   
  12. while( !$oMyIterator->isDone() ):  ?>   
  13.   
  14.     迭代啊 - The DoYouHaoBaby Framework !<br>   
  15.   
  16. <?php $oMyIterator->next() ;endwhile;  ?>  

如图:


程序输出结果如下:

  1. 迭代啊 - The DoYouHaoBaby Framework !   
  2. 迭代啊 - The DoYouHaoBaby Framework !   
  3. 迭代啊 - The DoYouHaoBaby Framework !  

如图:

说明:同时我们还是为迭代器添加索引和当前变量,接下来我们将会做的。


2:为迭代器添加索引和当前变量


我们在模版中写下如下的代码:

  1. <php>   
  2.      $arrList = array(   
  3.          'title'=>'The DoYouHaoBaby PHP Framework',   
  4.          'time'=>'2010-2011',   
  5.          'copyright'=>'The DoYouHaoBaby Company !',   
  6.      );   
  7.      $oMyIterator = new DArrayIterator( $arrList );   
  8. </php>   
  9.   
  10. <iterate iterator='oMyIterator' var="dyhb" index="index">   
  11.     {$index}-{$dyhb}<br>   
  12. </iterate>  


程序运行后的编译PHP 代码如下:

  1. <?php !defined('DYHB_PATH') && exit; /* DoYouHaoBaby Framework 模板缓存文件 生成时间:2002-01-12 22:32:52  */ ?>   
  2. <?php    
  3.      $arrList = array(   
  4.          'title'=>'The DoYouHaoBaby PHP Framework',   
  5.          'time'=>'2010-2011',   
  6.          'copyright'=>'The DoYouHaoBaby Company !',   
  7.      );   
  8.      $oMyIterator = new DArrayIterator( $arrList );   
  9.  ?>   
  10.   
  11. <?php $oMyIterator->first() ;   
  12. $index = -1 ;   
  13. while( !$oMyIterator->isDone() ):    
  14.     $index ++;   
  15.     $dyhb = $oMyIterator->current() ; ?>   
  16.   
  17.     <?php echo( $index ); ?>-<?php echo( $dyhb ); ?><br>   
  18.   
  19. <?php $oMyIterator->next() ;endwhile;  ?>  


如图:


程序最后运行结果如下:

  1. 0-The DoYouHaoBaby PHP Framework   
  2. 1-2010-2011   
  3. 2-The DoYouHaoBaby Company !  


如图:


3:同时迭代器的条件还支持条件表达式 ( 同if的条件表达式 ) :

例如,我们在模板中写下如下的代码:

  1. <php>   
  2.      $arrList = array(   
  3.          'title'=>'The DoYouHaoBaby PHP Framework',   
  4.          'time'=>'2010-2011',   
  5.          'copyright'=>'The DoYouHaoBaby Company !',   
  6.      );   
  7.      $oMyIterator = new DArrayIterator( $arrList );   
  8.      $sIteratorName = 'oMyIterator';   
  9.      $sVar = 'dyhb';   
  10.      $sIndex = 'index';   
  11. </php>   
  12.   
  13. <iterate iterator='$sIteratorName' var="$sVar" index="$sIndex">   
  14.     {$$sIndex}-{$$sVar}<br>   
  15. </iterate>  


程序编译后PHP 代码如下:

  1. <?php !defined('DYHB_PATH') && exit; /* DoYouHaoBaby Framework 模板缓存文件 生成时间:2002-01-12 22:44:40  */ ?>   
  2. <?php    
  3.      $arrList = array(   
  4.          'title'=>'The DoYouHaoBaby PHP Framework',   
  5.          'time'=>'2010-2011',   
  6.          'copyright'=>'The DoYouHaoBaby Company !',   
  7.      );   
  8.      $oMyIterator = new DArrayIterator( $arrList );   
  9.      $sIteratorName = 'oMyIterator';   
  10.      $sVar = 'dyhb';   
  11.      $sIndex = 'index';   
  12.  ?>   
  13.   
  14. <?php $$sIteratorName->first() ;   
  15. $$sIndex = -1 ;   
  16. while( !$$sIteratorName->isDone() ):    
  17.     $$sIndex ++;   
  18.     $$sVar = $$sIteratorName->current() ; ?>   
  19.   
  20.     <?php echo( $$sIndex ); ?>-<?php echo( $$sVar ); ?><br>   
  21.   
  22. <?php $$sIteratorName->next() ;endwhile;  ?>   


如图:


程序运行结果如下:

  1. 0-The DoYouHaoBaby PHP Framework   
  2. 1-2010-2011   
  3. 2-The DoYouHaoBaby Company !  


如图: