修正block标签的bug

This commit is contained in:
oldrind
2016-03-17 09:50:12 +08:00
parent ee8e6a26c0
commit 14ee945b0c
2 changed files with 23 additions and 21 deletions

View File

@@ -217,21 +217,21 @@ class Template
* 设置布局 * 设置布局
* @access public * @access public
* @param mixed $name 布局模板名称 false 则关闭布局 * @param mixed $name 布局模板名称 false 则关闭布局
* @return void * @return object
*/ */
public function layout($name) public function layout($name)
{ {
if (false === $name) { if (false === $name) {
// 关闭布局 // 关闭布局
$this->config['layout_on'] = false; $this->config['layout_on'] = false;
return $this; } else {
}
// 开启布局 // 开启布局
$this->config['layout_on'] = true; $this->config['layout_on'] = true;
// 名称必须为字符串 // 名称必须为字符串
if (is_string($name)) { if (is_string($name)) {
$this->config['layout_name'] = $name; $this->config['layout_name'] = $name;
} }
}
return $this; return $this;
} }
@@ -524,8 +524,9 @@ class Template
$replace = str_replace($baseBlocks[$key]['begin'] . $baseBlocks[$key]['content'] . $baseBlocks[$key]['end'], $blocks[$key]['content'], $replace); $replace = str_replace($baseBlocks[$key]['begin'] . $baseBlocks[$key]['content'] . $baseBlocks[$key]['end'], $blocks[$key]['content'], $replace);
} }
} }
if (isset($blocks[$name])) {
// 带有{__block__}表示与所继承模板的相应标签合并,而不是覆盖 // 带有{__block__}表示与所继承模板的相应标签合并,而不是覆盖
$replace = !isset($blocks[$name]) ? $replace : str_replace(['{__BLOCK__}', '{__block__}'], $replace, $blocks[$name]['content']); $replace = str_replace(['{__BLOCK__}', '{__block__}'], $replace, $blocks[$name]['content']);
if (!empty($val['parent'])) { if (!empty($val['parent'])) {
// 如果不是最顶层的block标签 // 如果不是最顶层的block标签
$parent = $val['parent']; $parent = $val['parent'];
@@ -534,12 +535,13 @@ class Template
} }
$blocks[$name]['content'] = $replace; $blocks[$name]['content'] = $replace;
$children[$parent][] = $name; $children[$parent][] = $name;
} else { continue;
}
}
// 替换模板中的block标签 // 替换模板中的block标签
$extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend); $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend);
} }
} }
}
$content = $extend; $content = $extend;
unset($blocks, $baseBlocks); unset($blocks, $baseBlocks);
} }

View File

@@ -323,7 +323,7 @@ EOF;
'view_path' => dirname(__FILE__) . '/', 'view_path' => dirname(__FILE__) . '/',
]; ];
$data = ['name' => 'value']; $data = ['name' => 'value'];
$template->display('display', $data, $config); $template->layout('layout')->display('display', $data, $config);
$this->expectOutputString('value'); $this->expectOutputString('value');
} }