简体   繁体   中英

Symfony2 pass variable from own bundle to twig

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 }}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM