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

Small improvements to JViewHtml #1186

Merged
merged 1 commit into from
May 9, 2012
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
10 changes: 10 additions & 0 deletions docs/manual/en-US/chapters/classes/jviewhtml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ try
$view = new MyView(new MyDatabaseModel, new MyController, $paths);
$view->setLayout('count');
echo $view->render();

// Alternative approach.
$view = new MyView(new MyDatabaseModel);

// Use some chaining.
$view->setPaths($paths)
->setLayout('count');

// Take advantage of the magic __toString method.
echo $view;
}
catch (RuntimeException $e)
{
Expand Down
20 changes: 18 additions & 2 deletions libraries/joomla/view/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public function __construct(JModel $model, SplPriorityQueue $paths = null)
$this->paths = isset($paths) ? $paths : $this->loadPaths();
}

/**
* Magic toString method that is a proxy for the render method.
*
* @return string
*
* @since 12.1
*/
public function __toString()
{
return $this->render();
}

/**
* Method to escape output.
*
Expand Down Expand Up @@ -148,27 +160,31 @@ public function render()
*
* @param string $layout The layout name.
*
* @return void
* @return JViewHtml Method supports chaining.
*
* @since 12.1
*/
public function setLayout($layout)
{
$this->layout = $layout;

return $this;
}

/**
* Method to set the view paths.
*
* @param SplPriorityQueue $paths The paths queue.
*
* @return void
* @return JViewHtml Method supports chaining.
*
* @since 12.1
*/
public function setPaths(SplPriorityQueue $paths)
{
$this->paths = $paths;

return $this;
}

/**
Expand Down
24 changes: 22 additions & 2 deletions tests/suites/unit/joomla/view/JViewHtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public function test__construct()
$this->assertAttributeSame($paths, 'paths', $this->_instance, 'Check default paths.');
}

/**
* Tests the __toString method.
*
* @return void
*
* @covers JViewHtml::__toString
* @since 12.1
*/
public function test__toString()
{
// Set up a priority queue.
$paths = $this->_instance->getPaths();
$paths->insert(__DIR__ . '/layouts1', 1);

$this->_instance->setLayout('olivia');
$this->assertEquals($this->_instance->setLayout('olivia'), (string) $this->_instance);
}

/**
* Tests the escape method.
*
Expand Down Expand Up @@ -159,8 +177,9 @@ public function testRender_exception()
*/
public function testSetLayout()
{
$this->_instance->setLayout('fringe/division');
$result = $this->_instance->setLayout('fringe/division');
$this->assertAttributeSame('fringe/division', 'layout', $this->_instance);
$this->assertSame($this->_instance, $result);
}

/**
Expand All @@ -176,8 +195,9 @@ public function testSetPaths()
$paths = new SplPriorityQueue;
$paths->insert('bar', 99);

$this->_instance->setPaths($paths);
$result = $this->_instance->setPaths($paths);
$this->assertAttributeSame($paths, 'paths', $this->_instance);
$this->assertSame($this->_instance, $result);
}

/**
Expand Down