Smarty模板的继承示例:
{extends 'parent.tpl'} {block name='main'} ... override content in parent ... {/block}
File: libs/sysplugins/smarty_internal_templatecompilerbase.php function compileTemplate(...){ ... while($this->template->source = array_shift($this->sources)){ ... ... } ... }
# File: libs/sysplugins/smarty_interal_compile_extends.php function compileTemplate(...){ array_unshift($compiler->sources, $_template->source); $compiler->inheritance_child = true; }
Smarty_Internal_Compile_Block::compileChildBlock()
负责# File: libs/sysplugins/smarty_interal_compile_block.php if($compiler->inheritance_child){ self::$block_data[$name]['source'] = "{private_child_block name='$name'}"; self::$block_data[$name]['source'] .= "{/private_child_block}"; return; } else { $output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $name); }
{$smarty.block.parent}
会被简单地替换为点位符__SMARTY_BLOCK_PARENT__
。
{$smarty.block.child}
也会触发相应的compileChildBlock()
的操作。
Smarty_Internal_Compile_Block::compileChildBlock()
的任务主要是编译字符串形式的模版源代码,并和父模版中的相应"{block}"进行合并。
子模版和父模版合并时的关系主要是
$smarty.block.parent
: 通过替换占位符__SMARTY_BLOCK_PARENT__
来实现prepend
append