8000 merged branch GromNaN/templating-var (PR #7951) · unframework/symfony@ea913e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea913e0

Browse files
committed
merged branch GromNaN/templating-var (PR symfony#7951)
This PR was squashed before being merged into the master branch (closes symfony#7951). Discussion ---------- [Templating] Allows "template" and "parameters" as parameter name (replaces symfony#7908) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | n/a Allows "template" and "parameters" as parameter name. **BC break:** These variables were previously declared on every template with respectively the Templating\Storage instance and the array of parameters. Commits ------- 6e8b918 [Templating] Allows "template" and "parameters" as parameter name (replaces symfony#7908)
2 parents 5f16e99 + 6e8b918 commit ea913e0

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

src/Symfony/Component/Templating/PhpEngine.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class PhpEngine implements EngineInterface, \ArrayAccess
4242
protected $globals;
4343
protected $parser;
4444

45+
private $evalTemplate;
46+
private $evalParameters;
47+
4548
/**
4649
* Constructor.
4750
*
@@ -156,24 +159,36 @@ public function supports($name)
156159
*/
157160
protected function evaluate(Storage $template, array $parameters = array())
158161
{
159-
$__template__ = $template;
162+
$this->evalTemplate = $template;
163+
$this->evalParameters = $parameters;
164+
unset($template, $parameters);
160165

161-
if (isset($parameters['__template__'])) {
162-
throw new \InvalidArgumentException('Invalid parameter (__template__)');
166+
if (isset($this->evalParameters['this'])) {
167+
throw new \InvalidArgumentException('Invalid parameter (this)');
168+
}
169+
if (isset($this->evalParameters['view'])) {
170+
throw new \InvalidArgumentException('Invalid parameter (view)');
163171
}
164172

165-
if ($__template__ instanceof FileStorage) {
166-
extract($parameters, EXTR_SKIP);
167-
$view = $this;
173+
$view = $this;
174+
if ($this->evalTemplate instanceof FileStorage) {
175+
extract($this->evalParameters, EXTR_SKIP);
176+
$this->evalParameters = null;
177+
168178
ob_start();
169-
require $__template__;
179+
require $this->evalTemplate;
180+
181+
$this->evalTemplate = null;
170182

171183
return ob_get_clean();
172-
} elseif ($__template__ instanceof StringStorage) {
173-
extract($parameters, EXTR_SKIP);
174-
$view = $this;
184+
} elseif ($this->evalTemplate instanceof StringStorage) {
185+
extract($this->evalParameters, EXTR_SKIP);
186+
$this->evalParameters = null;
187+
175188
ob_start();
176-
eval('; ?>'.$__template__.'<?php ;');
189+
eval('; ?>'.$this->evalTemplate.'<?php ;');
190+
191+
$this->evalTemplate = null;
177192

178193
return ob_get_clean();
179194
}

src/Symfony/Component/Templating/Tests/PhpEngineTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ public function testExtendRender()
116116
$this->assertEquals('bar-foo-', $engine->render('foo.php', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
117117
}
118118

119+
public function testRenderParameter()
120+
{
121+
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
122+
$this->loader->setTemplate('foo.php', '<?php echo $template . $parameters ?>');
123+
$this->assertEquals('foobar', $engine->render('foo.php', array('template' => 'foo', 'parameters' => 'bar')), '->render() extract variables');
124+
}
125+
126+
/**
127+
* @expectedException \InvalidArgumentException
128+
* @dataProvider forbiddenParameterNames
129+
*/
130+
public function testRenderForbiddenParameter($name)
131+
{
132+
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
133+
$this->loader->setTemplate('foo.php', 'bar');
134+
$engine->render('foo.php', array($name => 'foo'));
135+
}
136+
137+
public function forbiddenParameterNames()
138+
{
139+
return array(
140+
array('this'),
141+
array('view'),
142+
);
143+
}
144+
119145
public function testEscape()
120146
{
121147
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);

0 commit comments

Comments
 (0)
0