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
542
votes
11 answers

How to concatenate strings in twig

Anyone knows how to concatenate strings in twig? I want to do something like: {{ concat('http://', app.request.host) }}
stoefln
  • 14,498
  • 18
  • 79
  • 138
307
votes
8 answers

How to check for null in Twig?

What construct should I use to check whether a value is NULL in a Twig template?
Fluffy
  • 27,504
  • 41
  • 151
  • 234
251
votes
8 answers

Twig: in_array or similar possible within if statement?

I am using Twig as templating engine and I am really loving it. However, now I have run in a situation which definitely mustbe accomplishable in a simpler way than I have found. What I have right now is this: {% for myVar in someArray %} {%…
sprain
  • 7,552
  • 6
  • 35
  • 49
240
votes
11 answers

How to render a DateTime object in a Twig template

One of my fields in one of my entities is a "datetime" variable. How can I convert this field into a string to render in a browser? Here is a code snippet: {% for game in games %} ... {{game.gameTeamIdOne.teamName}} …
Chris Ridmann
  • 2,836
  • 4
  • 18
  • 19
208
votes
5 answers

How to display string that contains HTML in twig template?

How can I display a string that contains HTML tags in twig template? My PHP variable contains this html and text: $word = ' a word '; When I do this in my twig template: {{ word }} I get this: <b> a word <b> I want this…
Gildas Ross
  • 3,812
  • 5
  • 21
  • 31
207
votes
8 answers

Get current URL in Twig template?

I looked around for the code to get the current path in a Twig template (and not the full URL), i.e. I don't want http://www.sitename.com/page, I only need /page.
Mark Cibor
  • 2,737
  • 4
  • 21
  • 23
202
votes
5 answers

Twig ternary operator, Shorthand if-then-else

Does Twig support ternary (shorthand if-else) operator? I need some conditional logic like: {%if ability.id in company_abilities %} {%else%} {%endif%} but using shorthand in Twig.
Meliborn
  • 6,495
  • 6
  • 34
  • 53
197
votes
11 answers

AngularJS-Twig conflict with double curly braces

As you know, both angular and twig has common control construction - double curly braces. How can I change default value of Angular? I know that I can do it in Twig, but in some projects I can't, only JS.
Meliborn
  • 6,495
  • 6
  • 34
  • 53
184
votes
8 answers

How to get config parameters in Symfony2 Twig Templates

I have a Symfony2 Twig template. I want to output the value of a config parameter in this twig template (a version number). Therefore I defined the config parameter like this: parameters: app.version: 0.1.0 I'm able to use this config parameter…
Timo Haberkern
  • 4,409
  • 2
  • 27
  • 41
180
votes
4 answers

Twig for loop for arrays with keys

I use Twig and I have an array with keys like this: array[1] = "alpha" array[2] = "bravo" array[3] = "charlie" array[8] = "delta" array[9] = "echo" And I would like to get the key (1,2,3,8,9) and the content (alpha, bravo, charlie, delta, echo) in…
Guillaume
  • 8,741
  • 11
  • 49
  • 62
175
votes
15 answers

How to var_dump variables in twig templates?

View layer pattern where you only present what you have been given is fine and all, but how do you know what is available? Is there a "list all defined variables" functionality in TWIG? Is there a way to dump a variable? The solution I found by…
Alexander Morland
  • 6,356
  • 7
  • 32
  • 51
169
votes
1 answer

Find substring in the string in TWIG

I want to find substring of the string or check if there is no such substring using Twig. On the words, I need analogue of 'strstr' or 'strpos' in php. I googled and searched this issue in stackoverflow but nothing found. Does someone know how to…
user1440167
  • 1,913
  • 2
  • 12
  • 12
154
votes
7 answers

How to access class constants in Twig?

I have a few class constants in my entity class, e.g.: class Entity { const TYPE_PERSON = 0; const TYPE_COMPANY = 1; } In normal PHP I often do if($var == Entity::TYPE_PERSON) and I would like to do this kind of stuff in Twig. Is it…
canni
  • 5,737
  • 9
  • 46
  • 68
149
votes
16 answers

symfony 2 twig limit the length of the text and put three dots

How can I limit the length of the text, e.g., 50, and put three dots in the display? {% if myentity.text|length > 50 %} {% block td_text %} {{ myentity.text}}{% endblock %} {%endif%}
GRafoKI
  • 1,515
  • 2
  • 9
  • 8
146
votes
12 answers

Setting element of array from Twig

How can I set member of an already existing array from Twig? I tried doing it next way: {% set arr['element'] = 'value' %} but I got the following error: Unexpected token "punctuation" of value "[" ("end of statement block" expected) in ...
falinsky
  • 7,229
  • 3
  • 32
  • 56
1
2 3
99 100