Think/Template.php
private function replaceBlock($content)
    {
        static $parse = 0;
        $begin = $this->config['taglib_begin'];
        $end = $this->config['taglib_end'];
        $reg = '/('.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.')(.*?)'.$begin.'\/block'.$end.'/is';
        if (is_string($content)) {
            do {
                $content = preg_replace_callback($reg, [$this, 'replaceBlock'], $content);
            } while ($parse && $parse--);

            return $content;
        } elseif (is_array($content)) {
            //存在嵌套，进一步解析
            if (preg_match('/'.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.'/is', $content[3])) {
                $parse = 1;
                $content[3] = preg_replace_callback(
                    $reg,
                    [$this, 'replaceBlock'],
                    "{$content[3]}{$begin}/block{$end}"
                );

                return $content[1].$content[3];
            } else {
                $name = $content[2];
                $content = $content[3];

                return isset($this->block[$name]) ? $this->block[$name] : $content;
            }
        }
    }
private function replaceBlock($content)
    {
        static $parse = 0;
        $begin = $this->config['taglib_begin'];
        $end = $this->config['taglib_end'];
        $reg = '/('.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.')(.*?)'.$begin.'\/block'.$end.'/is';
        if (is_string($content)) {
            do {
                $content = preg_replace_callback($reg, [$this, 'replaceBlock'], $content);
            } while ($parse && $parse--);

            return $content;
        } elseif (is_array($content)) {
            //存在嵌套，进一步解析
            if (preg_match('/'.$begin.'block\sname=[\'"](.+?)[\'"]\s*?'.$end.'/is', $content[3])) {
                $parse = 1;
                $content[3] = preg_replace_callback(
                    $reg,
                    [$this, 'replaceBlock'],
                    "{$content[3]}{$begin}/block{$end}"
                );

                return $content[1].$content[3];
            } else {
                $name = $content[2];
                $content = $content[3];

                return isset($this->block[$name]) ? $this->block[$name] : $content;
            }
        }
    }
替换继承模板中的block标签


@param string $content 模板内容

@return string|bool