• Skip to content
  • Skip to primary sidebar

Developers Tips & Tricks

You are here: Home / TWIG / Variables y Estructuras en TWIG

Variables y Estructuras en TWIG

26 noviembre, 2014 by MPezzolano

Variables y Estructuras en TWIG

Variables

En una plantilla Twig podemos utilizar variables locales o globales.

Variables locales

Las variables locales son aquellas que se han proporcionado a la plantilla a través del controlador:

$this->render('MyRecipesBundle:Default:show', array('recipe' => $recipe));

También son variables locales las definidas dentro de la propia plantilla:

{% set system_messages = ['error', 'warning', 'notice', 'success'] %}
{% for type in system_messages %}
  {{ ... }}
{% endfor %}

Estructuras de control

En Twig existen dos estructuras de control; bucles y condicionales. Los condicionales se representan con el tag if.

{% if recipe.difficulty == 'fácil' %}
  No tendrás problemas para concinar esta receta.
{% elseif recipe.difficulty == 'media' %}
  Esta receta requiere conocimientos avanzados de cocina.
{% else %}
  ¡Para dominar esta receta necesitas ser un Top Chef!
{% endif %}

Podremos recorrer arrays y colecciones con el tag for.

<h3>Recetas del autor</h3>
<ul>
    {% for recipe in author.recipes %}
        <li class="recipe">{{ recipe.name }}</li>
    {% endfor %}
</ul>

En los bucles podemos recuperar el número de la iteración con loop.index y loop.index0:

{% for recipe in author.recipes %}
  <li class="recipe {% if loop.index0 is odd %}odd{% else %}even{% endif %}">{{ recipe.name }}</li>
{% endfor %}


Filed Under: TWIG

Primary Sidebar

Categorías

  • Angular JS (10)
  • BackBone JS (6)
  • Ember JS (2)
  • Express JS (4)
  • EXTJS (1)
  • General (7)
  • GIT (2)
  • Grunt JS (2)
  • HTML5 (8)
  • JAVA (3)
  • JavaScript (14)
  • JQUERY (15)
  • JSON (1)
  • JSP (1)
  • Knockout JS (2)
  • LARAVEL (13)
  • Linux (2)
  • Mobile Angular UI (1)
  • Modulus.io (1)
  • MongoDB (5)
  • MySQL (3)
  • NodeJS (11)
  • PHP (1)
  • Polymer (3)
  • PostgreSQL (1)
  • Prism.JS (1)
  • PYTHON (10)
  • QOOXDOO (1)
  • React JS (7)
  • Redis (2)
  • Sin categoría (4)
  • SQL (1)
  • TWIG (4)
  • UnderScore (1)
  • Web Services. (2)
  • Wordpress (1)

Cloud Tags

Angular JS BackBone JS Ember JS Express JS EXTJS General GIT Grunt JS HTML5 JAVA JavaScript JQUERY JSON JSP Knockout JS LARAVEL Linux Mobile Angular UI Modulus.io MongoDB MySQL NodeJS PHP Polymer PostgreSQL Prism.JS PYTHON QOOXDOO React JS Redis Sin categoría SQL TWIG UnderScore Web Services. Wordpress

Copyright © 2019 · Genesis Sample Theme on Genesis Framework · WordPress · Log in