Skip to content

Instantly share code, notes, and snippets.

@xavierbriand
Created December 20, 2011 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xavierbriand/1501819 to your computer and use it in GitHub Desktop.
Save xavierbriand/1501819 to your computer and use it in GitHub Desktop.
Twig training
{# manipulation des variables #}
{% set my_var = 'test' %}
<p>
{{ my_var }}
</p>
<hr />
{# tableau #}
{% set my_other_var = {'title': 'Titre', body: 'Corps'} %}
<h1>
{{ my_other_var.title }}
</h1>
<p>
{{ my_other_var['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']) }}
{% include 'column.html.twig' %}
{% set article = { titre: 'Mon titre' } %}
{% include 'column.html.twig' with {titre: article.titre} %}
{% include 'column.html.twig' with {titre: article.titre} only %}
{% include 'column.html.twig' only%}
{% set var = 'column.html.twig' %}
{% include var %}
{% include ajax_actif ? 'ajax.html.twig' : 'ss_ajax.html.twig' %}
{% include 'header_'~locale~'.html.twig' ignore missing only %}
{% include ['header_'~locale~'.html.twig', 'header.html.twig'] %}
{# 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' %}
{% spaceless %}
<a href="#">
Mon super titre
</a>
{% endspaceless %}
<a href="#">Mon super titre</a>
{% set var = 'test' %}
{%- if true -%}
{{- var -}}
{%- endif -%}
test
{% set var = 'test' %}
<li> {{- var }} </li>
<li>test </li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment