Class ViewBlock
ViewBlock implements the concept of Blocks or Slots in the View layer. Slots or blocks are combined with extending views and layouts to afford slots of content that are present in a layout or parent view, but are defined by the child view or elements used in the view.
Constants
Property Summary
- 
        $_active protectedarrayThe active blocks being captured. 
- 
        $_blocks protectedarrayBlock content. An array of blocks indexed by name. 
- 
        $_discardActiveBufferOnEnd protectedboolShould the currently captured content be discarded on ViewBlock::end() 
Method Summary
- 
          active() publicGet the name of the currently open block. 
- 
          concat() publicConcat content to an existing or new block. Concating to a new block will create the block. 
- 
          end() publicEnd a capturing block. The compliment to ViewBlock::start() 
- 
          exists() publicCheck if a block exists 
- 
          get() publicGet the content for a block. 
- 
          keys() publicGet the names of all the existing blocks. 
- 
          set() publicSet the content for a block. This will overwrite any existing content. 
- 
          start() publicStart capturing output for a 'block' 
- 
          unclosed() publicGet the names of the unclosed/active blocks. 
Method Detail
active() ¶ public
active(): string|nullGet the name of the currently open block.
Returns
string|nullEither null or the name of the last open block.
concat() ¶ public
concat(string $name, mixed $value = null, string $mode = ViewBlock::APPEND): voidConcat 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.
Parameters
- 
                string$name
- Name of the block 
- 
                mixed$value optional
- The content for the block. Value will be type cast to string. 
- 
                string$mode optional
- If ViewBlock::APPEND content will be appended to existing content. If ViewBlock::PREPEND it will be prepended. 
Returns
voidend() ¶ public
end(): voidEnd a capturing block. The compliment to ViewBlock::start()
Returns
voidSee Also
exists() ¶ public
exists(string $name): boolCheck if a block exists
Parameters
- 
                string$name
- Name of the block 
Returns
boolget() ¶ public
get(string $name, string $default = ''): stringGet the content for a block.
Parameters
- 
                string$name
- Name of the block 
- 
                string$default optional
- Default string 
Returns
stringThe block content or $default if the block does not exist.
keys() ¶ public
keys(): arrayGet the names of all the existing blocks.
Returns
arrayAn array containing the blocks.
set() ¶ public
set(string $name, mixed $value): voidSet the content for a block. This will overwrite any existing content.
Parameters
- 
                string$name
- Name of the block 
- 
                mixed$value
- The content for the block. Value will be type cast to string. 
Returns
voidstart() ¶ public
start(string $name, string $mode = ViewBlock::OVERRIDE): voidStart capturing output for a 'block'
Blocks allow you to create slots or blocks of dynamic content in the layout. view files can implement some or all of a layout's slots.
You can end capturing blocks using View::end(). Blocks can be output using View::get();
Parameters
- 
                string$name
- The name of the block to capture for. 
- 
                string$mode optional
- 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. 
Returns
voidThrows
Cake\Core\Exception\ExceptionWhen starting a block twice
unclosed() ¶ public
unclosed(): arrayGet the names of the unclosed/active blocks.
Returns
arrayAn array of unclosed blocks.
