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

{if $edit_flag}
	<input type=checkbox name=edit value="y"> edit <br>
{/if}

It is up to the application programmer to assign the $edit_flag, a simple and easy-to-understand variable in the template. This way the template is no longer relying on your underlying data structure. If the format of the session data structure ever changes, nothing needs to be adjusted in the template.

Now lets look at a few things you can do with Smarty. One thing it can do is custom functions. These are tags in the template that execute a certain task. Example:

{html_image file="masthead.gif"}

Here we have a function called "html_image". This function takes the image given in the "file" attribute and does all the work necessary to come up with the following HTML code:

<img src="masthead.gif" border="0" height="48" width="256">

The image function did the chore of figuring out the height and width and supplying the default border flag. Of course you could just use the static HTML tag in the template instead, but this demonstrates how a custom function can be utilized to simplify a very common task. The designer can focus on design and less on the technical stuff. Furthermore, if the designer decides to drop in a different size masthead image, the template does not need adjusting.

html_image is a function that comes with Smarty. You can also make your own custom functions. Here's another example of what one might look like:

{html_link type="article" id="abc123" text="Fire takes out Hotel"}

This is using a custom function called "html_link". It comes up with the following HTML code:

<a href="/display_article.php?id=abc123">Fire takes out Hotel</a>

What does this accomplish? For one, the designer does not need to be concerned with the format of a URL to an article. With hard-coded URLs, what happens if one day the programmer decides to clean things up, and changes the URL syntax from /display_article.php?id=abc123 to /ART/abc123 ? We would have to edit every template with an article URL. This is just another example of how a template function can make templates easier to maintain.

 

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:
(阅读次数:

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