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

Symfony2 Expaned table with checkboxes form

I have a table which contains information for example "ID", "Category", "Name" and "Action". Now I want to improve the table with checkboxes which enables the user to choose multiple rows and to delete them in one step. My problem: I have created a…
Atreides78
  • 577
  • 1
  • 7
  • 22
3
votes
1 answer

How to retrieve a route pattern from a route name in a Symfony Controller

I would like to basically get the same result as Twig's function path(routeName) but not in the view.html.twig but inside a controller itself. routing.yml : MyBundle_route_first: pattern: /dummy/first/{foo} defaults: { _controller:…
Pierre Roudaut
  • 1,013
  • 1
  • 18
  • 32
3
votes
1 answer

iterate and print all date in range in twig

I am trying to iterate all dates in twig I have tried using this: {% for x in range(startDate|date('U'), endDate|date('U'), 86400 ) %} {{ x|date('m/d') }} {% endfor %} It worked in some dates. but if my startDate is 10/01/2015 and endDate is…
kimondoe
  • 567
  • 2
  • 9
  • 27
3
votes
1 answer

Twig tag or function inside PHP String

Is there any way to use something like this? $foo = "{{ object|filter }}"; Because I'm trying to write a dynamic image converter that needs to output something like the example, but when in my twig I use {{ foo }}, it just outputs a raw string {{…
Robert W. Hunter
  • 2,895
  • 7
  • 35
  • 73
3
votes
3 answers

Symfony2 - Blank pages on php errors and 404's

In my production environment, symfony2 is showing a blank page on 404's and a 503 Service Unavailable on general php errors. I have custom TwigTemplates under app/Resources/TwigBundle/views/Exception/error|error404|error500.html.twig When looking in…
xfscrypt
  • 16
  • 5
  • 28
  • 59
3
votes
1 answer

gender related translations with transchoice and lingohub

I have been trying to find a solution for this but have not found the answer that works for me. Basically what I want are gender specific translations in Symfony2 using twig and the service lingohub. Our translations files (XLIFF format) for English…
Eve
  • 79
  • 1
  • 6
3
votes
3 answers

Symfony2. Twig: Doesn't work logic function when date variable changes

{% set event_date = event.schedule|date('d-m-y') %} {% set nowdate = "now"|date('d-m-y') %} {% if event_date < nowdate %} view some 1 {% else %} view some 2 {% endif %} If change event_date bigger or lower than nowdate, have one result:…
chrisperk
  • 47
  • 8
3
votes
2 answers

SImple Symfony2 'Front Page' controller, router & template. Why do I still see the Welcome page?

I'm just starting with Symfony 2.7.3. I've been reading "the Book", trying to piece the new-to-me concepts together. I've put together my 1st simple example -- my 'Front Page'. With the code below, I' expecting to see a blank page with just the…
unknown
  • 45
  • 8
3
votes
0 answers

Response time is too long symfony2 app

I have a problem with response time which is too high. For instance, I have a simple controller which returns only text (no images). In sf Profiler I got these values Main Request - 2155 ms Is it normal that kernel.view/twig eats almost 1400 ms…
spezia
  • 183
  • 1
  • 1
  • 7
3
votes
1 answer

My Twig template's parameters are ignored

I'm using a standalone Twig instance in a custom application, but parameters (context) passed to the template seem to be surprisingly ignored. Executing the following code results in this error : Fatal error: Uncaught exception 'Twig_Error_Runtime'…
Profet
  • 944
  • 1
  • 15
  • 22
3
votes
2 answers

Twig escaping HTML and rendering as raw

I hope someone can assist me on this issue. I am pulling details from a database to display on a twig template (using Symfony2) but the way in which it is saved in the db makes it difficult to interpret the HTML. Basically, the HTML tags are already…
Michael Emerson
  • 1,774
  • 4
  • 31
  • 71
3
votes
1 answer

Pass an array of a twig for a jQuery

I have a block on my twig writing a table from a variable received from controller {% block foot_informations %} {% if ads is not empty %}
Marcius Leandro
  • 775
  • 1
  • 11
  • 34
3
votes
1 answer

Gulp/Symfony2: How can I avoid overwriting my js/css imports when cache busting?

I have a gulp build process for my Symfony2 application and am trying to integrate cache busting, but have hit a roadblock. I use gulp-rev to append a hash to my concatenated javascript file and output a manifest file to map between the original and…
lukeo05
  • 1,833
  • 2
  • 14
  • 13
3
votes
1 answer

Twig: how to check if a variable is a DateTime object

I have to check if a variable is a DateTimeobject or a simple string to use it in my template. If the variable is a DateTime, I have to format it as a date; simply print it if is a string. {% if post.date is DateTime %} {% set postDate =…
Aerendir
  • 6,152
  • 9
  • 55
  • 108