Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
SmartyBundle - Assign variables to every template
<?php
namespace Vendorx;
class GlobalVariablesContainer
{
protected $vars;
public function __construct($three = 3)
{
$vars = array(
'one' => 1,
'two' => 2,
'three' => $three
);
}
public function getVars()
{
return $this->vars;
}
}
<?php
namespace Vendorx;
use NoiseLabs\Bundle\SmartyBundle\Extension\AbstractExtension;
class GlobalVariablesExtension extends AbstractExtension
{
public function __construct(GlobalVariablesContainer $gvc)
{
$this->gvc = $gvc;
}
public function getGlobals()
{
return array('_vars' => $this->gvc->getVars());
}
public function getName()
{
return 'vendorx_global_vars';
}
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="vendorx.global_variables_container">
</service>
<service id="vendorx.smartybundle_extension.globals">
<argument type="service" id="vendorx.global_variables_container">
<tag name="smarty.extension" />
</service>
</services>
</container>
<h1>One is {$_vars.one}</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment