Code Monkey home page Code Monkey logo

Comments (16)

diimpp avatar diimpp commented on June 14, 2024 13

For who are looking into the same question, in symfony it should be registered as service.

For example for intl

# app/config/services.yml
services:
    twig.extension.intl:
        class: Twig_Extensions_Extension_Intl
        tags:
            - { name: twig.extension }

http://stackoverflow.com/a/28410279

from twig-extensions.

KojoEnch avatar KojoEnch commented on June 14, 2024 2

In Symfony 4.3 with all dependencies updated , this is now into config/packages/twig_extensions.yaml , just uncomment the line(s) for each extension you wish to activate :

services:
    _defaults:
        public: false
        autowire: true
        autoconfigure: true

    # Uncomment any lines below to activate that Twig extension
    #Twig\Extensions\ArrayExtension: ~
    #Twig\Extensions\DateExtension: ~
    #Twig\Extensions\IntlExtension: ~
    #Twig\Extensions\TextExtension: ~

from twig-extensions.

stof avatar stof commented on June 14, 2024

The line $twig = new Twig_Environment($loader); is the one where you instantiate Twig itself. Locate it in your code, and this is the place where registering the extension should be done.

If you use a framework which provides a Twig integration, this integration probably provides a way to register extensions, which is what you would need to look for.

from twig-extensions.

QuentinCurtet avatar QuentinCurtet commented on June 14, 2024

I am working with symfony 2.7
Should i put it in my controller ?

I am completely lost, i just want to put the correct format with intl but i can't use it

from twig-extensions.

stof avatar stof commented on June 14, 2024

this is exactly the case I describe above. Given that Symfony handles the twig instantiation itself, it provides a way to register extensions in the setup it does. And the Symfony documentation explains the way to register twig extensions in a Symfony setup: http://symfony.com/doc/current/cookbook/templating/twig_extension.html shows how to write and register an extension. In the case of the Twig_Extensions_Extension_Intl, the code is already written, so you only need to to the second step. (And http://symfony.com/doc/current/reference/dic_tags.html#twig-extension concerns it too)

from twig-extensions.

QuentinCurtet avatar QuentinCurtet commented on June 14, 2024

Ok but i need an "use" statement but i don't know what to put in my file?

from twig-extensions.

stof avatar stof commented on June 14, 2024

which use statement are you talking about ?

from twig-extensions.

QuentinCurtet avatar QuentinCurtet commented on June 14, 2024

i put the service in my config.yml but i have an error :

Attempted to load class "Twig_Extensions_Extension_Intl" from the global namespace.
Did you forget a "use" statement?

from twig-extensions.

stof avatar stof commented on June 14, 2024

Well, have you installed the twig-extensions package in your project ? It looks like the extension class cannot be autoloaded.

This message is displayed by Symfony when it cannot find the class after autoloading, pointing at a potential mistake. But in this case, loading it from the global namespace is logical, as the class is in the global namespace.

from twig-extensions.

QuentinCurtet avatar QuentinCurtet commented on June 14, 2024

Yes i installed it.

It seems that Symfony is looking for the twig extension class in the cache, i got this fatal error when trying to clear the cache :

Fatal Error: Class 'Twig_Extensions_Extension_Intl' not found {"type":1,"file":"/home/idci/Workspace/symposium2/app/cache/de_/ap_DevDebugProjectContainer.php","line":3398...

I found the twig extension in the vendor

from twig-extensions.

stof avatar stof commented on June 14, 2024

Well, the cache file is the place where it will try to use the class, which is totally logical.

Are you using the composer autoloader or a custom autoloader ?

from twig-extensions.

QuentinCurtet avatar QuentinCurtet commented on June 14, 2024

The composer one, i am not using any custom autoloader

from twig-extensions.

QuentinCurtet avatar QuentinCurtet commented on June 14, 2024

If it can help my application is dockerized, but i am running the composer commands inside the container so i don't see in which way it could be an issue

from twig-extensions.

QuentinCurtet avatar QuentinCurtet commented on June 14, 2024

Excuse me, but i put the extension in the bad composer.json i am a noob...
Thanks for the help!

from twig-extensions.

stof avatar stof commented on June 14, 2024

Please close this issue then (I cannot do it myself. I'm not collaborator on the repo)

from twig-extensions.

luispabon avatar luispabon commented on June 14, 2024

In addition to @KojoEnch comment above, you also need to add twig/extensions to your composer.json.

For the benefit of anybody stumbling upon this after a minor symfony twig bundle update:
The latter (>= 2.0) versions of twig finally moved their code into proper namespaces, and separated some of the extensions on a separate composer package, hence the breakage above. Solution above by @diimpp doesn't work anymore since twig bundle has introduced an unintentional bc break by working with both twig 1.x and 2.x (some will argue that there's no bc break in twig-bundle, but as far as I'm concerned bc extends also to configuration, not just class interfaces), where the old config now induces symfony to crash.

from twig-extensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.