think\Template::parse PHP Method

parse() public method

模板解析入口 支持普通标签和TagLib解析 支持自定义标签库
public parse ( string &$content ) : void
$content string 要解析的模板内容
return void
    public function parse(&$content)
    {
        // 内容为空不解析
        if (empty($content)) {
            return;
        }
        // 替换literal标签内容
        $this->parseLiteral($content);
        // 解析继承
        $this->parseExtend($content);
        // 解析布局
        $this->parseLayout($content);
        // 检查include语法
        $this->parseInclude($content);
        // 替换包含文件中literal标签内容
        $this->parseLiteral($content);
        // 检查PHP语法
        $this->parsePhp($content);
        // 获取需要引入的标签库列表
        // 标签库只需要定义一次,允许引入多个一次
        // 一般放在文件的最前面
        // 格式:<taglib name="html,mytag..." />
        // 当TAGLIB_LOAD配置为true时才会进行检测
        if ($this->config['taglib_load']) {
            $tagLibs = $this->getIncludeTagLib($content);
            if (!empty($tagLibs)) {
                // 对导入的TagLib进行解析
                foreach ($tagLibs as $tagLibName) {
                    $this->parseTagLib($tagLibName, $content);
                }
            }
        }
        // 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀
        if ($this->config['taglib_pre_load']) {
            $tagLibs = explode(',', $this->config['taglib_pre_load']);
            foreach ($tagLibs as $tag) {
                $this->parseTagLib($tag, $content);
            }
        }
        // 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀
        $tagLibs = explode(',', $this->config['taglib_build_in']);
        foreach ($tagLibs as $tag) {
            $this->parseTagLib($tag, $content, true);
        }
        // 解析普通模板标签 {$tagName}
        $this->parseTag($content);
        // 还原被替换的Literal标签
        $this->parseLiteral($content, true);
        return;
    }

Usage Example

Beispiel #1
0
    public function testThinkVar()
    {
        $config['tpl_begin'] = '{';
        $config['tpl_end'] = '}';
        $template = new Template($config);
        $_SERVER['SERVER_NAME'] = 'server_name';
        $_GET['action'] = 'action';
        $_POST['action'] = 'action';
        \think\Cookie::set('action', ['name' => 'name']);
        \think\Session::set('action', ['name' => 'name']);
        define('SITE_NAME', 'site_name');
        $content = <<<EOF
{\$Think.SERVER.SERVER_NAME}<br/>
{\$Think.GET.action}<br/>
{\$Think.POST.action}<br/>
{\$Think.COOKIE.action}<br/>
{\$Think.COOKIE.action.name}<br/>
{\$Think.SESSION.action}<br/>
{\$Think.SESSION.action.name}<br/>
{\$Think.ENV.OS}<br/>
{\$Think.REQUEST.action}<br/>
{\$Think.CONST.SITE_NAME}<br/>
{\$Think.LANG.action}<br/>
{\$Think.CONFIG.action.name}<br/>
{\$Think.NOW}<br/>
{\$Think.VERSION}<br/>
{\$Think.LDELIM}<br/>
{\$Think.RDELIM}<br/>
{\$Think.SITE_NAME}
EOF;
        $data = <<<EOF
<?php echo \$_SERVER['SERVER_NAME']; ?><br/>
<?php echo \$_GET['action']; ?><br/>
<?php echo \$_POST['action']; ?><br/>
<?php echo \\think\\Cookie::get('action'); ?><br/>
<?php echo \$_COOKIE['action']['name']; ?><br/>
<?php echo \\think\\Session::get('action'); ?><br/>
<?php echo \$_SESSION['action']['name']; ?><br/>
<?php echo \$_ENV['OS']; ?><br/>
<?php echo \$_REQUEST['action']; ?><br/>
<?php echo SITE_NAME; ?><br/>
<?php echo \\think\\Lang::get('action'); ?><br/>
<?php echo \\think\\Config::get('action.name'); ?><br/>
<?php echo date('Y-m-d g:i a',time()); ?><br/>
<?php echo THINK_VERSION; ?><br/>
<?php echo '{'; ?><br/>
<?php echo '}'; ?><br/>
<?php echo SITE_NAME; ?>
EOF;
        $template->parse($content);
        $this->assertEquals($data, $content);
    }
All Usage Examples Of think\Template::parse