Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Adding parameters to templates before render-time #143

Merged
merged 15 commits into from
Oct 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Template/AddParametersTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Template;

trait AddParametersTrait
{
/**
* @var array
*/
protected $templateParams = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these members (this and the mergeParameters() method) are in the trait, they can be private visibility.


/**
* Add parameters to template
*
* If no template name is given, the parameters will be added to all templates rendered
*
* @param array|object $params
* @param string|null $name
*/
public function addParameters($params, $name = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method also needs to be added to the interface; otherwise, an implementation could omit it, leaving developers depending on concretions instead of abstractions.

{
if (method_exists($this, 'normalizeParams')) {
$params = $this->normalizeParams($params);
}
$name = (string) $name;
$existing = isset($this->templateParams[$name]) ? $this->templateParams[$name] : null;
$this->templateParams[$name] = array_merge($existing, $params, $name);
}

/**
* Returns merged global, template-specific and given params
*
* @param array $params
* @param string $name
* @return array
*/
protected function mergeParams($params, $name)
{
return array_merge(
isset($this->templateParams['']) ? $this->templateParams[''] : [],
isset($this->templateParams[$name]) ? $this->templateParams[$name] : [],
$params
);
}
}
15 changes: 15 additions & 0 deletions src/Template/PlatesRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ public function getPaths()
return $paths;
}

/**
* Add parameters to template
*
* If no template name is given, the parameters will be added to all templates rendered
*
* @param array|object $params
* @param string $name
*/
public function addParameters($params, $name = null)
{
$params = $this->normalizeParams($params);
$this->template->addData($params, $name);
}


/**
* Create a default Plates engine
*
Expand Down
10 changes: 10 additions & 0 deletions src/Template/TemplateRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ public function addPath($path, $namespace = null);
* @return TemplatePath[]
*/
public function getPaths();

/**
* Add parameters to template
*
* If no template name is given, the parameters will be added to all templates rendered
*
* @param array|object $params
* @param string|null $name
*/
public function addParameters($params, $name = null);
}
3 changes: 2 additions & 1 deletion src/Template/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
class TwigRenderer implements TemplateRendererInterface
{
use AddParametersTrait;
use ArrayParametersTrait;

/**
Expand Down Expand Up @@ -90,7 +91,7 @@ private function getDefaultLoader()
public function render($name, $params = [])
{
$name = $this->normalizeTemplate($name);
$params = $this->normalizeParams($params);
$params = $this->mergeParams($this->normalizeParams($params), $name);
return $this->template->render($name, $params);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Template/ZendViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
class ZendViewRenderer implements TemplateRendererInterface
{
use AddParametersTrait;
use ArrayParametersTrait;

/**
Expand Down Expand Up @@ -118,8 +119,9 @@ public function __construct(RendererInterface $renderer = null, $layout = null)
*/
public function render($name, $params = [])
{
$params = $this->mergeParams($this->normalizeParams($params), $name);
return $this->renderModel(
$this->createModel($name, $this->normalizeParams($params)),
$this->createModel($name, $params),
$this->renderer
);
}
Expand Down
70 changes: 70 additions & 0 deletions test/Template/PlatesRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,74 @@ public function testProperlyResolvesNamespacedTemplate()

$this->assertSame($expected, $test);
}

public function testAddParameterToOneTemplate()
{
$renderer = new PlatesRenderer();
$renderer->addPath(__DIR__ . '/TestAsset');
$name = 'Plates';
$renderer->addParameters(['name' => $name], 'plates');
$result = $renderer->render('plates');
$content = file_get_contents(__DIR__ . '/TestAsset/plates.php');
$content= str_replace('<?=$this->e($name)?>', $name, $content);
$this->assertEquals($content, $result);

// @fixme hack to work around https://github.com/thephpleague/plates/issues/60, remove if ever merged
set_error_handler(function ($error, $message) {
$this->assertContains('Undefined variable: name', $message);
return true;
}, E_NOTICE);
$renderer->render('plates-2');
restore_error_handler();

$content= str_replace('<?=$this->e($name)?>', '', $content);
$this->assertEquals($content, $result);
}

public function testAddSharedParameters()
{
$renderer = new PlatesRenderer();
$renderer->addPath(__DIR__ . '/TestAsset');
$name = 'Plates';
$renderer->addParameters(['name' => $name]);
$result = $renderer->render('plates');
$content = file_get_contents(__DIR__ . '/TestAsset/plates.php');
$content= str_replace('<?=$this->e($name)?>', $name, $content);
$this->assertEquals($content, $result);
$result = $renderer->render('plates-2');
$content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php');
$content= str_replace('<?=$this->e($name)?>', $name, $content);
$this->assertEquals($content, $result);
}

public function testOverrideSharedParametersPerTemplate()
{
$renderer = new PlatesRenderer();
$renderer->addPath(__DIR__ . '/TestAsset');
$name = 'Plates';
$name2 = 'Saucers';
$renderer->addParameters(['name' => $name]);
$renderer->addParameters(['name' => $name2], 'plates-2');
$result = $renderer->render('plates');
$content = file_get_contents(__DIR__ . '/TestAsset/plates.php');
$content= str_replace('<?=$this->e($name)?>', $name, $content);
$this->assertEquals($content, $result);
$result = $renderer->render('plates-2');
$content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php');
$content= str_replace('<?=$this->e($name)?>', $name2, $content);
$this->assertEquals($content, $result);
}

public function testOverrideSharedParametersAtRender()
{
$renderer = new PlatesRenderer();
$renderer->addPath(__DIR__ . '/TestAsset');
$name = 'Plates';
$name2 = 'Saucers';
$renderer->addParameters(['name' => $name]);
$result = $renderer->render('plates', ['name' => $name2]);
$content = file_get_contents(__DIR__ . '/TestAsset/plates.php');
$content= str_replace('<?=$this->e($name)?>', $name2, $content);
$this->assertEquals($content, $result);
}
}
3 changes: 3 additions & 0 deletions test/Template/TestAsset/plates-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>This is a second template file for Plates</h1>

<p>You are using <strong><?=$this->e($name)?></strong>!</p>