From 8129d0a0172f3ac7a84a27bea308d236a648b881 Mon Sep 17 00:00:00 2001 From: Lo-X Date: Sun, 26 Jul 2015 15:15:34 +0200 Subject: [PATCH] ViewBlock able to prepend with capturing mode --- src/View/View.php | 7 +------ src/View/ViewBlock.php | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/View/View.php b/src/View/View.php index c4c5b7e5e1b..f9310ac719a 100644 --- a/src/View/View.php +++ b/src/View/View.php @@ -590,12 +590,7 @@ public function start($name) */ public function append($name, $value = null) { - if ($value !== null) { - $this->Blocks->concat($name, $value); - return; - } - $this->Blocks->start($name); - echo $this->Blocks->get($name); + $this->Blocks->concat($name, $value); } /** diff --git a/src/View/ViewBlock.php b/src/View/ViewBlock.php index ebf05d53e91..ccbdc27c3c9 100644 --- a/src/View/ViewBlock.php +++ b/src/View/ViewBlock.php @@ -26,6 +26,13 @@ class ViewBlock { + /** + * Override content + * + * @var string + */ + const OVERRIDE = 'override'; + /** * Append content * @@ -72,15 +79,18 @@ class ViewBlock * using View::get(); * * @param string $name The name of the block to capture for. + * @param string $mode If ViewBlock::OVERRIDE existing content will be overridden by new content. + * If ViewBlock::APPEND content will be appended to existing content. + * If ViewBlock::PREPEND it will be prepended. * @throws \Cake\Core\Exception\Exception When starting a block twice * @return void */ - public function start($name) + public function start($name, $mode = ViewBlock::OVERRIDE) { - if (in_array($name, $this->_active)) { + if (in_array($name, array_keys($this->_active))) { throw new Exception(sprintf("A view block with the name '%s' is already/still open.", $name)); } - $this->_active[] = $name; + $this->_active[$name] = $mode; ob_start(); } @@ -98,9 +108,14 @@ public function end() return; } if (!empty($this->_active)) { - $active = end($this->_active); + $mode = end($this->_active); + $active = key($this->_active); $content = ob_get_clean(); - $this->_blocks[$active] = $content; + if($mode === ViewBlock::OVERRIDE) { + $this->_blocks[$active] = $content; + } else { + $this->concat($active, $content, $mode); + } array_pop($this->_active); } } @@ -119,8 +134,13 @@ public function end() * If ViewBlock::PREPEND it will be prepended. * @return void */ - public function concat($name, $value, $mode = ViewBlock::APPEND) + public function concat($name, $value = null, $mode = ViewBlock::APPEND) { + if($value === null) { + $this->start($name, $mode); + return; + } + if (!isset($this->_blocks[$name])) { $this->_blocks[$name] = ''; }