Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renderer options (close #642).

  • Loading branch information...
commit 08a2433163f4147c23c06e2510ea1a1a30585785 1 parent 3b0c1d5
@jails jails authored
View
13 template/view/Renderer.php
@@ -132,6 +132,14 @@
*/
protected $_vars = array();
+ /*
+ * Available options accepted by `template\View::render()`, used when rendering.
+ *
+ * @see lithium\template\View::render()
+ * @var array
+ */
+ protected $_options = array();
+
/**
* Render the template with given data. Abstract; must be added to subclasses.
*
@@ -487,10 +495,7 @@ 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()) {
- if ($this->_request) {
- $library = $this->_request->library;
- $options += compact('library');
- }
+ $options += $this->_options;
return $this->_view->render($type, $data + $this->_data, compact('template') + $options);
}
}
View
1  template/view/adapter/File.php
@@ -102,6 +102,7 @@ public function render($template, $data = array(), array $options = array()) {
$this->_context = $options['context'] + $this->_context;
$this->_data = (array) $data + $this->_vars;
+ $this->_options = $options;
$template__ = $template;
unset($options, $template, $defaults, $data);
View
45 tests/cases/template/ViewTest.php
@@ -9,6 +9,7 @@
namespace lithium\tests\cases\template;
use Closure;
+use lithium\core\Libraries;
use lithium\template\View;
use lithium\action\Response;
use lithium\template\view\adapter\Simple;
@@ -175,6 +176,50 @@ public function testNolayout() {
unset($renderData[0]['data']['h']);
$this->assertEqual($expected, $renderData);
}
+
+ public function testElementRenderingOptions() {
+ $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, 0777, true);
+ Libraries::add('test_app', array('path' => $testApp));
+
+ $body = '<?php echo isset($this->_options[$option]) ? $this->_options[$option] : ""; ?>';
+ $template = $viewDir . '/template.html.php';
+
+ file_put_contents($template, $body);
+
+ $view = new View(array(
+ 'paths' => array(
+ 'template' => '{:library}/views/{:template}.html.php',
+ 'layout' => false
+ )
+ ));
+
+ $options = array(
+ 'template' => 'template',
+ 'library' => 'test_app'
+ );
+ $result = $view->render('all', array('option' => 'custom'), $options);
+ $this->assertIdentical('', $result);
+ $result = $view->render('all', array('option' => 'library'), $options);
+ $this->assertIdentical('test_app', $result);
+
+ $options = array(
+ 'template' => 'template',
+ 'library' => 'test_app',
+ 'custom' => 'custom option'
+ );
+ $result = $view->render('all', array('option' => 'custom'), $options);
+ $this->assertIdentical('custom option', $result);
+ $result = $view->render('all', array('option' => 'library'), $options);
+ $this->assertIdentical('test_app', $result);
+
+ Libraries::remove('test_app');
+ $this->_cleanUp();
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.