跳出循环

Testing Is Documentation

tests/View/Compiler/CompilerBreakTest.php跳出循环 - 图1

break 和 continue 是各种循环中非常重要的两个流程标记语言,框架当然也会支持它们。

break 标签

  1. public function testBaseUse(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <list for=list>
  6. <if condition="$value eq 'H'">
  7. <break/>
  8. </if>
  9. {$value}
  10. </list>
  11. eot;
  12. $compiled = <<<'eot'
  13. <?php $index = 1; ?>
  14. <?php if (is_array($list)): foreach ($list as $key => $value): ?>
  15. <?php if ($value == 'H'): ?>
  16. <?php break; ?>
  17. <?php endif; ?>
  18. <?php echo $value; ?>
  19. <?php $index++; ?>
  20. <?php endforeach; endif; ?>
  21. eot;
  22. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  23. }

ontinue 标签

  1. public function testContinue(): void
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <list for=list>
  6. <if condition="$value eq 'H'">
  7. <continue/>
  8. </if>
  9. {$value}
  10. </list>
  11. eot;
  12. $compiled = <<<'eot'
  13. <?php $index = 1; ?>
  14. <?php if (is_array($list)): foreach ($list as $key => $value): ?>
  15. <?php if ($value == 'H'): ?>
  16. <?php continue; ?>
  17. <?php endif; ?>
  18. <?php echo $value; ?>
  19. <?php $index++; ?>
  20. <?php endforeach; endif; ?>
  21. eot;
  22. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  23. }