Bucles
Traducción automática
Esta página se tradujo automáticamente del inglés y todavía no la revisó un hablante nativo; las correcciones son bienvenidas en GitHub. Si algo no coincide, vale el original en inglés.
Tarea: repetir parte de una página para cada elemento de una lista o cada número de un rango.
yaml
# Recipe: q:loop over a list, a range of numbers and comma-separated text.
paths:
components: ./componentstype="array" recorre una lista, con index= nombrando la posición desde 0. type="range" cuenta desde from hasta to, ambos incluidos, de step en step. type="list" divide un texto por comas y quita los espacios de cada elemento.
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 recorrer las filas de una consulta, ver Varios formularios en una página. Las reglas: LOOP-5 y LOOP-6.