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

{{ exception.message }} in Twig doesn't render HTML

I have this controller where \Exception is raised (I haven't figured out which SF2 Exception to use yet) upon certain condition. Here is it:
D4V1D
  • 5,805
  • 3
  • 30
  • 65
3
votes
1 answer

Is there any way in Twig to determine if a method/parameter exists without putting ifs everywhere?

When using Symfony2 and Twig together, if you need to do this: {{ myObject.method.parameter }} or just {{ myObject.parameter }} If what the twig file is trying to access is not set, an error will be thrown. Is there a cleaner way…
timhc22
  • 7,213
  • 8
  • 48
  • 66
3
votes
1 answer

Parse txt file using Twig

I am using Symfony2 with twig in it and I have large files (>100MB), which I want to parse and write some information from it into the HTML page. If I use my own Twig extension I get a memory exhaustion error. So is there a way to parse txt file…
H.W.
  • 343
  • 1
  • 5
  • 21
3
votes
1 answer

Render multi-dimensional arrays in TWIG

I am fetching associated objects through 3 tables (“User”,” Client”,” Account”). “User” has a one-to-many relationship to “Client”, “Client” has a one-to-many relationship to “Account”. I fetch all the “Accounts” for one specific “User” using the…
Bastien
  • 596
  • 1
  • 11
  • 30
3
votes
1 answer

Relative path to Bundle in Twig

I have got standard Extend in Twig file: {% extends 'AcmeMyBundle:Layout:defaultLayout.html.twig' %} Can I call this template file by relative name of bundle? eg. {% extends '.:Layout:defaultLayout.html.twig' %} ^ //…
123qwe
  • 1,525
  • 1
  • 14
  • 16
3
votes
1 answer

Twig internals: setCompiler

I'm having trouble properly changing from the default compiler to a custom compiler. It looks like I may not be creating the class or object correctly. From http://twig.sensiolabs.org/doc/internals.html "The default compiler (Twig_Compiler) can be…
d_edmonton
  • 31
  • 1
3
votes
2 answers

Concatenation with dynamic variables for Twig symfony

I have a specific problem with concat of twig. When I trying to concatenate dynamic variables showing the error. Here is my code : {% set i = 0 %} {% set nbLignes = codeEvt.nb_lignes_~i %} {% set nbLignesRef = codeEvt.nb_lignes_ref_~i %} But I…
alexandre bru
  • 103
  • 2
  • 9
3
votes
1 answer

Symfony Gaufrette - Download or Read Image from filesystem and show it in twig

my question is simple. I use Symfony framework and Gaufrette filesystem (KnpGaufretteBundle) and I allready saved files (images) in filesystem. Now I want show that images in twig. In controller I get the file: $image =…
user1336101
  • 441
  • 1
  • 7
  • 17
3
votes
2 answers

Twig: while-workaround

I've already a solution, but just for JavaScript. Unfortunately while-loops do not exist in Twig. My Twig-target in JavaScript: var x = 10; // this is an unknown number var result = x; while (100 % result !== 0) { …
ReeCube
  • 2,545
  • 17
  • 23
3
votes
1 answer

Symfony2 get request in action called in twig

I have a twig template with : {% render controller('MyBundle:Default:leftside') %} So i have an action leftside in my controller : public function leftsideAction(Request $request) I'm trying, in this action to retrieve GET parameters with :…
Clément Andraud
  • 9,103
  • 25
  • 80
  • 158
3
votes
2 answers

Passing variable to Twig through BuildView Method Symfony2

I was looking around for a solution, but I cannot find any in stackoverflow. The following problem is that I want to pass an object to a twig template. I have a class which extends the AbstractType of Symfony2 Core. Then I understood that I have to…
AugustusS
  • 43
  • 6
3
votes
3 answers

How to handle special symbols in twig

What I need I need to show special symbols in view page. say name: Sara Fernström oksss. php code {% set name=value.metadata.name %}

{%if name|length < 40 %} {% set strategy = 'html' %} {% autoescape 'html' %} …

afeef
  • 547
  • 3
  • 9
  • 25
3
votes
1 answer

Allow bundle to extend asset configuration of Assetic

I'm currently struggling with this requirement: We've got a modular app which where modules can be enabled by the customer on demand. Each module is a bundle. Now we've got some kind of dashboard where I'd like to inject assets (JS/CSS) based on the…
althaus
  • 2,009
  • 2
  • 25
  • 33
3
votes
4 answers

Display image stored in BLOB database in symfony

I load my image (blob data ) in my GETer Entity When I just return ($this->foto) in my GETer I see :Resource id #284 on the screen When I change my GETer like this : return stream_get_contents($this->foto); I see these : ���JFIF��� ( ,,,,,,,, (…
Sois
  • 33
  • 1
  • 1
  • 4
3
votes
1 answer

Filter not exit in my custom Twig extension

Followed http://symfony.com/doc/current/cookbook/templating/twig_extension.html to create my custom Twig filter to be used in my Symfony 2 project. Everything seems fine but when loading the page, it says: The filter "tss" does not exist in…
TaylorR
  • 3,746
  • 5
  • 25
  • 42
1 2 3
99
100