Skip to content

Instantly share code, notes, and snippets.

@vitorbrandao
Created March 14, 2014 13:18
Show Gist options
  • Save vitorbrandao/9547521 to your computer and use it in GitHub Desktop.
Save vitorbrandao/9547521 to your computer and use it in GitHub Desktop.
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