Questions tagged [twig]

Twig is a modern, fast, flexible, and secure templating engine for PHP. Created for Symfony and adopted by Drupal 8.

Twig is a modern template engine for . It is developed for, and used by default, in the , , and frameworks, but can also easily be used in other projects. It’s both designer and developer friendly by sticking to PHP’s principles and adding functionality useful for templating environments.

Its key features are (source: https://twig.symfony.com/) :

  • Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
  • Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
  • Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.

When asking or answering a question, do not hesitate to isolate the problematic code to reproduce it or solve it in an external environment, such as twigfiddle, to get more efficient help or give a working solution.

For more details refer to:

  1. Official Website
  2. Twig Documentation
  3. Twig Installation
  4. Twig Download from Github
10606 questions
144
votes
4 answers

Persistent :set syntax for a given filetype?

I'm working on a Symfony2 project which uses Twig, and the filetypes are myfile.html.twig. Vim doesn't automatically detect the syntax highlighting and so applies none. I can use :set syntax=HTML after I've opened the file but this is a pain when…
Bendihossan
  • 2,407
  • 5
  • 22
  • 25
142
votes
2 answers

twig: IF with multiple conditions

It seem I have problem with a twig if statement. {%if fields | length > 0 || trans_fields | length > 0 -%} The error is: Unexpected token "punctuation" of value "|" ("name" expected) in I can't understand why this doesn't work, it's like if twig…
FMaz008
  • 11,161
  • 19
  • 68
  • 100
124
votes
5 answers

How can I use break or continue within for loop in Twig template?

I try to use a simple loop, in my real code this loop is more complex, and I need to break this iteration like: {% for post in posts %} {% if post.id == 10 %} {# break #} {% endif %}

{{ post.heading }}

{% endfor %} How…
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
123
votes
4 answers

PhpStorm wrap/surround selection?

Often in coding and templating I need to wrap a certain part of text. Is there any shortcut to wrap the current selection, for example: Hello World "Hello World" Hello World {{ trans 'Hello World' }} Im using PhpStorm 7 for Mac and PC. I found…
bench-o
  • 2,339
  • 5
  • 21
  • 21
119
votes
2 answers

Add more than one parameter in Twig path

How to add more than one parameter in Twig path? Say you have this route : article_show: pattern: /article/{slug} defaults: { _controller: AcmeArticleBundle:Article:show } You can do this in your twig template : {{ path('article_show', {…
Wissem
  • 1,705
  • 4
  • 16
  • 22
108
votes
2 answers

Symfony 2: How do I check if a user is not logged in inside a template?

In Symfony 2 templates (using Twig), how can I effectively check whether a user is not logged in? I don't want to use ROLE checks. I want a straightforward way to check if a user is not logged in. I'm aware that comparing app.user.username with anon…
Tool
  • 12,126
  • 15
  • 70
  • 120
104
votes
1 answer

Symfony get Asset Name in Assetic output

I generate my file with code like this: {% block head_stylesheets %} {% stylesheets filter='?uglifycss' '@MyBundle/Resources/public/less/myfile.less' %}
BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
103
votes
4 answers

In Twig, check if a specific key of an array exists

In PHP we can check if a key exists in an array by using the function array_key_exists(). In the Twig templating language we can check if an variable or an object's property exists simply by using an if statement, like this: {% if app.user %} do…
user852610
  • 2,205
  • 7
  • 36
  • 46
103
votes
3 answers

Get the environment name in a Twig template with Symfony2

Is there a way to get the name of the current environment in a Twig template? I'd like to set some CSS style values depending on it.
GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69
101
votes
3 answers

Accessing the logged-in user in a template

I'm using FOSuserbundle to get started with User registration https://github.com/FriendsOfSymfony/FOSUserBundle I've got it registering / logging in and out. What I want to do now is grab the logged in users data and present it on every page of my…
ed209
  • 11,075
  • 19
  • 66
  • 82
95
votes
6 answers

How to render a tree in Twig

I would like to render a tree with an undetermined depth (children of children of children, etc.). I need to loop through the array recursively; how can I do this in Twig?
T-RonX
  • 1,053
  • 1
  • 9
  • 7
87
votes
2 answers

IF a == true OR b == true statement

I can't find a way to have TWIG interpret the following conditional statement: {% if a == true or b == true %} do stuff {% endif %} Am I missing something or it's not possible?
MarkL
  • 8,770
  • 7
  • 26
  • 27
87
votes
1 answer

How do you check if an object exists in the Twig templating engine in Symfony2?

I have a multidimensional array where some objects exist and others don't. I keep getting a Method "code" for object "stdClass" does not exist in...? The code I am using in my template is: {% for item in items %}

{% if item.product.code %}{{…

Adam Stacey
  • 2,833
  • 4
  • 26
  • 38
85
votes
4 answers

How do I fix "Add myBundle to the asseticBundle config" symfony2 exception?

When I am trying to use the TWIG {% javascript %} tag to link to my .js file it return me with the following exception : An exception has been thrown during the compilation of a template ("You must add CompetitiongameBundle to the assetic.bundle…
Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102
82
votes
4 answers

Counting the number of elements in array

I am looking to count the number of entries I have in an array in Twig. This is the code I've tried: {%for nc in notcount%} {{ nc|length }} {%endfor%} This however only produces the length of the string of one of the values in the array. {{nc}}…
MikeHolford
  • 1,851
  • 2
  • 21
  • 32