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)
protected $visitors;
protected $filters;
protected $tests;
+ protected $globals;
protected $runtimeInitialized;
protected $loadedTemplates;
protected $strictVariables;
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) {
{
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();
+ }
}
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.
*
* @return string The extension name
->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()
;
$compiler
->outdent()
- ->write(");\n\n")
+ ->write("));\n\n")
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->raw("\n")
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) {
--- /dev/null
+--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
array($node, <<<EOF
public function getfoo(\$foo = null)
{
- \$context = array(
+ \$context = array_merge(\$this->env->getGlobals(), array(
"foo" => \$foo,
- );
+ ));
ob_start();
echo "foo";
{
public function display(array \$context, array \$blocks = array())
{
+ \$context = array_merge(\$this->env->getGlobals(), \$context);
+
echo "foo";
}
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));
}
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));
}
public function display(array \$context, array \$blocks = array())
{
\$this->checkSecurity();
+ \$context = array_merge(\$this->env->getGlobals(), \$context);
+
echo "foo";
}
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));
}