Lists 循环

Testing Is Documentation

tests/View/Compiler/CompilerListsTest.phpLists 循环 - 图1

lists 标签主要用于在模板中循环输出数据集或者多维数组。

普通输出

lists 标签的 name 属性表示模板赋值的变量名称,因此不可随意在模板文件中改变。 id 表示当前的循环变量,可以随意指定,但确保不要和 name 属性冲突。

  1. public function testBaseUse(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo">
  6. {$vo.title} {$vo.people}
  7. </lists>
  8. eot;
  9. $compiled = <<<'eot'
  10. <?php if (is_array($list)):
  11. $index = 0;
  12. $tmp = $list;
  13. if (0 === count($tmp)):
  14. echo "";
  15. else:
  16. foreach ($tmp as $key => $vo):
  17. ++$index;
  18. $mod = $index % 2; ?>
  19. <?php echo $vo->title; ?> <?php echo $vo->people; ?>
  20. <?php endforeach;
  21. endif;
  22. else:
  23. echo "";
  24. endif; ?>
  25. eot;
  26. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  27. }

部分输出指定开始位置和长度的记录

支持输出部分数据,例如输出其中的第 2~4 条记录。

  1. public function testOffsetLength(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo" offset="2" length='4'>
  6. {$vo.title} {$vo.people}
  7. </lists>
  8. eot;
  9. $compiled = <<<'eot'
  10. <?php if (is_array($list)):
  11. $index = 0;
  12. $tmp = array_slice($list, 2, 4);
  13. if (0 === count($tmp)):
  14. echo "";
  15. else:
  16. foreach ($tmp as $key => $vo):
  17. ++$index;
  18. $mod = $index % 2; ?>
  19. <?php echo $vo->title; ?> <?php echo $vo->people; ?>
  20. <?php endforeach;
  21. endif;
  22. else:
  23. echo "";
  24. endif; ?>
  25. eot;
  26. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  27. }

部分输出指定开始位置到结尾的所有记录

支持输出部分数据,例如输出指定开始位置到结尾的所有记录。

  1. public function testOffset(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo" offset="3">
  6. {$vo.title} {$vo.people}
  7. </lists>
  8. eot;
  9. $compiled = <<<'eot'
  10. <?php if (is_array($list)):
  11. $index = 0;
  12. $tmp = array_slice($list, 3);
  13. if (0 === count($tmp)):
  14. echo "";
  15. else:
  16. foreach ($tmp as $key => $vo):
  17. ++$index;
  18. $mod = $index % 2; ?>
  19. <?php echo $vo->title; ?> <?php echo $vo->people; ?>
  20. <?php endforeach;
  21. endif;
  22. else:
  23. echo "";
  24. endif; ?>
  25. eot;
  26. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  27. }

输出偶数记录

lists 还支持偶数记录的输出,基于 mod 属性来控制。

  1. public function testMod(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo" mod="2">
  6. <?php if ($mod == 1): ?>
  7. {$vo.title} {$vo.people}
  8. <?php endif; ?>
  9. </lists>
  10. eot;
  11. $compiled = <<<'eot'
  12. <?php if (is_array($list)):
  13. $index = 0;
  14. $tmp = $list;
  15. if (0 === count($tmp)):
  16. echo "";
  17. else:
  18. foreach ($tmp as $key => $vo):
  19. ++$index;
  20. $mod = $index % 2; ?>
  21. <?php if ($mod == 1): ?>
  22. <?php echo $vo->title; ?> <?php echo $vo->people; ?>
  23. <?php endif; ?>
  24. <?php endforeach;
  25. endif;
  26. else:
  27. echo "";
  28. endif; ?>
  29. eot;
  30. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  31. }

TIP

奇数记录和偶数记录规定如下,我们以数组的 0 为开始,0、2、4为偶记录,其它的都为基数记录。

输出奇数记录

