added global variables

This commit is contained in:
Fabien Potencier
2010-12-19 14:01:21 +01:00
parent 7fc5927721
commit afa47bd9a4
10 changed files with 81 additions and 4 deletions
+1
View File
@@ -6,6 +6,7 @@ Backward incompatibilities:
Changes:
* added global variables support
* made macros return their value instead of echoing directly (fixes calling a macro in sandbox mode)
* added the "from" tag to import macros as functions
* added support for functions (a function is just syntactic sugar for a getAttribute() call)
+22
View File
@@ -27,6 +27,7 @@ class Twig_Environment
protected $visitors;
protected $filters;
protected $tests;
protected $globals;
protected $runtimeInitialized;
protected $loadedTemplates;
protected $strictVariables;
@@ -454,6 +455,27 @@ class Twig_Environment
return $this->tests;
}
public function addGlobal($name, $value)
{
if (null === $this->globals) {
$this->getGlobals();
}
$this->globals[$name] = $value;
}
public function getGlobals()
{
if (null === $this->globals) {
$this->globals = array();
foreach ($this->getExtensions() as $extension) {
$this->globals = array_merge($this->globals, $extension->getGlobals());
}
}
return $this->globals;
}
public function getUnaryOperators()
{
if (null === $this->unaryOperators) {
+10
View File
@@ -70,4 +70,14 @@ abstract class Twig_Extension implements Twig_ExtensionInterface
{
return array();
}
/**
* Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions
*/
public function getGlobals()
{
return array();
}
}
+7
View File
@@ -61,6 +61,13 @@ interface Twig_ExtensionInterface
*/
public function getOperators();
/**
* Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions
*/
public function getGlobals();
/**
* Returns the name of the extension.
*
+2 -2
View File
@@ -38,7 +38,7 @@ class Twig_Node_Macro extends Twig_Node
->addDebugInfo($this)
->write(sprintf("public function get%s(%s)\n", $this->getAttribute('name'), implode(', ', $arguments)), "{\n")
->indent()
->write("\$context = array(\n")
->write("\$context = array_merge(\$this->env->getGlobals(), array(\n")
->indent()
;
@@ -53,7 +53,7 @@ class Twig_Node_Macro extends Twig_Node
$compiler
->outdent()
->write(");\n\n")
->write("));\n\n")
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->raw("\n")
+2
View File
@@ -102,6 +102,8 @@ class Twig_Node_Module extends Twig_Node
protected function compileDisplayBody($compiler)
{
$compiler->write("\$context = array_merge(\$this->env->getGlobals(), \$context);\n\n");
if (null !== $this->getNode('parent')) {
// remove all but import nodes
foreach ($this->getNode('body') as $node) {
+25
View File
@@ -0,0 +1,25 @@
--TEST--
global variables
--TEMPLATE--
{{ foo() }}
{{ bar() }}
{% include "included.twig" %}
{% from "included.twig" import foobar %}
{{ foobar() }}
--TEMPLATE(included.twig)--
{% macro foobar() %}
{{ foo() }}
{% endmacro %}
{{ foo() }}
--DATA--
$twig->addGlobal('foo', new Twig_Function(new Foo(), 'getFoo'));
$twig->addGlobal('bar', new Twig_Function('barObj', 'getFoo'));
return array('barObj' => new Foo());
--EXPECT--
foo
foo
foo
foo
+2 -2
View File
@@ -46,9 +46,9 @@ class Twig_Tests_Node_MacroTest extends Twig_Tests_Node_TestCase
array($node, <<<EOF
public function getfoo(\$foo = null)
{
\$context = array(
\$context = array_merge(\$this->env->getGlobals(), array(
"foo" => \$foo,
);
));
ob_start();
echo "foo";
+6
View File
@@ -69,6 +69,8 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
{
public function display(array \$context, array \$blocks = array())
{
\$context = array_merge(\$this->env->getGlobals(), \$context);
echo "foo";
}
@@ -105,6 +107,8 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
public function display(array \$context, array \$blocks = array())
{
\$context = array_merge(\$this->env->getGlobals(), \$context);
\$context['macro'] = \$this->env->loadTemplate("foo.twig", true);
\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
}
@@ -148,6 +152,8 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
public function display(array \$context, array \$blocks = array())
{
\$context = array_merge(\$this->env->getGlobals(), \$context);
\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
}
@@ -68,6 +68,8 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
public function display(array \$context, array \$blocks = array())
{
\$this->checkSecurity();
\$context = array_merge(\$this->env->getGlobals(), \$context);
echo "foo";
}
@@ -114,6 +116,8 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
public function display(array \$context, array \$blocks = array())
{
\$context = array_merge(\$this->env->getGlobals(), \$context);
\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
}