php开源嘛
首页 | English | MySql数据库 | Php编程 | 建站杂烩 | 系统服务器 | 资源共享 | 专题 | 才桀网 | 支持论坛
  当前位置:主页>English>文章内容
Smarty:Why use it?模板怎么使用的呢?
来源:smarty 作者:未知 发布时间:2008-02-22  

<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.

Smarty is too complicated, how can it be that fast?

Smarty's core is pretty lean considering what it is capable of. Most of its functionality lies in plugins. The plugin architecture is designed so that only the required plugins are loaded on demand. With this framework, adding even hundreds of new plugins will not affect performance. This makes Smarty fast, scalable and flexible.

Smarty also has caching features that can dynamically refresh and keep portions of the page uncached at your liking. Caching stores the output of the compiled templates, saving the need to execute them on each invocation.

All this talk about accelerators, how does Smarty run without one?

Actually it runs pretty well without one. Smarty does not require an accelerator, but the template files themselves will take advantage of one, something that is unique to Smarty (AFAIK). If you don't have an accelerator, template execution isn't as fast but not slow by any means since they aren't parsed! You also retain all the other benefits and features of Smarty. Also, since accelerators are freely available there isn't really an excuse to not be using one. They'll help performance with all PHP apps, using Smarty or not.

How can it be easier to maintain?

Some things can't be explained, but only experienced :-) The benefit of separation of the application logic from the presentation cannot be stressed enough. Smarty also has some nice error handling features and a built-in debugging console so you can see the template heirarchy and assigned variables at a glance. Adding custom features to Smarty is as easy as dropping them in the plugin directory and mentioning them in the template.

The template tags are not XML based, My editor doesn't like it.

The {} delimiters are just a default, they are easy to discern among HTML tags. If you don't like them, change your delimiters to <% %> or maybe something more XMLish like <smarty: > There are also lots of user contributions for dreamweaver and the like, give them a look in the contribs area.

That's Smarty in a nutshell, hopefully you can add it to your arsenal of tools for web application building. To really learn more, read the manual top to bottom, join the forums and see what people are discussing.



(阅读次数:
共3页: 上一页 [1] [2] 3 下一页
上一篇:如何正确认识MySQL对服务器端光标的限制   下一篇:PHP中的面向对象和面向过程OOP……
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·什么是Smaty?一个php模板引擎.
·Mysql中innodb 和myisam的区别?
·what is apache? 服务器
·What is PHP?
·Apache是一款流行的伺服器
·what is openssh?而ssh又是什么?
·What is MySQL? MySQL数据库
·what is Linux? 操作系统
·php|works, Session Day 1
·Late static binding....sorta .
·What is EditPlus?How to Instal
·php表达式之explode() 分割字符
  相关文章
·PHP&Smarty Is Smarty right for
·Ajax 能够做什么?
·Ajax技术(五)用AjaxTags简化Ajax
·Ajax技术(四)用AjaxTags简化Ajax
·Ajax技术(三)用AjaxTags简化Ajax
·Ajax技术(二)用AjaxTags简化Ajax
·Ajax技术(一)用AjaxTags简化Ajax
·php表达式之explode() 分割字符
·what is openssh?而ssh又是什么?
·Mysql中innodb 和myisam的区别?
·What is EditPlus?How to Instal
·Late static binding....sorta .

关于我们 | 本站声明 | 友情连接 | 诚邀加盟 | 网站地图
版权Power by DedeCms   技术支持QQ =>> 罗江游鱼   Jacking  后台登陆
Copyright @ 2007 公司地址:柳州市东环路268号 邮编:545006 电话:15920389818 桂ICP备07006725号