Mohammad Fareed Mohammad Fareed - 3 months ago 13
Twig Question

create parameters in parameters.ini in Symfony2 and access in all Twig Files

How to create Constant in Symfony2 from a variable defined in

parameters.ini
so I can use that constant in all twig Files.

Example:

parameters.ini:

blog="https://www.blog.com"


Constant value declaration where I have to declare so that it accessible in all twig files.?

'blog_link' => $this->container->getParameter( 'blog' )


view:

<a href="{{blog_link}}" target="_blank">BLOG</a>

Answer

You can simply add Twig global variable like this:

#app/config/config.yml
twig:
    globals:
        blog_link: '%blog%'

Where %blog% is a parameter defined earlier in parameters.ini just like you've already did.

More info in Symfony's Cookbook