lists 还支持奇数记录的输出,基于 mod 属性来控制。

  1. public function testMod2(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo" mod="2">
  6. <?php if (0 === $mod): ?>
  7. {$vo.title} {$vo.people}
  8. <?php endif; ?>
  9. </lists>
  10. eot;
  11. $compiled = <<<'eot'
  12. <?php if (is_array($list)):
  13. $index = 0;
  14. $tmp = $list;
  15. if (0 === count($tmp)):
  16. echo "";
  17. else:
  18. foreach ($tmp as $key => $vo):
  19. ++$index;
  20. $mod = $index % 2; ?>
  21. <?php if (0 === $mod): ?>
  22. <?php echo $vo->title; ?> <?php echo $vo->people; ?>
  23. <?php endif; ?>
  24. <?php endforeach;
  25. endif;
  26. else:
  27. echo "";
  28. endif; ?>
  29. eot;
  30. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  31. }

TIP

奇数记录和偶数记录规定如下,我们以数组索引的 0 为开始,0、2、4 为偶数记录,1、3、5 为基数记录。

控制换行

mod 属性还用于控制一定记录的换行。

  1. public function testMod3(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo" mod="2">
  6. {$vo.title} {$vo.people}
  7. <?php if (0 === $mod): ?>
  8. <br>
  9. <?php endif; ?>
  10. </lists>
  11. eot;
  12. $compiled = <<<'eot'
  13. <?php if (is_array($list)):
  14. $index = 0;
  15. $tmp = $list;
  16. if (0 === count($tmp)):
  17. echo "";
  18. else:
  19. foreach ($tmp as $key => $vo):
  20. ++$index;
  21. $mod = $index % 2; ?>
  22. <?php echo $vo->title; ?> <?php echo $vo->people; ?>
  23. <?php if (0 === $mod): ?>
  24. <br>
  25. <?php endif; ?>
  26. <?php endforeach;
  27. endif;
  28. else:
  29. echo "";
  30. endif; ?>
  31. eot;
  32. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  33. }

mod 支持变量

mod 属性支持变量。

  1. public function testModCanBeVar(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. {~$mod = 4}
  6. <lists name="list" id="vo" mod="mod">
  7. {$vo.title} {$vo.people}
  8. </lists>
  9. eot;
  10. $compiled = <<<'eot'
  11. <?php $mod = 4; ?>
  12. <?php if (is_array($list)):
  13. $index = 0;
  14. $tmp = $list;
  15. if (0 === count($tmp)):
  16. echo "";
  17. else:
  18. foreach ($tmp as $key => $vo):
  19. ++$index;
  20. $mod = $index % $mod; ?>
  21. <?php echo $vo->title; ?> <?php echo $vo->people; ?>
  22. <?php endforeach;
  23. endif;
  24. else:
  25. echo "";
  26. endif; ?>
  27. eot;
  28. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  29. }

输出循环索引

  1. public function testIndex(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo" index="k">
  6. {$k} {$vo.people}
  7. </lists>
  8. eot;
  9. $compiled = <<<'eot'
  10. <?php if (is_array($list)):
  11. $k = 0;
  12. $tmp = $list;
  13. if (0 === count($tmp)):
  14. echo "";
  15. else:
  16. foreach ($tmp as $key => $vo):
  17. ++$k;
  18. $mod = $k % 2; ?>
  19. <?php echo $k; ?> <?php echo $vo->people; ?>
  20. <?php endforeach;
  21. endif;
  22. else:
  23. echo "";
  24. endif; ?>
  25. eot;
  26. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  27. }

输出数组的键值

如果要输出数组的键值,可以直接使用 key 变量,和循环变量不同的是,这个 key 是由数据本身决定,而不是循环控制的,这个 key 可以通过 key 属性指定。

  1. public function testKey(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <lists name="list" id="vo">
  6. key: {$key}
  7. </lists>
  8. eot;
  9. $compiled = <<<'eot'
  10. <?php if (is_array($list)):
  11. $index = 0;
  12. $tmp = $list;
  13. if (0 === count($tmp)):
  14. echo "";
  15. else:
  16. foreach ($tmp as $key => $vo):
  17. ++$index;
  18. $mod = $index % 2; ?>
  19. key: <?php echo $key; ?>
  20. <?php endforeach;
  21. endif;
  22. else:
  23. echo "";
  24. endif; ?>
  25. eot;
  26. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  27. }