Skip to content

Instantly share code, notes, and snippets.

@lyrixx
Forked from xavierbriand/base.twig
Created January 3, 2012 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyrixx/1555283 to your computer and use it in GitHub Desktop.
Save lyrixx/1555283 to your computer and use it in GitHub Desktop.
Twig training
{# 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>
<?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);
//...
{% 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 %}
{{ 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 %}
{% 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
{% for post in posts %}
{{ post.title }}
{% else %}
Pas de billets.
{% endfor %}
{{ random(['plouf', 'pic', 'nic', 'douille']) }}
{# 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' %}
<?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...
<?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