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

Smarty:Why use it?模板怎么使用的呢?php开源com

        One of Smartys primary design goals is to facilitate the separation of application code from presentation. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers. php开源com

       At its most basic function, the application code collects content, assigns it to the template engine and displays it. The content might be something like the headline, tagline, author and body of a newspaper article. The application code has no concern how this content will be presented in the template. The template designer is responsible for the presentation. They edit the template files, adding markup and bringing it to completion. This typically involves things like HTML tags, cascading style sheets and other tools provided by the template engine.

This paradigm serves several purposes: php开源com

*) Designers can't break application code. They can mess with the templates all they want, but the code stays intact. The code will be tighter, more secure and easier to maintain.

*) Errors in the templates are confined to the Smartys error handling routines, making them as simple and intuitive as possible for the designer.

*) With presentation on its own layer, designers can modify or completely redesign it from scratch, all without intervention from the programmer.

*) Programmers aren't messing with templates. They can go about maintaining the application code, changing the way content is acquired, making new business rules, etc. without disturbing the presentation layer.

*) Templates are a close representation of what the final output will be, which is an intuitive approach. Designers don't care how the content got to the template. If you have extraneous data in the template such as an SQL statement, this opens the risk of breaking application code by accidental deletion or alteration by the designer.

*) You are not opening your server to the execution of arbitrary PHP code. Smarty has many security features built in so designers won't breach security, whether intentional or accidental. They can only do what they are confined to in the templates.

 

Although application code is separated from presentation, this does not necessarily mean that logic is separated. The application code obviously has logic, but the templates may have logic based on the condition that it is for presentation only. For example, if the designer wants to alternate table row colors or upper-case some assigned content, they can. This is presentation logic, something the programmer should not be concerned with. How often have you had some presentation displayed in a single column and then you wanted it in two or three columns, so the application code needs adjusting to accomodate this? A better approach is to assign the content in one single array and let the template handle the presentation. This will simplify your application and keep your templates flexible. Smarty supplies the tools to handle this kind of situation.

This doesn't mean that Smarty prevents you from putting application logic in the template, you have to have a bit of self discipline. Here is an example of embedding business logic in the template (that's right, avoid doing this if at all possible): php开源com

{if $smarty.session.user and ( $user_type eq "editor" or $user_type eq "admin" )}
	<input type=checkbox name=edit value="y"> edit <br>
{/if}

The logic checks if the user is logged in and they are either an editor or administrator, then they are allowed to edit this so the edit checkbox shows up. That is logic that belongs in the application code. The template doesn't care about what credentials this user has, it just needs to know if the edit box is displayed or not! So let's look at a more suitable approach:
(阅读次数:

共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号