Contao\TemplateInheritance::block PHP Method

block() public method

Start a new block
public block ( string $name )
$name string The block name
    public function block($name)
    {
        $this->arrBlockNames[] = $name;
        // Root template
        if ($this->strParent === null) {
            // Register the block name
            if (!isset($this->arrBlocks[$name])) {
                $this->arrBlocks[$name] = '[[TL_PARENT]]';
            } elseif (is_array($this->arrBlocks[$name])) {
                $callback = function ($current, $parent) {
                    return str_replace('[[TL_PARENT]]', $parent, $current);
                };
                $this->arrBlocks[$name] = array_reduce($this->arrBlocks[$name], $callback, '[[TL_PARENT]]');
            }
            // Handle nested blocks
            if ($this->arrBlocks[$name] != '[[TL_PARENT]]') {
                // Output everything before the first TL_PARENT tag
                if (strpos($this->arrBlocks[$name], '[[TL_PARENT]]') !== false) {
                    list($content) = explode('[[TL_PARENT]]', $this->arrBlocks[$name], 2);
                    echo $content;
                } else {
                    echo $this->arrBlocks[$name];
                    ob_start();
                }
            }
        } else {
            // Clean the output buffer
            ob_end_clean();
            // Check for nested blocks
            if (count($this->arrBlockNames) > 1) {
                throw new \Exception('Nested blocks are not allowed in child templates');
            }
            // Start a new output buffer
            ob_start();
        }
    }