Calling Smarties display() method multiple times vs using includes

拈花ヽ惹草 提交于 2019-12-10 11:26:54

问题


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:

  1. Reducing API between PHP and templates
  2. allows output caching in a simpler fashion
  3. allows $cache_modified_check for basic HTTP 304 Not Modified handling right out of the box
  4. can be optimized (by Smarty3) through {include … inline} to reduce filesystem I/O
  5. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!