Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Small improvements to JViewHtml #1186

Merged
merged 1 commit into from

4 participants

@eddieajau

Added __toString to JViewHtml (proxies for the render method).
Added chaining support to setLayout and setPaths in JViewHtml.

@eddieajau eddieajau Added __toString to JViewHtml.
Added chaining support to setLayout and setPaths in JViewHtml.
4405536
@realityking
Collaborator
@elinw

That's so so so helpful.

@eddieajau

Odd, I wonder what's causing that error.

@chdemko chdemko merged commit a21b380 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 4, 2012
  1. @eddieajau

    Added __toString to JViewHtml.

    eddieajau authored
    Added chaining support to setLayout and setPaths in JViewHtml.
This page is out of date. Refresh to see the latest.
View
10 docs/manual/en-US/chapters/classes/jviewhtml.xml
@@ -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)
{
View
20 libraries/joomla/view/html.php
@@ -53,6 +53,18 @@ public function __construct(JModel $model, SplPriorityQueue $paths = null)
}
/**
+ * 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.
*
* @param string $output The output to escape.
@@ -148,13 +160,15 @@ 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;
}
/**
@@ -162,13 +176,15 @@ public function setLayout($layout)
*
* @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;
}
/**
View
24 tests/suites/unit/joomla/view/JViewHtmlTest.php
@@ -46,6 +46,24 @@ public function test__construct()
}
/**
+ * 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.
*
* @return void
@@ -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);
}
/**
@@ -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);
}
/**
Something went wrong with that request. Please try again.