Grégoire Pineau
DevOps @ Blackfire.io
composer require twig/twig:^1.0
require __DIR__.'/vendor/autoload.php';
$loader = new Twig_Loader_Filesystem([__DIR__.'/templates']);
$twig = new Twig_Environment($loader);
Template
{# hello.html.twig ; ceci est un commentaire #}
{% if 'gr' in name %}"gr" est bien dans "{{ name }}"{% endif %}
{% for i in [1, 2, 5, 10] %}
i vaut {{ i }}
{% else %}
il n'y a pas d'élement dans la liste
{% endfor %}
PHP
echo $twig->render('hello.html.twig', ['name' => 'greg']);
Résultat
"gr" est bien dans "greg"
i vaut 1
i vaut 2
i vaut 5
i vaut 10
Twig traite les objets et les tableaux de la meme facon:
{{ user.name }}
$user['name'];
$user->name;
$user->name();
$user->getName();
{% set var = 'my first car' %}
Filtres:
{{ var|upper }} {# MY FIRST CAR #}
{{ var|lower }} {# my first car #}
{{ var|title }} {# My First Car #}
{{ var|capitalize }} {# My first car #}
{{ another_var|default('default value') }} {# default value #}
Fonctions:
{{ date('yesterday 12:00')|date('c') }} {# 2015-10-05T12:00:00+02:00 #}
hello {{ name }}
echo $twig->render('index.html.twig', [
'name' => '<script>alert("coucou")</script>',
]);
hello <script>alert("coucou")</script>
{{ user.username|raw }}
{{ user.username|e('js') }}
{{ user.username|e('css') }}
{{ user.username|e('url') }}
{{ user.username|e('html_attr') }}
hello {{ name }}
try {
$twig->render('var.html.twig', array('prénom' => 'greg'));
} catch (\Exception $e) {
echo $e->getMessage();
}
Twig_Error_Runtime: Variable "name" does not exist in "vars.html.twig"
at line 1
rand: {{ rand() }}
try {
$twig->render('funct.html.twig');
} catch (\Exception $e) {
echo $e->getMessage();
}
The function "rand" does not exist. Did you mean "random" in "funct.html.twig"
at line 1
Vous pouvez ajouter facilement des:
macro
, global
, function
, filter
, test
, operator
et plus difficilement des:
tag
{{ word|shuffle }}
class ShuffleExtension extends Twig_Extension
{
public function getFilters()
{
return [
new Twig_SimpleFilter('shuffle', 'str_shuffle'),
];
}
public function getName()
{
return 'shuffle';
}
}
$twig->addExtension(new ShuffleExtension());
echo $twig->render('shuffle.html.twig', array('word' => 'shuffle'));
lsuffhe
$profile = new Twig_Profiler_Profile();
$twig->addExtension(new Twig_Extension_Profiler($profile));
$twig->render('homepage.html.twig', [
'name' => 'greg',
]);
// $dumper = new Twig_Profiler_Dumper_Blackfire();
$dumper = new Twig_Profiler_Dumper_Text();
echo $dumper->dump($profile);
main 1.94ms/100%
└ homepage.html.twig 1.94ms/100%
└ qui-sommes-nous.html.twig
Pourquoi ? Quand ?
{# layout.html.twig #}
<html>
<head>
<title>{% block title '' %}</title>
{% block meta '' %}
{% block css %}
<style type="text/css" src="/css/styles.css"></style>
{% endblock %}
</head>
<body>
Navigation
Fil d'arianne
{% block body '' %}
{% block javascripts '' %}
</body>
</html>
{# homepage.html.twig #}
{% extends 'layout.html.twig' %}
{% block title 'Ma page' %}
{% block css %}
{{ parent() }}
<style type="text/css" src="/css/homepage.css"></style>
{% endblock %}
{% block body %}
Coverflow
Boutons
{% endblock %}
<html>
<head>
<title>Ma page</title>
<style type="text/css" src="/css/styles.css"></style>
<style type="text/css" src="/css/homepage.css"></style>
</head>
<body>
Navigation
Fil d'arrianne
Coverflow
Boutons
</body>
</html>
Pourquoi ? Quand ?
{# qui-sommes-nous.html.twig #}
<div>
Qui sommes nous ?
</div>
::
{# homepage.html.twig #}
<div>
New
</div>
<div>
Derniers Commentaires
</div>
{{ include('qui-sommes-nous.html.twig') }}
{# news.html.twig #}
{% for post in posts %}
{{ include('post.html.twig', { post: post }, with_context = false) }}
{% endfor %}
::
{# post.html.twig #}
<div>
<h3>{{ post.title}}</h3>
<div>{{ post.content }}</div>
</div>
Pourquoi ? Quand ?
{# blocks.html.twig #}
{% block qui_sommes_nous %}
<div>
Qui sommes nous ?
</div>
{% endblock %}
::
{# homepage.html.twig #}
{# ... #}
{% use "blocks.html.twig" %}
{{ block('qui_sommes_nous') }}
{# OU #}
{% block qui_sommes_nous %}
<div class="container">
{{ parent() }}
</div>
{% endblock %}
Pourquoi ? Quand ?
{# news.html.twig #}
{% for post in posts %}
{% embed "post.html.twig" with { post: post } only %}{% endembed %}
{% endfor %}
{# post.html.twig #}
<div>
{% block title %}<h3>{{ post.title}}</h3>{% endblock %}
{% block content%}<div>{{ post.content }}</div>{% endblock %}
{% block metas %}
<p>Par {{ post.author }} le {{ post.date }}</p>
{% endblock %}
{% block comments %}
<p>il y a {{ post.comment|length }} commentaires</p>
{% endblock %}
</div>
{# homepage.html.twig #}
<div>
{% embed "embed/post.html.twig" with { post: posts|last } only %}
{% block title %}
<h1>{{ post.title}}</h1>
{% endblock %}
{% block comments %}
{{ parent() }}<p>Lire les commentaire</p>
{% endblock %}
{% endembed %}
</div>
<div>
Derniers Commentaires
</div>
{{ include('include/qui-sommes-nous.html.twig') }}
Pourquoi ? Quand ?
{% macro input(name, type = 'text', value = '', size = '20') %}
{% spaceless %}
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}"
size="{{ size }}" />
{% endspaceless %}
{% endmacro %}
{% import _self as forms %}
{# ou #}
{% import 'forms.html.twig' as forms %}
{{ forms.input('user') }}
{{ forms.input('email', 'email') }}
Vous voulez:
title_*
)Les slides:
Sinon:
Et:
Comment ça marche?