mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Allow content to be added before existing content in view block.
This commit is contained in:
parent
127b906fdb
commit
47708c52cd
3 changed files with 90 additions and 14 deletions
|
@ -1257,6 +1257,19 @@ class ViewTest extends CakeTestCase {
|
|||
$this->assertEquals('Block content', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test prepending to a block with append.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBlockPrepend() {
|
||||
$this->View->assign('test', 'Block');
|
||||
$this->View->prepend('test', 'Before ');
|
||||
|
||||
$result = $this->View->fetch('test');
|
||||
$this->assertEquals('Before Block', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* You should be able to append to undefined blocks.
|
||||
*
|
||||
|
@ -1268,6 +1281,17 @@ class ViewTest extends CakeTestCase {
|
|||
$this->assertEquals('Unknown', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* You should be able to prepend to undefined blocks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBlockPrependUndefined() {
|
||||
$this->View->prepend('test', 'Unknown');
|
||||
$result = $this->View->fetch('test');
|
||||
$this->assertEquals('Unknown', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* setting an array should cause an exception.
|
||||
*
|
||||
|
|
|
@ -599,17 +599,31 @@ class View extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Append to an existing or new block. Appending to a new
|
||||
* Append to an existing or new block. Appending to a new
|
||||
* block will create the block.
|
||||
*
|
||||
* @param string $name Name of the block
|
||||
* @param string $value The content for the block.
|
||||
* @return void
|
||||
* @throws CakeException when you use non-string values.
|
||||
* @see ViewBlock::append()
|
||||
* @see ViewBlock::concat()
|
||||
*/
|
||||
public function append($name, $value = null) {
|
||||
return $this->Blocks->append($name, $value);
|
||||
return $this->Blocks->concat($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepend to an existing or new block. Prepending to a new
|
||||
* block will create the block.
|
||||
*
|
||||
* @param string $name Name of the block
|
||||
* @param string $value The content for the block.
|
||||
* @return void
|
||||
* @throws CakeException when you use non-string values.
|
||||
* @see ViewBlock::concat()
|
||||
*/
|
||||
public function prepend($name, $value = null) {
|
||||
return $this->Blocks->concat($name, $value, ViewBlock::PREPEND);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,6 +23,20 @@
|
|||
*/
|
||||
class ViewBlock {
|
||||
|
||||
/**
|
||||
* Append content
|
||||
*
|
||||
* @constant APPEND
|
||||
*/
|
||||
const APPEND = 'append';
|
||||
|
||||
/**
|
||||
* Prepend content
|
||||
*
|
||||
* @constant PREPEND
|
||||
*/
|
||||
const PREPEND = 'prepend';
|
||||
|
||||
/**
|
||||
* Block content. An array of blocks indexed by name.
|
||||
*
|
||||
|
@ -72,6 +86,39 @@ class ViewBlock {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Concat content to an existing or new block.
|
||||
* Concating to a new block will create the block.
|
||||
*
|
||||
* Calling concat() without a value will create a new capturing
|
||||
* block that needs to be finished with View::end(). The content
|
||||
* of the new capturing context will be added to the existing block context.
|
||||
*
|
||||
* @param string $name Name of the block
|
||||
* @param string $value The content for the block
|
||||
* @param string $mode If ViewBlock::APPEND content will be appended to existing content.
|
||||
* If ViewBlock::PREPEND it will be prepended.
|
||||
* @return void
|
||||
* @throws CakeException when you use non-string values.
|
||||
*/
|
||||
public function concat($name, $value = null, $mode = ViewBlock::APPEND) {
|
||||
if (isset($value)) {
|
||||
if (!is_string($value)) {
|
||||
throw new CakeException(__d('cake_dev', '$value must be a string.'));
|
||||
}
|
||||
if (!isset($this->_blocks[$name])) {
|
||||
$this->_blocks[$name] = '';
|
||||
}
|
||||
if ($mode === ViewBlock::PREPEND) {
|
||||
$this->_blocks[$name] = $value . $this->_blocks[$name];
|
||||
} else {
|
||||
$this->_blocks[$name] .= $value;
|
||||
}
|
||||
} else {
|
||||
$this->start($name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to an existing or new block. Appending to a new
|
||||
* block will create the block.
|
||||
|
@ -84,19 +131,10 @@ class ViewBlock {
|
|||
* @param string $value The content for the block.
|
||||
* @return void
|
||||
* @throws CakeException when you use non-string values.
|
||||
* @deprecated As of 2.3 use ViewBlock::concat() instead.
|
||||
*/
|
||||
public function append($name, $value = null) {
|
||||
if (isset($value)) {
|
||||
if (!is_string($value)) {
|
||||
throw new CakeException(__d('cake_dev', '$value must be a string.'));
|
||||
}
|
||||
if (!isset($this->_blocks[$name])) {
|
||||
$this->_blocks[$name] = '';
|
||||
}
|
||||
$this->_blocks[$name] .= $value;
|
||||
} else {
|
||||
$this->start($name);
|
||||
}
|
||||
$this->concat($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue