From e2c7b2fadd7593c9ed784c2c4d229ffa1b545166 Mon Sep 17 00:00:00 2001 From: fabien Date: Tue, 15 Dec 2009 08:01:13 +0000 Subject: [PATCH] added a way to specify variables to pass to an included template git-svn-id: http://svn.twig-project.org/trunk@190 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- CHANGELOG | 1 + doc/02-Twig-for-Template-Designers.markdown | 19 +++++++++++++++++++ lib/Twig/Node/Include.php | 19 ++++++++++++++++--- lib/Twig/TokenParser/Include.php | 13 ++++++++++--- test/unit/integrationTest.php | 2 +- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e0c9b58..a1c83f3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,6 +12,7 @@ environment constant by the "needs_environment" option: 'even' => new Twig_Filter_Function('twig_is_even_filter'), 'escape' => new Twig_Filter_Function('twig_escape_filter', array('needs_environment' => true)), + * added a way to specify variables to pass to an included template * changed the automatic-escaping rules to be more sensible and more configurable in custom filters (the documentation lists all the rules) * improved the filter system to allow object methods to be used as filters * changed the Array and String loaders to actually make use of the cache mechanism diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 17e1f7c..a2557bd 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -617,6 +617,25 @@ An included file can be evaluated in the sandbox environment by appending [twig] {% include 'user.html' sandboxed %} +You can also restrict the variables passed to the template by explicitly pass +them as an array: + + [twig] + {% include 'foo' with ['foo': 'bar'] %} + + {% set vars as ['foo': 'bar'] %} + {% include 'foo' with vars %} + +The most secure way to include a template is to use both the `sandboxed` mode, +and to pass the minimum amount of variables needed for the template to be +rendered correctly: + + [twig] + {% include 'foo' sandboxed with vars %} + +>**NOTE** +>The `with` keyword is supported as of Twig 0.9.5. + ### Import Twig supports putting often used code into macros. These macros can go into diff --git a/lib/Twig/Node/Include.php b/lib/Twig/Node/Include.php index f8f0b84..45b62df 100644 --- a/lib/Twig/Node/Include.php +++ b/lib/Twig/Node/Include.php @@ -21,13 +21,15 @@ class Twig_Node_Include extends Twig_Node { protected $expr; protected $sandboxed; + protected $variables; - public function __construct(Twig_Node_Expression $expr, $sandboxed, $lineno, $tag = null) + public function __construct(Twig_Node_Expression $expr, $sandboxed, $variables, $lineno, $tag = null) { parent::__construct($lineno, $tag); $this->expr = $expr; $this->sandboxed = $sandboxed; + $this->variables = $variables; } public function __toString() @@ -39,7 +41,7 @@ class Twig_Node_Include extends Twig_Node { if (!$compiler->getEnvironment()->hasExtension('sandbox') && $this->sandboxed) { - throw new Twig_SyntaxError('Unable to use the sanboxed attribute on an include if the sandbox extension is not enabled.'); + throw new Twig_SyntaxError('Unable to use the sanboxed attribute on an include if the sandbox extension is not enabled.', $this->lineno); } $compiler->addDebugInfo($this); @@ -56,9 +58,20 @@ class Twig_Node_Include extends Twig_Node $compiler ->write('$this->env->loadTemplate(') ->subcompile($this->expr) - ->raw(')->display($context);'."\n") + ->raw(')->display(') ; + if (null === $this->variables) + { + $compiler->raw('$context'); + } + else + { + $compiler->subcompile($this->variables); + } + + $compiler->raw(");\n"); + if ($this->sandboxed) { $compiler diff --git a/lib/Twig/TokenParser/Include.php b/lib/Twig/TokenParser/Include.php index 240d629..9e007c8 100644 --- a/lib/Twig/TokenParser/Include.php +++ b/lib/Twig/TokenParser/Include.php @@ -16,15 +16,22 @@ class Twig_TokenParser_Include extends Twig_TokenParser $expr = $this->parser->getExpressionParser()->parseExpression(); $sandboxed = false; - if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE)) + if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'sandboxed')) { - $this->parser->getStream()->expect(Twig_Token::NAME_TYPE); + $this->parser->getStream()->next(); $sandboxed = true; } + $variables = null; + if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'with')) + { + $this->parser->getStream()->next(); + $variables = $this->parser->getExpressionParser()->parseExpression(); + } + $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); - return new Twig_Node_Include($expr, $sandboxed, $token->getLine(), $this->getTag()); + return new Twig_Node_Include($expr, $sandboxed, $variables, $token->getLine(), $this->getTag()); } public function getTag() diff --git a/test/unit/integrationTest.php b/test/unit/integrationTest.php index 4310843..97ce241 100644 --- a/test/unit/integrationTest.php +++ b/test/unit/integrationTest.php @@ -51,7 +51,7 @@ class TestExtension extends Twig_Extension } } -$t = new LimeTest(58); +$t = new LimeTest(59); $fixturesDir = realpath(dirname(__FILE__).'/../fixtures/'); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) -- 1.7.2.5