PHP&Smarty Why use it? 怎样使用smarty的..

2008-01-25 23:13 来源: smarty 作者:phpma 网友评论 0 条 浏览次数 0

Now for a bit on programmers and templates. Earlier it was mentioned that the programmer has no care for what the templates do with the content. At a conceptual level this is true, but in the real world you are not going to expect the template designer to have to construct all the templates out of thin air. After all, the business logic does determine what content is assigned to the templates. So, the programmer will typically setup skeleton templates for the designer to start with. This usually contains the raw elements such as content variables and section loops, and maybe a few simple markup tags so they don't start with the content in a big mess. Here is an example of a skeleton template that loops through a list of articles and displays them in a table:

<table>
   {section name=art loop=$article}
      <tr>
         <td>{$article[art].headline}<td>
         <td>{$article[art].date}<td>
         <td>{$article[art].author}<td>
      </tr>
   {/section}
</table>

The output may look something like this:

<table>
   <tr>
      <td>How the west was won<td>
      <td>Dec 2, 1999<td>
      <td>John Wayne<td>
   </tr>
   <tr>
      <td>Team loses, Coach quits<td>
      <td>Feb 2, 2002<td>
      <td>John Smith<td>
   </tr>
   <tr>
      <td>Gourmet Cooking<td>
      <td>Jan 23, 1954<td>
      <td>Betty Crocker<td>
   </tr>
</table>

Now for some common questions:

Why use templates at all? What is so tough about writing <? echo $title; ?> instead of {$title}?

Making things easier to read wasn't a design goal, but more of a side effect. Using templates has huge benefits, many of which have been explained above. Since we are in a template environment anyways, {$title} is less extraneous than <?php echo $title; ?>, especially when you start looking at it in long pages of content, so it was pretty evident that a simpler syntax helps to make templates easier to read and maintain.

Template take time to parse, making applications much slower.

That may be true in some cases, but with Smarty it is no slower than executing a PHP script. On the first execution of a template, Smarty converts the template files into PHP scripts (called template compiling.) Thereafter, the PHP script is just included. Couple this with a PHP accelerator and you truly have a fast templating environment with minimal overhead.

[上一页1  2  3  4 [下一页]
上一篇:Smarty php Cras..    下一篇:Using suexec To ..

相关主题:

网友评论