• For 循环
    • code
    • node 简单版
    • node 完整版
    • JS 风格版: 例 1
    • JS 风格版: 例 2
    • JS 风格版: 例 3

    For 循环

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

    code

    1. public function testBaseUse()
    2. {
    3. $parser = $this->createParser();
    4. $source = <<<'eot'
    5. {for $i=1;$i<10;$i++}
    6. QueryPHP - 代码版本for <br>
    7. {/for}
    8. eot;
    9. $compiled = <<<'eot'
    10. <?php for ($i=1;$i<10;$i++): ?>
    11. QueryPHP - 代码版本for <br>
    12. <?php endfor; ?>
    13. eot;
    14. $this->assertSame($compiled, $parser->doCompile($source, null, true));
    15. }

    node 简单版

    1. public function testForNode()
    2. {
    3. $parser = $this->createParser();
    4. $source = <<<'eot'
    5. <for start='1'>
    6. QueryPHP - node - for <br>
    7. </for>
    8. eot;
    9. $compiled = <<<'eot'
    10. <?php for ($var = 1; $var <= 0; $var += 1): ?>
    11. QueryPHP - node - for <br>
    12. <?php endfor; ?>
    13. eot;
    14. $this->assertSame($compiled, $parser->doCompile($source, null, true));
    15. }

    node 完整版

    1. public function testForNode2()
    2. {
    3. $parser = $this->createParser();
    4. $source = <<<'eot'
    5. <for start='1' end='10' var='myValue' step='3'>
    6. QueryPHP for <br>
    7. </for>
    8. eot;
    9. $compiled = <<<'eot'
    10. <?php for ($myValue = 1; $myValue <= 10; $myValue += 3): ?>
    11. QueryPHP for <br>
    12. <?php endfor; ?>
    13. eot;
    14. $this->assertSame($compiled, $parser->doCompile($source, null, true));
    15. }

    JS 风格版: 例 1

    最终生成一个 foreach 结果,简单的循环。

    1. public function testForJsStyle()
    2. {
    3. $parser = $this->createParser();
    4. $source = <<<'eot'
    5. {% for item in navigation %}
    6. <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    7. {% /for %}
    8. eot;
    9. $compiled = <<<'eot'
    10. <?php foreach ($navigation as $key => $item): ?>
    11. <li><a href="<?php echo $item->href; ?>"><?php echo $item->caption; ?></a></li>
    12. <?php endforeach; ?>
    13. eot;
    14. $this->assertSame($compiled, $parser->doCompile($source, null, true));
    15. }

    JS 风格版: 例 2

    可以使用逗号分割建和值,逗号连接不能有空格。

    1. public function testForJsStyle2()
    2. {
    3. $parser = $this->createParser();
    4. $source = <<<'eot'
    5. {% for mykey,item in navigation %}
    6. <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    7. {% /for %}
    8. eot;
    9. $compiled = <<<'eot'
    10. <?php foreach ($navigation as $mykey => $item): ?>
    11. <li><a href="<?php echo $item->href; ?>"><?php echo $item->caption; ?></a></li>
    12. <?php endforeach; ?>
    13. eot;
    14. $this->assertSame($compiled, $parser->doCompile($source, null, true));
    15. }

    JS 风格版: 例 3

    可以使用空格分割建和值。

    1. public function testForJsStyle3()
    2. {
    3. $parser = $this->createParser();
    4. $source = <<<'eot'
    5. {% for mykey item in navigation %}
    6. <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    7. {% /for %}
    8. eot;
    9. $compiled = <<<'eot'
    10. <?php foreach ($navigation as $mykey => $item): ?>
    11. <li><a href="<?php echo $item->href; ?>"><?php echo $item->caption; ?></a></li>
    12. <?php endforeach; ?>
    13. eot;
    14. $this->assertSame($compiled, $parser->doCompile($source, null, true));
    15. }