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
82
votes
3 answers

Is there a Twig shorthand syntax for outputting conditional text

Is there a shorter syntax in Twig to output a conditional string of text?

{% if not info.id %}create{% else %}edit{% endif %}

Traditional php is even easier than this:

Justin
  • 4,203
  • 7
  • 41
  • 58
77
votes
1 answer

Symfony2 security functions in Twig? How to check the user's role?

I have Symfony 2.0.9 standard edition installed with JMSSecurityBundle. Just wondering what functions are available to me in a Twig template to deal with users? Specifically I want check to see if a user has the role ROLE_ADMIN. Any help? Thanks
Chris Tickner
  • 2,008
  • 2
  • 20
  • 24
75
votes
4 answers

Formatting money in twig templates

Are there any filters or something like that in twig template engine to format money or numbers?
umpirsky
  • 9,902
  • 13
  • 71
  • 96
74
votes
1 answer

How can I reuse a block multiple times?

How can I render a block multiple times within a view in a twig template? Currently I do have a block, let's call it something defined in an included (via render) twig file. I then include it at the layout-twig file using the following: {% block…
Chris
  • 8,031
  • 10
  • 41
  • 67
73
votes
4 answers

How to include CSS file in Symfony 2 and Twig?

I'm playing around with Symfony2, and I have problems including CSS and JS files in Twig template. I have a bundle named Webs/HomeBundle inside which I have HomeController with indexAction that renders a twig template file: public function…
Limeni
  • 4,954
  • 8
  • 31
  • 33
72
votes
10 answers

How to use absolute path in twig functions

I have an application with Symfony2 (2.2). When I want to send a mail, I'm having trouble with the paths, which are all relative paths and obviously aren't working inside emails for rendering the paths I'm using:
rkmax
  • 17,633
  • 23
  • 91
  • 176
72
votes
4 answers

How to make form_rest() not display a field with Symfony2?

I've started to use Symfony2 but I've some problems. I wanted to render fields by hand but it doesn't work because my field yet rendered by me is displayed with the form_rest() function too, so I have two same fields. Here is my code :
{{…
Ilan Coulon
  • 1,126
  • 1
  • 7
  • 14
71
votes
1 answer

What does the hyphen (dash) mean for a Twig block like in {% block body -%}?

When generating a CRUD in a Symfony2 application with Doctrine commands, generated Twig template content is defined within a Twig block this way: {% block body -%} {% endblock %} What does the hyphen (dash) in -%} mean? It works fine without the…
Michaël Perrin
  • 5,903
  • 5
  • 40
  • 65
70
votes
5 answers

Check if variable is string or array in Twig

Is it possible to check if given variable is string in Twig ? Expected solution: messages.en.yml: hello: stranger: Hello stranger ! known: Hello %name% ! Twig template: {% set title='hello.stranger' %} {% set title=['hello.known',{'%name%' :…
hsz
  • 148,279
  • 62
  • 259
  • 315
69
votes
16 answers

How to retrieve all Variables from a Twig Template?

Is it possible to retrieve all variables inside a Twig template with PHP? Example someTemplate.twig.php: Hello {{ name }}, your new email is {{ email }} Now I want to do something like this: $template =…
Stefan Neubig
  • 958
  • 1
  • 7
  • 8
68
votes
5 answers

Accessing session from TWIG template

I've searched a lot on the net how to access the global $_SESSION array from TWIG template and found this: {{app.session.get('index')}}, but when I'm calling it, it returns an empty string. I have a $_SESSION['filter']['accounts'] and I'm getting…
haynar
  • 5,961
  • 7
  • 33
  • 53
68
votes
3 answers

Id attribute on form tag symfony

I would like to define a id attribute on my symfony2 forms. I've tried with this in my twig template: {{ form_start(form, {'id': 'form_person_edit'}) }} But it seems not working.
wonzbak
  • 7,734
  • 6
  • 26
  • 25
67
votes
11 answers

Conflict on Template of Twig and Vue.js

I'm doing a program using Slim 2 that uses Twig as my templating engine. so It uses the syntax {{ foo }} in php file. On the other hand, I'm using vue.js, it also uses {{ bar }}. E.g. I'm gonna do the two way binding, below is my html code.
Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
66
votes
10 answers

Simple check if form field has errors in Twig template

In Twig template I check if a field has an error like this: {% if form.points.get('errors') is not empty %} Is there any method like: {% if form.points.hasErrors() %} to do it simpler? It's not a big difference, but if I can't do it easier why…
Dawid Ohia
  • 16,129
  • 24
  • 81
  • 95
65
votes
4 answers

How to combine two string in twig?

I want to do something like this: {% set c=a+b %} Where a and b are strings. How can I do it?
sh3211
  • 993
  • 3
  • 9
  • 9