Created
December 20, 2011 14:51
-
-
Save xavierbriand/1501819 to your computer and use it in GitHub Desktop.
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 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> |
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
{% 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'] %} |
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
{% 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