Twig training
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# manipulation des variables #} | |
{% set une_string = 'test' %} | |
<p> | |
{{ une_string }} | |
</p> | |
<hr /> | |
{# tableau #} | |
{% set un_tableau = {'title' : 'Titre', 'body' : 'Corps'} %} | |
<h1> | |
{{ un_tableau.title }} | |
</h1> | |
<p> | |
{{ un_tableau['body'] }} | |
<p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* TwigBundle:Exception:trace.html.twig */ | |
class __TwigTemplate_159eb1c13ae13a5937a4c6f8b873c6ca extends Twig_Template | |
{ | |
protected function doDisplay(array $context, array $blocks = array()) | |
{ | |
// line 1 | |
if ($this->getAttribute($this->getContext($context, "trace"), "function")) { | |
// line 2 | |
echo " at | |
<strong> | |
<abbr title=\""; | |
// line 4 | |
echo twig_escape_filter($this->env, $this->getAttribute($this->getContext($context, "trace"), "class"), "html", null, true); | |
echo "\">"; | |
echo twig_escape_filter($this->env, $this->getAttribute($this->getContext($context, "trace"), "short_class"), "html", null, true); | |
echo "</abbr> | |
"; | |
// line 5 | |
echo twig_escape_filter($this->env, ($this->getAttribute($this->getContext($context, "trace"), "type") . $this->getAttribute($this->getContext($context, "trace"), "function")), "html", null, true); | |
//... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if articles is defined %} | |
<ul> | |
{% for article in articles %} | |
<li class="{{ cycle(['odd', 'even'], loop.index0)}}"> | |
{{ loop.index }}/{{ loop.length }}. | |
{{ user.username|title }} | |
</li> | |
{% else %} | |
<li> | |
Aucun article disponible. | |
</li> | |
{% endfor %} | |
</ul> | |
{% elseif posts is defined %} | |
{# handle posts #} | |
{% for key, post in posts if post.active %} | |
{# ... #} | |
{% endfor %} | |
{% else %} | |
{# ... #} | |
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ var|escape }} | |
{{ var|raw }} | |
{% autoescape true %} | |
{% var %} | |
{% var|raw %} {# désactive échappement #} | |
{% var|escape %} {# échappe une seul fois #} | |
{% endautoescape %} | |
{% autoescape true js %} | |
{{ var|escape('html') }} {# échappe le js et le html #} | |
{{ var }} {# échappe le javascript #} | |
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% set name = 'XAVIER' %} | |
{{ name|lower }} | |
XAVIER => xavier | |
{# avec arguments #} | |
{% set list = [1, 2, 3] %} | |
{{ list|join(' - ') }} | |
1 - 2 - 3 | |
{# sur un bloc de code #} | |
{% filter upper %} | |
à mettre en majuscule | |
{% endfilter %} | |
{# chainés #} | |
{{ name|lower|replace({'xavier': 'charles'})|capitalize }} | |
XAVIER => xavier => charles => Charles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for post in posts %} | |
{{ post.title }} | |
{% else %} | |
Pas de billets. | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ random(['plouf', 'pic', 'nic', 'douille']) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# Assigne 'bar' à la variable foo #} | |
{% set foo = 'bar' %} | |
{# Assigne un tableau à la variable foo #} | |
{% set foo = [1, 2] %} | |
{# Assigne un tableau associatif à la variable foo #} | |
{% set foo = {'foo': 'bar'} %} | |
{# Concatène la chaine 'foo' avec la variable bar #} | |
{% set foo = 'foo' ~ bar %} | |
{# Assigne 'foo' à la variable foo et | |
'bar' à la variable bar #} | |
{% set foo, bar = 'foo', 'bar' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Ici, Twig est une instance de Twig_Environment | |
// On charge une template. | |
$template = $twig->loadTemplate('index.html'); | |
// On affiche la template, avec des variables passées à celle-ci. | |
echo $template->render(array( | |
'clé de ma variable' => 'valeur de ma variable', | |
'k2' => 'v2' | |
)); | |
// Ou, en un coup : | |
echo $twig->render('index.html', array( | |
'clé de ma variable' => 'valeur de ma variable', | |
'k2' => 'v2' | |
)); | |
// Mais on peut aussi rendre un block directement : | |
echo $template->renderBlock('mon_block_title', array()); | |
// Attention le deuxième paramètre est le contexte... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Ici, Twig est une instance de Twig_Environment | |
// On récupère la macro. | |
$macroButtons = $twig->loadTemplate('macros/buttons.twig'); | |
// On ajoute la template dans l’environnement | |
$twig->addGlobal('macro_buttons', $macroButtons); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment