Skip to content

Instantly share code, notes, and snippets.

@ValeriiVasyliev
Last active May 18, 2021 08:10
Show Gist options
  • Save ValeriiVasyliev/42795fe0e001828aa26452f04a19bf8f to your computer and use it in GitHub Desktop.
Save ValeriiVasyliev/42795fe0e001828aa26452f04a19bf8f to your computer and use it in GitHub Desktop.
Twig base constructions

Variable

{{ var }}

Conditional

{% if conditional %}
            
             ...

        {% else %}
             ...

{% endif %}

Set variable

{%
  set var = value
%}

Comment

{# ... #}

For

{% for row in rows %}

{% for key, user in users %}
        <li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}

Debug

Inspecting a single variable

If your template has a title variable available, the following will dump its contents to your template:

{{ dump(title) }}
Discovering all available variables in a template

To dump all available variables and their contents in a template, add the following to your template (after enabling debugging):

{{ dump() }}
To dump only the available variable keys use:
{{ dump(_context|keys) }}

Debug content

{% for key, value in _context.view  %}
     <li>{{ key }}</li>
{% endfor %}

https://twig.symfony.com/doc/2.x/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment