将网站的模板化部分组合在一起的最佳方法是哪种方法?我更喜欢第一种解决方案,但我不确定使用多次调用display()是一种很好的做法.我正在寻找易于维护和速度.
<?php
$smarty->display('header.tpl');
$smarty->display('menu.tpl');
$smarty->display('article1.tpl');
$smarty->display('footer.tpl');
?>
Run Code Online (Sandbox Code Playgroud)
或显示单个智能模板,然后在模板中
{include file="header.tpl"}
<body id={$pageid}>
{include file="menu.tpl"}
{include file="header_inner.tpl"}
Content of page
{include file="footer.tpl"}
Run Code Online (Sandbox Code Playgroud)
如果你正在使用Smarty3(你应该)看一下继承和模板继承.它允许您定义模板,就像您构建类一样 - OOP风格.
如果您不能(或不想)与TI合作,我建议采用这种{include}方法.原因:
HTTP 304 Not Modified处理多个display()调用(唯一)有一个优点.您可以将数据以块的形式推送到浏览器.因此,您可以在渲染之前刷新浏览器.这允许浏览器在收到整个文档之前获取(阻塞)脚本和css等资源.("管道加载文件")
至于可维护性,我正在使用TI和{include}方法.永远不要多次display()调用.如果发生了变化,我必须触摸太多脚本.