Skip to content

Commit

Permalink
Merge pull request #817 from jails/feature/renderer-options
Browse files Browse the repository at this point in the history
Keep the same `_options` context on each cascading level using `Renderer::_render`.
  • Loading branch information
nateabele committed Feb 12, 2013
2 parents 3d75aaf + 9b49eb9 commit ba19920
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion template/view/Renderer.php
Expand Up @@ -506,8 +506,11 @@ public function set(array $data = array()) {
* @return string Returns a the rendered template content as a string.
*/
protected function _render($type, $template, array $data = array(), array $options = array()) {
$context = $this->_options;
$options += $this->_options;
return $this->_view->render($type, $data + $this->_data, compact('template') + $options);
$result = $this->_view->render($type, $data + $this->_data, compact('template') + $options);
$this->_options = $context;
return $result;
}
}

Expand Down
58 changes: 58 additions & 0 deletions tests/cases/template/ViewTest.php
Expand Up @@ -219,6 +219,64 @@ public function testElementRenderingOptions() {
Libraries::remove('test_app');
$this->_cleanUp();
}

public function testContextWithElementRenderingOptions() {
$tmpDir = realpath(Libraries::get(true, 'resources') . '/tmp');
$this->skipIf(!is_writable($tmpDir), "Can't write to resources directory.");

$testApp = $tmpDir . '/tests/test_app';
$viewDir = $testApp . '/views';
mkdir($viewDir . '/elements', 0777, true);
Libraries::add('test_app', array('path' => $testApp));

$testApp2 = $tmpDir . '/tests/test_app2';
$viewDir2 = $testApp2 . '/views';
mkdir($viewDir2 . '/elements', 0777, true);
Libraries::add('test_app2', array('path' => $testApp2));

$body = "<?php ";
$body .= "echo \$this->_render('element', 'element2', array(), ";
$body .= "array('library' => 'test_app2'));";
$body .= "echo \$this->_render('element', 'element1');";
$body .= "?>";

file_put_contents($viewDir . '/template.html.php', $body);
file_put_contents($viewDir . '/elements/element1.html.php', 'element1');
file_put_contents($viewDir2 . '/elements/element2.html.php', 'element2');


$view = new View(array(
'compile' => false,
'paths' => array(
'template' => '{:library}/views/{:template}.html.php',
'element' => '{:library}/views/elements/{:template}.html.php',
'layout' => false
)
));

$options = array(
'template' => 'template',
'library' => 'test_app'
);

$result = $view->render('all', array(), $options);
$this->assertIdentical('element2element1', $result);

$body = "<?php ";
$body .= "echo \$this->_render('element', 'element1');";
$body .= "echo \$this->_render('element', 'element2', array(), ";
$body .= "array('library' => 'test_app2'));";
$body .= "?>";

file_put_contents($viewDir . '/template.html.php', $body);

$result = $view->render('all', array(), $options);
$this->assertIdentical('element1element2', $result);

Libraries::remove('test_app');
Libraries::remove('test_app2');
$this->_cleanUp();
}
}

?>

0 comments on commit ba19920

Please sign in to comment.