Ir al contenido

Condicionales ​

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: mostrar una cosa u otra según un valor.

yaml
# Recipe: q:if, q:elseif and q:else.
paths:
  components: ./components

Se ejecuta la primera rama cuya condición se cumple; q:else se ejecuta cuando ninguna se cumple. Una condición es una expresión, con and, or y not. Dentro de un atributo, < se escribe &lt;: un archivo .q es XML.

xml
<q:component name="Stock">
  <!-- /?stock=3 -->
  <q:set name="stock" type="number" value="{query.stock}" default="0" />

  <html>
  <body>
    <q:if condition="stock == 0">
      <p class="out">Out of stock.</p>
    </q:if>
    <q:elseif condition="stock &lt; 5">
      <p class="low">Only {stock} left.</p>
    </q:elseif>
    <q:else>
      <p>In stock.</p>
    </q:else>

    <!-- A condition is an expression: and, or, not work as in Python. -->
    <q:if condition="stock > 0 and stock % 2 == 0">
      <p>Sold in pairs: {stock // 2} pairs.</p>
    </q:if>
  </body>
  </html>
</q:component>
xml
<q:test name="no stock" page="/">
  <test:visit />
  <test:expect text="Out of stock." />
  <test:expect no-text="In stock." />
</q:test>

<q:test name="a little stock" page="/?stock=3">
  <test:visit />
  <test:expect text="Only 3 left." />
  <test:expect no-text="pairs" />
</q:test>

<q:test name="plenty, and an even number" page="/?stock=8">
  <test:visit />
  <test:expect text="In stock." />
  <test:expect text="Sold in pairs: 4 pairs." />
</q:test>
text
tests/stock.test.q
  PASS  no stock
  PASS  a little stock
  PASS  plenty, and an even number
3 passed, 0 failed

Ver IF-1 y IF-2.

Licencia MIT · Hecho con VitePress