问题
Which method of pulling together the templated parts of my site is best practice? I prefer the first solution, but I'm not sure using multiple calls to display() is good practice. I'm looking for ease of maintenance and speed.
<?php
$smarty->display('header.tpl');
$smarty->display('menu.tpl');
$smarty->display('article1.tpl');
$smarty->display('footer.tpl');
?>
or displaying a single smarty template and then within the template having
{include file="header.tpl"}
<body id={$pageid}>
{include file="menu.tpl"}
{include file="header_inner.tpl"}
Content of page
{include file="footer.tpl"}
回答1:
If you're using Smarty3 (which you should) have a look at Inheritance and Template Inheritance. It allows you to define templates much like you'd build your classes - OOP style.
If you can't (or don't want to) roll with TI, I'd suggest the {include}
approach. Reasons:
- Reducing API between PHP and templates
- allows output caching in a simpler fashion
- allows $cache_modified_check for basic
HTTP 304 Not Modified
handling right out of the box - can be optimized (by Smarty3) through {include … inline} to reduce filesystem I/O
- can be generally optimized (by Smarty3) through $merge_compiled_includes (still reducing filesystem I/O)
There is (only) one advantage of multiple display() calls. You can push data to the browser in chunks. So you could flush the to the browser before your is rendered. This allows the browser to fetch (blocking) resources like scripts and css before it even received the whole document. ("Pipelining the loading Of documents")
As for maintainability, I'm using TI and {include} approaches. Never multiple display() calls. I'd have to touch too many scripts If something changed.
来源:https://stackoverflow.com/questions/8019093/calling-smarties-display-method-multiple-times-vs-using-includes