merged branch GromNaN/templating-var (PR #7951)

This PR was squashed before being merged into the master branch (closes #7951).

Discussion
----------

[Templating] Allows "template" and "parameters" as parameter name (replaces #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 #7908)
This commit is contained in:
Fabien Potencier 2013-06-04 17:16:10 +02:00
commit ea913e07e5
2 changed files with 52 additions and 11 deletions

View File

@ -42,6 +42,9 @@ class PhpEngine implements EngineInterface, \ArrayAccess
protected $globals;
protected $parser;
private $evalTemplate;
private $evalParameters;
/**
* Constructor.
*
@ -156,24 +159,36 @@ class PhpEngine implements EngineInterface, \ArrayAccess
*/
protected function evaluate(Storage $template, array $parameters = array())
{
$__template__ = $template;
$this->evalTemplate = $template;
$this->evalParameters = $parameters;
unset($template, $parameters);
if (isset($parameters['__template__'])) {
throw new \InvalidArgumentException('Invalid parameter (__template__)');
if (isset($this->evalParameters['this'])) {
throw new \InvalidArgumentException('Invalid parameter (this)');
}
if (isset($this->evalParameters['view'])) {
throw new \InvalidArgumentException('Invalid parameter (view)');
}
if ($__template__ instanceof FileStorage) {
extract($parameters, EXTR_SKIP);
$view = $this;
$view = $this;
if ($this->evalTemplate instanceof FileStorage) {
extract($this->evalParameters, EXTR_SKIP);
$this->evalParameters = null;
ob_start();
require $__template__;
require $this->evalTemplate;
$this->evalTemplate = null;
return ob_get_clean();
} elseif ($__template__ instanceof StringStorage) {
extract($parameters, EXTR_SKIP);
$view = $this;
} elseif ($this->evalTemplate instanceof StringStorage) {
extract($this->evalParameters, EXTR_SKIP);
$this->evalParameters = null;
ob_start();
eval('; ?>'.$__template__.'<?php ;');
eval('; ?>'.$this->evalTemplate.'<?php ;');
$this->evalTemplate = null;
return ob_get_clean();
}

View File

@ -116,6 +116,32 @@ class PhpEngineTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('bar-foo-', $engine->render('foo.php', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
}
public function testRenderParameter()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$this->loader->setTemplate('foo.php', '<?php echo $template . $parameters ?>');
$this->assertEquals('foobar', $engine->render('foo.php', array('template' => 'foo', 'parameters' => 'bar')), '->render() extract variables');
}
/**
* @expectedException \InvalidArgumentException
* @dataProvider forbiddenParameterNames
*/
public function testRenderForbiddenParameter($name)
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
$this->loader->setTemplate('foo.php', 'bar');
$engine->render('foo.php', array($name => 'foo'));
}
public function forbiddenParameterNames()
{
return array(
array('this'),
array('view'),
);
}
public function testEscape()
{
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);