1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?php
class CompositeView extends AbstractView
{
protected $_views = array();
// factory method (chainable)
public static function factory($view, array $data = array())
{
return new self($viewfile, $data);
}
// add a new view object
public function addView(AbstractView $view)
{
if (!in_array($view, $this->_views, TRUE)) {
$this->_views[] = $view;
}
return $this;
}
// remove an existing view object
public function removeView(AbstractView $view)
{
if (in_array($view, $this->_views, TRUE)) {
$views = array();
foreach ($this->_views as $_view) {
if ($_view !== $view) {
$views[] = $_view;
}
}
$this->_views = $views;
}
return $this;
}
// render each partial view (leaf) and optionally the composite view
public function render()
{
$innerView = '';
if (!empty($this->_views)) {
foreach ($this->_views as $view) {
$innerView .= $view->render();
}
$this->content = $innerView;
}
$compositeView = parent::render();
return !empty($compositeView) ? $compositeView : $innerView;
}
}
?> |
Partager