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
3
votes
1 answer

Warning "Cannot reinitialise DataTable" when using Symfony2 and Twig

Every time I log on to the system there's this dialog box that keeps on popping up when running with the dev environment. DataTables warning: table id=datTable - Cannot reinitialise DataTable. But whenever I run the production environment,…
user123
  • 1,060
  • 3
  • 13
  • 29
3
votes
1 answer

Symfony render controller exception template ("")

I tried to render a controller inside twig. I follow the documentation "Embedding other Controllers". I constantly get an exeption with the method render(controller()): An exception has been thrown during the rendering of a template ("") In my…
Alexandre
  • 3,088
  • 3
  • 34
  • 53
3
votes
1 answer

Customize form_row for different form types

I want to set different form_row layout for different form type. I found in templates block named "choice_widget_collapsed", but it render only select tag with options. I cannot find where this block is being used. Actually it seems that it is…
Fisher
  • 1,712
  • 1
  • 19
  • 38
3
votes
2 answers

Symfony include navbar html into Twig template

My Symfony application has a defaultBundle which loads a default.html.twig successfully. I am trying to include the contents of a navbar.html.twig into default.html.twig. The following code is my attempt to include the navbar code: {% block body…
sisko
  • 9,604
  • 20
  • 67
  • 139
3
votes
2 answers

Symfony2 best way to organise javascript in twig template

It's not about libraries, it's about custom scripts. I know two solutions: To write everything in one big file (many functions or custom library) To write pieces of scripts in twig templates Both solutions do not really like. In the first case,…
Max Lipsky
  • 1,774
  • 1
  • 18
  • 29
3
votes
2 answers

Symfony2 wrong instance passed with twig extension

I created a Twig extension, registered it in services, but im getting an error: This is the extension:
Dominykas55
  • 1,231
  • 14
  • 45
3
votes
1 answer

How to get a value from one select list based on value selected from another list in Symfony2?

When I select a member from a Member list I need to get his default department from another select Department list on 'Add new member' form and, if necessary to change this member's department affiliation for a selected committee. I have two…
Gregg
  • 59
  • 2
  • 7
3
votes
1 answer

Create multi statement instructions in TWIG

I wonder if there is a way of creating multi statements in TWIG Example: two separate statements ... {% set foo:bar %} {% set baz:qux %} into one single statement {% set foo:bar set baz:qux %}
Nicolas
  • 1,320
  • 2
  • 16
  • 28
3
votes
2 answers

Twig: Slug with Regex in Twig?

I want to slugify a value, for example "slugName = Spain & Italy" When I make my slug in TWIG {{ slugName|slugify }}, the result is Spain_amp_Italy Is it possible to set a REGEX in Twig, so that my "&" for example will be translated in "and"…
Zwen2012
  • 3,360
  • 9
  • 40
  • 67
3
votes
2 answers

Symfony -- Twig's trans filter not picking up locale

I have verified that if I add the following line to my twig template, it outputs the desired locale: {{ app.request.locale }} However, the following is outputting in English: {{ 'String'|trans }} If I force the locale of the trans filter: {{…
Kai
  • 3,803
  • 1
  • 16
  • 33
3
votes
4 answers

How to use Mustache in Twig templates? They both have the same tag delimiters

I use Twig in my project. It uses these tags: {{ name }} I want to include Mustache in my project as well. But Mustache also uses the same tags {{ name }}, so there is a conflict and nothing works. The solution Mustache provides in their docs of…
Green
  • 28,742
  • 61
  • 158
  • 247
3
votes
1 answer

Contenttypes in Bolt CMS: How to change field used for listing in backend

I would like to change the field used for displaying in backend listings. By default the first defined field is used, here it would be title news: name: News singular_name: Newsitem fields: title: type: text …
Jensman
  • 60
  • 6
3
votes
1 answer

symfony2 / twig : how to use include in a block used in a form theme?

To process my form collections I have a custom form theme for the block collection widget. This block collection widget is rendered as a table and so depends on a block_collection_header and a block_collection_body. The block collection widget…
Sébastien
  • 5,263
  • 11
  • 55
  • 116
3
votes
0 answers

Twig_Error_Runtime - The token storage contains no authentication token

On Symfony2/production, I get this error on my log file request.CRITICAL: Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("The token storage contains no authentication token. One…
Yohann
  • 265
  • 1
  • 8
  • 17
3
votes
2 answers

Twig template - access array within array

Just started playing around with twig templates and i've ran into an issue. I got an array i'm looping over and extracting data from, within this array i've got another array (images) that I need to access, problem is I can't seem to get it to…
woolm110
  • 1,194
  • 17
  • 27
1 2 3
99
100