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

203 questions
32
votes
7 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?
Jakub Maj
  • 531
  • 2
  • 6
  • 12
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?
julio
  • 2,762
  • 4
  • 22
  • 34
10
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
  • 547
  • 3
  • 9
  • 25
8
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
  • 81
  • 1
  • 2
7
votes
2 answers

Register Twig Extension in Symfony

I want to use nochso/html-compress-twig extension to compress all html, inline css and js.But it is the first time that I register a new extension on Twig and I am bit confused about where I should add the following lines in my project: $twig = new…
Kevin Gravell
  • 499
  • 2
  • 7
  • 20
7
votes
2 answers

Timber (Twig) how to call wordpress functions

In Timber is this function {{post}} where you get things like {{post.post_title}} but there are other functions in WordPress like get_the_permalink(); which are not accessible from post.get_the_permalink or post.get_permalink these are the…
Noel Schenk
  • 724
  • 1
  • 8
  • 19
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…
user849017
  • 193
  • 1
  • 3
  • 10
7
votes
3 answers

Custom trans filter in Twig and Symfony2

I need to override the standard Twig trans filter for my own purposes, i.e I want to get my translations from a custom storage. I tried to figure it out in the docs. But there is no details about translations. I found the…
nowiko
  • 2,507
  • 6
  • 38
  • 82
6
votes
1 answer

How to replace the "localizeddate" filter from twig/extensions

I'm currently updating my projects to Symfony 5.1 (where I was previously using Symfony 4.1). When updating I noticed that I can't install the package twig/extensions anymore, which means I can't use the |localizeddate filter. This has basically…
D3strukt0r
  • 571
  • 1
  • 4
  • 19
6
votes
1 answer

allow new methods in sandbox policy in twig (drupal 8)

I am getting this error when using the "field collection" module : Twig_Sandbox_SecurityError: Calling "uri" method on a "Drupal\field_collection\Entity\FieldCollectionItem" object is not allowed in…
Asma Supih
  • 61
  • 6
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,738
  • 18
  • 29
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
  • 723
  • 1
  • 12
  • 37
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
  • 17,570
  • 19
  • 91
  • 225
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,994
  • 9
  • 37
  • 70
5
votes
4 answers

Twig - display template names if debug mode is enabled

I'm using Twig lately and I was wondering if it is possible to output the template names which are loaded on the page. The best way I can think of is to display the name above the template itself as a html comment.