[Templating] Allows "template" and "parameters" as parameter name (replaces #7908)
This commit is contained in:
parent
cce3a6bfca
commit
6e8b9181ba
@ -42,6 +42,9 @@ class PhpEngine implements EngineInterface, \ArrayAccess
|
|||||||
protected $globals;
|
protected $globals;
|
||||||
protected $parser;
|
protected $parser;
|
||||||
|
|
||||||
|
private $evalTemplate;
|
||||||
|
private $evalParameters;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
@ -156,24 +159,36 @@ class PhpEngine implements EngineInterface, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
protected function evaluate(Storage $template, array $parameters = array())
|
protected function evaluate(Storage $template, array $parameters = array())
|
||||||
{
|
{
|
||||||
$__template__ = $template;
|
$this->evalTemplate = $template;
|
||||||
|
$this->evalParameters = $parameters;
|
||||||
|
unset($template, $parameters);
|
||||||
|
|
||||||
if (isset($parameters['__template__'])) {
|
if (isset($this->evalParameters['this'])) {
|
||||||
throw new \InvalidArgumentException('Invalid parameter (__template__)');
|
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();
|
ob_start();
|
||||||
require $__template__;
|
require $this->evalTemplate;
|
||||||
|
|
||||||
|
$this->evalTemplate = null;
|
||||||
|
|
||||||
return ob_get_clean();
|
return ob_get_clean();
|
||||||
} elseif ($__template__ instanceof StringStorage) {
|
} elseif ($this->evalTemplate instanceof StringStorage) {
|
||||||
extract($parameters, EXTR_SKIP);
|
extract($this->evalParameters, EXTR_SKIP);
|
||||||
$view = $this;
|
$this->evalParameters = null;
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
eval('; ?>'.$__template__.'<?php ;');
|
eval('; ?>'.$this->evalTemplate.'<?php ;');
|
||||||
|
|
||||||
|
$this->evalTemplate = null;
|
||||||
|
|
||||||
return ob_get_clean();
|
return ob_get_clean();
|
||||||
}
|
}
|
||||||
|
@ -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');
|
$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()
|
public function testEscape()
|
||||||
{
|
{
|
||||||
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
|
$engine = new ProjectTemplateEngine(new TemplateNameParser(), $this->loader);
|
||||||
|
Reference in New Issue
Block a user