4

I'm developing a third party bundle.

I need to define a variable that can be used in a twig template for this bundle.

when trying to declare in my bundle config.yml mode in which the variables step twig templates on my projects,

twig:
    globals:
        test_vars: %test_vars%

I get this error.

InvalidArgumentException in YamlFileLoader.php line 357:
There is no extension able to load the configuration for "twig" (in /home/domain.ext/vendor/test/test-bundle/test/TestBundle/DependencyInjection/../Resources/config/.yml). Looked for namespace "twig", found none

thanks a lot


Solution code, thanks to @alexander.polomodov and @mblaettermann

GlobalsExtension.php

namespace Vendor\MyBundle\Twig\Extension;

class GlobalsExtension extends \Twig_Extension {

    public function __construct($parameter) {
        $this->parameter= $parameter;
        //...
    }

    public function getGlobals() {

        return array(
            'parameter' => $this->parameter
            //...
        );
    }

    public function getName() {
        return 'MyBundle:GlobalsExtension';
    }
}

my.yml

services:
    twig.extension.globals_extension:
        class: Vendor\MyBundle\Twig\Extension\GlobalsExtension
        arguments: [%my.var%]
        tags:
            - { name: twig.extension }

my.html.twig

my parameter: {{ parameter }}
2

2 Answers 2

2

you should implement this logic completely in your own bundle using dependency injection. This means, not to hijack the twig: config key, but use your own bundle config key.

In your bundles Container Extension you can pass your configuration values into container parameters which are then passed to the Twig Extension as a Constructor Arguments.

However you need to check if the Twig Bundle is loaded and available before adding your Twig Extension to the container as Alex already pointed out.

http://symfony.com/doc/current/cookbook/templating/twig_extension.html

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe a code example would have been more instructive, I updated my question adding the answer with the code example. Thanks for your answer ;)
I was on mobile phone in train only. Sorry.
Thank you very much, it was enough to know how to deal with it.
1

I had the same case (passing an own bundle config value to a twig template), my actually working solution is to pass the config value as a twig global in my bundle extension :

1 - your bundle's extension should extends PrependExtensionInterface, see https://symfony.com/doc/current/bundles/prepend_extension.html

2 - you implement prepend method doing this :

    public function prepend(ContainerBuilder $container)
    {
// get configuration from config files
        $configs     = $container->getExtensionConfig($this->getAlias());
        $config      = $this->processConfiguration(new Configuration(), $configs);

// put your config value in an array to be passed in twig bundle
        $twigGlobals = [
            'globals' => [
                'my_global_twig_variable_name' => $config['myConfigKey'],
            ],
        ];
// pass the array to twig bundle
        $container->prependExtensionConfig('twig', $twigGlobals);
    }

Then you can use my_global_twig_variable_name in twig.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.