Skip to content

Instantly share code, notes, and snippets.

@merk
Last active December 16, 2015 07:49
Show Gist options
  • Save merk/5401836 to your computer and use it in GitHub Desktop.
Save merk/5401836 to your computer and use it in GitHub Desktop.
Overriding or adding variables to the twig app variable
parameters:
templating.globals.class: Ibms\Helper\GlobalVariables
<?php
namespace Ibms\Helper;
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables as BaseGlobalVariables;
use Symfony\Component\DependencyInjection\ContainerInterface;
use DateTime;
class GlobalVariables extends BaseGlobalVariables
{
/**
* Stores a DateTime instance.
*
* @var DateTime
*/
private $dateTime;
/**
* Stores an array of date and time formats to be used
* in the appliaction.
*
* @var array
*/
private $formats;
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
$this->dateTime = new DateTime;
}
public function getDate()
{
return $this->dateTime;
}
public function getRootDir()
{
return $this->container->get('kernel')->getRootDir();
}
public function getFormats()
{
if (!$this->formats) {
$this->formats = array(
'date' => $this->container->getParameter('format.date'),
'datetime' => $this->container->getParameter('format.datetime'),
'shortdate' => $this->container->getParameter('format.shortdate'),
'time' => $this->container->getParameter('format.time'),
);
}
return $this->formats;
}
}
@trsteel88
Copy link

This doesn't seem to work in 2.7. It uses https://github.com/symfony/TwigBridge/blob/master/AppVariable.php now. However, I can't find where this is set. Do you know where this is set @merk?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment