Loops
Tradução automática
Esta página foi traduzida automaticamente do inglês e ainda não foi revisada por um falante nativo; correções são bem-vindas no GitHub. Se algo não bater, vale o original em inglês. O código e os resultados são os mesmos do original, importados dos arquivos testados.
Tarefa: repetir parte de uma página para cada item de uma lista ou cada número de um intervalo.
yaml
# Recipe: q:loop over a list, a range of numbers and comma-separated text.
paths:
components: ./componentstype="array" percorre uma lista, com index= nomeando a posição a partir de 0. type="range" conta de from até to, os dois incluídos, de step em step. type="list" divide um texto pelas vírgulas e tira os espaços de cada item.
xml
<q:component name="Loops">
<q:set name="fruits" type="array" value='["Apple", "Banana", "Cherry"]' />
<html>
<body>
<h2>A list</h2>
<ol>
<!-- index= names the position, from 0 -->
<q:loop type="array" var="fruit" index="i" items="{fruits}">
<li>{i + 1}. {fruit}</li>
</q:loop>
</ol>
<h2>A range</h2>
<p>
<!-- from and to are both included -->
<q:loop type="range" var="n" from="2" to="10" step="2">
<span>[{n}]</span>
</q:loop>
</p>
<h2>Text split by commas</h2>
<ul>
<q:loop type="list" var="tag" items="red, green , blue">
<li>({tag})</li>
</q:loop>
</ul>
</body>
</html>
</q:component>xml
<q:test name="a list, with each position" page="/">
<test:visit />
<test:expect text="1. Apple" />
<test:expect text="3. Cherry" />
</q:test>
<q:test name="a range includes both ends" page="/">
<test:visit />
<test:expect text="[2] [4] [6] [8] [10]" />
</q:test>
<q:test name="list items lose the spaces around them" page="/">
<test:visit />
<test:expect text="(red) (green) (blue)" />
</q:test>text
tests/loops.test.q
PASS a list, with each position
PASS a range includes both ends
PASS list items lose the spaces around them
3 passed, 0 failedPara percorrer as linhas de uma consulta, veja Vários formulários numa página. As regras: LOOP-5 e LOOP-6.