The DoYouHaoBaby PHP Framework中文开发文档手册
Copyright(c) DoYouHaoBaby.NET All Rights Reserved.
Copyright(c) DoYouHaoBaby.NET All Rights Reserved.
[ Iterate迭代器 ]
如果我们需要在模板中使用迭代器,那么通过iterate标签可以很方便地输出。
1:最简单的一个迭代器
我们在模板中写下如下的代码,生成迭代器:
- <php>
- $arrList = array(
- 'title'=>'The DoYouHaoBaby PHP Framework',
- 'time'=>'2010-2011',
- 'copyright'=>'The DoYouHaoBaby Company !',
- );
- $oMyIterator = new DArrayIterator( $arrList );
- </php>
- <iterate iterator='oMyIterator'>
- 迭代啊 - The DoYouHaoBaby Framework !<br>
- </iterate>
程序运行后的编译PHP 代码如下:
- <?php !defined('DYHB_PATH') && exit; /* DoYouHaoBaby Framework 模板缓存文件 生成时间:2002-01-12 22:36:28 */ ?>
- <?php
- $arrList = array(
- 'title'=>'The DoYouHaoBaby PHP Framework',
- 'time'=>'2010-2011',
- 'copyright'=>'The DoYouHaoBaby Company !',
- );
- $oMyIterator = new DArrayIterator( $arrList );
- ?>
- <?php $oMyIterator->first() ;
- while( !$oMyIterator->isDone() ): ?>
- 迭代啊 - The DoYouHaoBaby Framework !<br>
- <?php $oMyIterator->next() ;endwhile; ?>
如图:

程序输出结果如下:
- 迭代啊 - The DoYouHaoBaby Framework !
- 迭代啊 - The DoYouHaoBaby Framework !
- 迭代啊 - The DoYouHaoBaby Framework !
如图:

说明:同时我们还是为迭代器添加索引和当前变量,接下来我们将会做的。
2:为迭代器添加索引和当前变量
我们在模版中写下如下的代码:
- <php>
- $arrList = array(
- 'title'=>'The DoYouHaoBaby PHP Framework',
- 'time'=>'2010-2011',
- 'copyright'=>'The DoYouHaoBaby Company !',
- );
- $oMyIterator = new DArrayIterator( $arrList );
- </php>
- <iterate iterator='oMyIterator' var="dyhb" index="index">
- {$index}-{$dyhb}<br>
- </iterate>
程序运行后的编译PHP 代码如下:
- <?php !defined('DYHB_PATH') && exit; /* DoYouHaoBaby Framework 模板缓存文件 生成时间:2002-01-12 22:32:52 */ ?>
- <?php
- $arrList = array(
- 'title'=>'The DoYouHaoBaby PHP Framework',
- 'time'=>'2010-2011',
- 'copyright'=>'The DoYouHaoBaby Company !',
- );
- $oMyIterator = new DArrayIterator( $arrList );
- ?>
- <?php $oMyIterator->first() ;
- $index = -1 ;
- while( !$oMyIterator->isDone() ):
- $index ++;
- $dyhb = $oMyIterator->current() ; ?>
- <?php echo( $index ); ?>-<?php echo( $dyhb ); ?><br>
- <?php $oMyIterator->next() ;endwhile; ?>
如图:

程序最后运行结果如下:
- 0-The DoYouHaoBaby PHP Framework
- 1-2010-2011
- 2-The DoYouHaoBaby Company !
如图:

3:同时迭代器的条件还支持条件表达式 ( 同if的条件表达式 ) :
例如,我们在模板中写下如下的代码:
- <php>
- $arrList = array(
- 'title'=>'The DoYouHaoBaby PHP Framework',
- 'time'=>'2010-2011',
- 'copyright'=>'The DoYouHaoBaby Company !',
- );
- $oMyIterator = new DArrayIterator( $arrList );
- $sIteratorName = 'oMyIterator';
- $sVar = 'dyhb';
- $sIndex = 'index';
- </php>
- <iterate iterator='$sIteratorName' var="$sVar" index="$sIndex">
- {$$sIndex}-{$$sVar}<br>
- </iterate>
程序编译后PHP 代码如下:
- <?php !defined('DYHB_PATH') && exit; /* DoYouHaoBaby Framework 模板缓存文件 生成时间:2002-01-12 22:44:40 */ ?>
- <?php
- $arrList = array(
- 'title'=>'The DoYouHaoBaby PHP Framework',
- 'time'=>'2010-2011',
- 'copyright'=>'The DoYouHaoBaby Company !',
- );
- $oMyIterator = new DArrayIterator( $arrList );
- $sIteratorName = 'oMyIterator';
- $sVar = 'dyhb';
- $sIndex = 'index';
- ?>
- <?php $$sIteratorName->first() ;
- $$sIndex = -1 ;
- while( !$$sIteratorName->isDone() ):
- $$sIndex ++;
- $$sVar = $$sIteratorName->current() ; ?>
- <?php echo( $$sIndex ); ?>-<?php echo( $$sVar ); ?><br>
- <?php $$sIteratorName->next() ;endwhile; ?>
如图:

程序运行结果如下:
- 0-The DoYouHaoBaby PHP Framework
- 1-2010-2011
- 2-The DoYouHaoBaby Company !
如图:

