Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Fixes bug with templates context variables
Browse files Browse the repository at this point in the history
Template contextual variables should be extracted within a render method instead setting up in query_vars, because they leaked bettwen multiple rendering the same template.
  • Loading branch information
jedrzejchalubek committed Oct 19, 2017
1 parent e974938 commit 89415b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
25 changes: 6 additions & 19 deletions src/Gin/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,17 @@ public function __construct(ConfigInterface $config)
*/
public function render(array $context = [])
{
if (locate_template($path = $this->getRelativePath(), false, false)) {
$this->setContext($context);
if ($template = locate_template($path = $this->getRelativePath(), false, false)) {
$this->doActions();

return locate_template($path, true, false);
}
extract(apply_filters("tonik/gin/template/context/{$this->getFilename()}", $context));

throw new FileNotFoundException("Template file [{$this->getRelativePath()}] cannot be located.");
}
require $template;

/**
* Sets context dataset on query.
*
* @param array $context
*
* @return void
*/
public function setContext(array $context)
{
$context = apply_filters("tonik/gin/template/context/{$this->getFilename()}", $context);

foreach ($context as $key => $value) {
set_query_var($key, $value);
return;
}

throw new FileNotFoundException("Template file [{$this->getRelativePath()}] cannot be located.");
}

/**
Expand Down
59 changes: 30 additions & 29 deletions tests/Gin/Template/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ public function test_centext_filter_on_template_rendering()
{
$config = $this->getConfig();

Functions::expect('locate_template')->atLeast()->once()->andReturn(true);
Functions::expect('set_query_var')->atLeast()->once()->with('key', 'changed')->andReturn(null);
Functions::expect('locate_template')->andReturn($this->getFixtureTemplatePath());

$template = $this->getTemplate($config, 'sample_template');
Actions::expectFired('get_template_part_sample_template')->once()->with('sample_template', null);
Filters::expectApplied('tonik/gin/template/context/sample_template.php')->once()->with(['key' => 'value'])->andReturn(['key' => 'changed']);
ob_start();
$template->render(['key' => 'value']);
$this->assertEquals('<div>changed</div>', $this->removeNewLineAtEOF(ob_get_clean()));

$template = $this->getTemplate($config, ['sample_template', 'named']);
Actions::expectFired('get_template_part_sample_template')->once()->with('sample_template', 'named');
Filters::expectApplied('tonik/gin/template/context/sample_template-named.php')->once()->with(['key' => 'value'])->andReturn(['key' => 'changed']);
ob_start();
$template->render(['key' => 'value']);
$this->assertEquals('<div>changed</div>', $this->removeNewLineAtEOF(ob_get_clean()));
}

/**
Expand All @@ -88,7 +91,7 @@ public function it_should_throw_exception_on_render_if_file_is_no_located()

$this->expectException(FileNotFoundException::class);

$template->render();
$template->render(['key' => 'value']);
}

/**
Expand Down Expand Up @@ -135,10 +138,12 @@ public function it_should_do_get_template_part_action_on_render_with_no_named_te
$config = $this->getConfig();
$template = $this->getTemplate($config, 'sample_template');

Functions::expect('locate_template')->twice()->andReturn(true);
Functions::expect('locate_template')->once()->andReturn($this->getFixtureTemplatePath());
Actions::expectFired('get_template_part_sample_template')->once()->with('sample_template', null);

$template->render();
ob_start();
$template->render(['key' => 'value']);
ob_get_clean();
}

/**
Expand All @@ -148,38 +153,24 @@ public function it_should_do_get_template_part_action_on_render_with_named_templ
{
$config = $this->getConfig();

ob_start();

$template = $this->getTemplate($config, ['sample_template', 'named']);
Functions::expect('locate_template')->twice()->andReturn(true);
Functions::expect('locate_template')->once()->andReturn($this->getFixtureTemplatePath());
Actions::expectFired('get_template_part_sample_template')->once()->with('sample_template', 'named');
$template->render();
$template->render(['key' => 'value']);

$template = $this->getTemplate($config, ['sample_template', false]);
Functions::expect('locate_template')->twice()->andReturn(true);
Functions::expect('locate_template')->once()->andReturn($this->getFixtureTemplatePath());
Actions::expectFired('get_template_part_sample_template')->once()->with('sample_template', false);
$template->render();
$template->render(['key' => 'value']);

$template = $this->getTemplate($config, ['sample_template', null]);
Functions::expect('locate_template')->twice()->andReturn(true);
Functions::expect('locate_template')->once()->andReturn($this->getFixtureTemplatePath());
Actions::expectFired('get_template_part_sample_template')->once()->with('sample_template', null);
$template->render();
}

/**
* @test
*/
public function it_should_set_up_context_to_the_query_var()
{
$config = $this->getConfig();
$template = $this->getTemplate($config, ['sample_template', 'named']);

Functions::expect('locate_template')->twice()->andReturn(true);
Functions::expect('set_query_var')->once()->with('key1', 'value1')->andReturn(null);
Functions::expect('set_query_var')->once()->with('key2', 'value2')->andReturn(null);
$template->render(['key' => 'value']);

$template->render([
'key1' => 'value1',
'key2' => 'value2'
]);
ob_get_clean();
}

public function getConfig()
Expand All @@ -201,4 +192,14 @@ public function getTemplate($config, $name)
{
return (new Template($config))->setFile($name);
}
}

public function getFixtureTemplatePath()
{
return dirname(__DIR__) . '/../fixtures/template.tpl.php';
}

public function removeNewLineAtEOF($string)
{
return trim(preg_replace('/\s\s+/', ' ', $string));
}
}
1 change: 1 addition & 0 deletions tests/fixtures/template.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><?= $key ?></div>

0 comments on commit 89415b5

Please sign in to comment.