2

It's easy to set a global static parameters available to all Twig templates, e.g.: How to get config parameters in Symfony2 Twig Templates

But say, I want to display everywhere the present year on the pages (for instance, in the footer for a copyright) - this can be got e.g. in PHP by:

date("Y")

Now, how to make it available to the all twig templates, without a need to pass it in every controller?

Community
  • 1
  • 1
forsberg
  • 1,681
  • 1
  • 21
  • 27

1 Answers1

3

You should create a Twig extension. At that link you can see how to create one and register it within Symfony. It doesn't show you how to define global through it but the process is really simple and described here.

Chewed down

Basically, you just need to create a class extending \Twig_Extension. Then override default getGlobals method in base extension class and provide your globals through it:

<?php

namespace MyBundle\Twig;

class MyTwigExtension extends \Twig_Extension
{
    public function getGlobals()
    {
        return [
            'current_year' => (new \DateTime())->format('Y'),
        ];
    }    
}

After that simply register it in your services.yml or whatever format you're using:

services:
    mybundle.twig.my_twig_extension:
        class: MyBundle\Twig\MyTwigExtension
        tags:
            - { name: twig.extension }

Update based on comment

If you need to access that value in other parts of the code, it can be a bit more tricky, and there are a couple of ways of dealing with this.

My suggestion is that you create a service that will provide you the values you need:

<?php

namespace MyBundle\Util;

class CurrentYearProvider
{
    public function getCurrentYear()
    {
        return (new \DateTime())->format('Y');
    }
}

and register it as a service of course:

services:

    current_year_provider:
        class: MyBundle\Util\CurrentYearProvider

After that you should just inject it in twig extension:

services:
    mybundle.twig.my_twig_extension:
        class: MyBundle\Twig\MyTwigExtension
        arguments: [@current_year_provider]
        tags:
            - { name: twig.extension }

And use it to fetch the value:

<?php

namespace MyBundle\Twig;

class MyTwigExtension extends \Twig_Extension
{
    private $yearProvider;

    public function __construct(CurrentYearProvider $yearProvider)
    {
        $this->yearProvider = $yearProvider;
    }

    public function getGlobals()
    {
        return [
            'current_year' => $this->yearProvider->getCurrentYear(),
        ];
    }    
}

And if you need it in controller for example:

<?php

namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
    public function myAction()
    {
        $currentYear = $this->get('current_year_provider')->getCurrentYear(); 
    }
}

This, however, seems quite silly if you just need to get current year. In my opinion.

Igor Pantović
  • 9,107
  • 2
  • 30
  • 43
  • For just a year, yes it is. ;) But if you have a number of related parameters, and perhaps (now I think of it so) related methods that might be of help, perhaps making a parameter a service is not that bad idea at all. After all, the global parameters are being accessed through use of a service (container), aren't they. Thanks. – forsberg Oct 14 '14 at 18:50