5. Javier Eguiluz
Formador + Programador
especializado en
nuevas tecnologías
Diseño y programación web
entusiasta del
framework Symfony
Comunidad sobre el framework Symfony
10. ¿Qué es Twig?
• Twig es un motor y lenguaje de
plantillas (como PHP, Smarty,
Moustache, etc.)
• Twig es el sistema de plantillas
utilizado por Drupal 8.
22. Variables de cada plantilla
{# core/modules/node/templates/node.html.twig #}
{# Default theme implementation to display a node.
Available variables:
- node: Full node entity.
- id: The node ID
- bundle: The type of the node, for example, "page" or "article".
- authorid: The user ID of the node author.
- createdtime: Formatted creation date.
... #}
<article id="node-{{ node.id }}" class="{{ attributes.class }} clearfix"{{ attributes }}>
{{ title_prefix }}
{% if not page %}
<h2{{ title_attributes }}>
<a href="{{ node_url }}" rel="bookmark">{{ label }}</a>
</h2>
{% endif %}
{{ title_suffix }}
{% if display_submitted %}
<footer>
{{ user_picture }}
las plantillas de
Drupal 8 listan
todas sus variables
37. Ejemplo de filtros para texto
lorEm iPsUm
{{ "..."|lower }}
{{ "..."|upper }}
{{ "..."|title }}
{{ "..."|capitalize }}
lorem ipsum
LOREM IPSUM
Lorem Ipsum
Lorem ipsum
38. Los filtros que define Twig
abs
batch
capitalize
convert_encoding
date
date_modify
default
escape
first
format
join
json_encode
keys
last
length
lower
merge
nl2br
number_format
raw
replace
reverse
slice
sort
split
striptags
title
trim
upper
url_encode
44. Aplicando filtros a varios elementos
<p>{{ nombre|striptags|trim }}</p>
<p>{{ apellidos|striptags|trim }}</p>
<p>{{ biografia|striptags|trim }}</p>
45. Aplicando filtros a varios elementos
{% filter striptags|trim %}
<p>{{ nombre }}</p>
<p>{{ apellidos }}</p>
<p>{{ biografia }}</p>
{% endfilter %}
50. Traduciendo contenidos variables
'More posts about Drupal 8'
'More posts about Twig'
'More posts about Symfony'
traducir la
parte fija
considerarlo
una variable
63. La etiqueta IF básica
{# core/modules/block/templates/block.html.twig #}
<div>
{{ title_prefix }}
{% if label %}
<h2 {{ attributes }}>{{ label }}</h2>
{% endif %}
{{ title_suffix }}
</div>
64. La etiqueta IF básica
{# core/modules/block/templates/block.html.twig #}
<div>
✔ Existe la variable
{{ title_prefix }}
✔ No es null
{% if label %}
✔ No es cadena vacía
<h2 {{ attributes }}>{{ label }}</h2>
{% endif %}
{{ title_suffix }}
</div>
72. Condiciones múltiples
{% if not label_hidden %}
{% if not label_hidden or user.is_admin %}
{% if label_hidden and not user.is_admin %}
{% if (label_hidden or user.is_admin)
and node.is_published %}