Questions tagged [twig-extension]

Twig extension is about custom type defined about Twig template PHP engine. About Common additional features for Twig that do not directly belong in core

can be extended in many ways; you can add extra tags, filters, tests, operators, global variables, and functions. You can even extend the parser itself with node visitors.

As a rule of thumb, use functions for frequently used features and global objects for everything else.

Keep in mind the following type available when you want to extend Twig:

  • global: Helper object
  • function: Content generation
  • filter: Value transformation
  • tag: DSL language construct
  • test: Boolean decision
  • operator: Values transformation

Detailed documentation about extending twig is available here

186 questions
28
votes
2 answers

How to install the Intl extension for Twig

The Intl extension is an extension for Twig that adds the localizeddate, localizednumber and localizedcurrency filters. How can I install and set up the extension so that I can use those filters in my Twig templates?
julestruong
  • 2,303
  • 3
  • 18
  • 29
24
votes
5 answers

Using an environment variable (from `.env` file) in custom Twig function in Symfony 4

How can I use an environment variable from the .env file in a custom Twig function (\Twig_SimpleFunction) in Symfony 4?
jpyzio
  • 352
  • 1
  • 3
  • 12
9
votes
3 answers

How to get days difference in twig

how to get days diffrence from current day. tweetedAt: "2015-02-22 09:56:42". twig {% for key,value in data.about %} {% set tweets_date=(value.tweetedAt|date).date("now").format('%a') %} {% endfor %} I have also tried {% set…
afeef
  • 507
  • 3
  • 7
  • 22
7
votes
4 answers

The filter "truncate" (of twig) does not exist in a view of a vendor bundle

I am trying to use the bundle JMS Translation but I have this error. It is I guess because my version of twig is not the same the one of the bundle? What to do? CRITICAL - Uncaught PHP Exception Twig_Error_Syntax: "The filter "truncate" does not…
virtualmail
  • 193
  • 1
  • 3
  • 10
6
votes
4 answers

Symfony - Unable to register extension "AppBundle\Twig\Extension\FileExtension" as it is already registered

When I try to implement custom Twig Extension in Symfony2-project, I get following error: Unable to register extension "AppBundle\Twig\Extension\FileExtension" as it is already registered. In my app/config/services.yml, I have…
liestack
  • 61
  • 1
  • 2
6
votes
3 answers

Wrap contextual html around a specific twig variable {{ product.name }}

I want to automatically wrap some html, lets say when I call {{ product.name }} in my twig template. So when in a twig template, I use {{ product.name }}, I want the output to be:
Steffen Brem
  • 1,662
  • 17
  • 26
6
votes
3 answers

Custom trans filter in Twig and Symfony2

I need to overide standart Twig trans filter for my special purposes, e.g. I want to getting my translations from custom storage. So I dont know what I need to do. I try to figure out in docs. But there is no description about translations. I found…
nowiko
  • 2,349
  • 6
  • 29
  • 69
6
votes
4 answers

How to have a global variable coming from db in symfony template?

How can I have a global variable in symfony template? I did read this but I prefer to fetch parameter from database, I think this service will be loaded on startup before it can fetch anything from db. Is it possible to do a trick to do so?
user4271704
  • 651
  • 1
  • 9
  • 28
5
votes
1 answer

Symfony Twig Extension breaks other service - Is templating done before security?

I am working on a Symfony 2.7 WebApp. One of the bundles I created includes a service that offer some user related stuff, e.g. userHasPurchases(). Problem is, that including a Twig Extesion breaks another service: AppShopService namespace…
Andrei Herford
  • 15,149
  • 16
  • 73
  • 171
5
votes
1 answer

Twig : url_decode

I encoded an url parameter with the twig filter url_encode. // app.request.query.get("date") output 01/04/2016 href="{{ path('page', {date: app.request.query.get("date")|url_encode}) }}"> which output in the url date=01%252F04%252F2016 So in the…
Brieuc
  • 3,361
  • 7
  • 34
  • 65
4
votes
1 answer

SlimPHP v3 how to display flash message on view

In their new documentation there isn't anything for flash messages. I installed the flash extension from their github repository (slimphp/Slim-Flash). Everything works fine, I can add messages and can also get these messages. // Adding a…
deflox
  • 93
  • 3
  • 8
4
votes
2 answers

Passing an array to an include in twig

Seems like the argument is not passed properly ... {% set items = { item: { 'id': '1', 'brand': 'client1', 'description': 'solutions.client1.description' | trans}, item: { 'id': …
Romainpetit
  • 760
  • 9
  • 28
4
votes
1 answer

How let Twig write the HTML output to a file, instead to a browser?

I have a Twig template (.html file) that I want to render (replace the tokens with values from my PHP file). Instead of sending the rendered result to a browser, however, I want to write it to a new .html file. I would imagine the solution would…
ReneFroger
  • 446
  • 7
  • 19
3
votes
2 answers

Install twig/extensions on a Symfony 4 or Symfony 5 new project

I'm preparing to migrate a huge website from Symfony 3.4 to Symfony 4.4. To do this, I'm starting from a new fresh installation of Symfony 4.4, and as the intial project requires the use of Twig Extensions I try to install it on this new Symfony 4.4…
Grégory C
  • 345
  • 2
  • 4
  • 19
3
votes
1 answer

How to increase decimal precision on percentage format using Twig's Intl extension?

I have a decimal number with 4 digits and a scale of 4. (Max is 0.9999 and min is 0.0000) I'm using Twig and its intl extension. When I want to render a percent number, decimals are rounded. {% set decimal = 0.0850 %} {{…
Alexandre Tranchant
  • 3,686
  • 4
  • 31
  • 56
1
2 3
12 13