幻想指点江山,梦中激扬文字(飞天小肥猪的简单人生 Register | Login
浏览模式: 标准 | 列表分类:Smarty

smarty3即将出来

PHP的模版里,smarty一直就是让人又爱又恨的,功能太强大了,以致于美工们看到他也会感觉头疼,毕竟是又要学一门新的语言了。
这么多年,smarty都是在2.x版本里面徘徊,如今,终于要出3了。
以下是官方的change log:

10/6/2008

- completed compilation of {include} tag, variable scoping same as in smarty2

- added compilation of {capture} tag.

- added error message on none existing template files.

10/4/2008
- added comments in the parser definition y file.

- added array of $smarty_token_names to lexer for use in trigger_template_error function

- change handling of none smarty2 style tags like {if}, {for}....

- lexer/parser optimization

10/3/2008

- create different compiled template files depending if caching and/or security is enabled

- cleaned up compiler

- lexer/parser optimization

10/2/2008

- new method trigger fatal error, displays massage and terminates smarty

- compile error status flag added
The compiler will in case of error continue to parse the template(s) to display all errors. No compiled
templates will be written, Smarty terminates after the compiler exits.

- added error message to function __call in Smarty.class.php

Tags: smarty, version, php, template, smarttemplate

smarty中的注释

写代码的时候不可避免的会使用到注释。大多数的情况下,我们都是使用<!-- 这里是注释 -->,因为这是HTML自带的注释功能,在这里的代码都不会被显示到浏览器。

然而,使用了smarty之类,我们确实不是很建议这样使用,因为,在<!---->标记里的smarty代码其实还是被解析了,如果是这样的话,那么,我们其实是多做了很多事情,却没有被显示出来,那就是说,我们其实多做了很多无用功。

因此,我们在使用smarty模版的时候,应该根据smarty的规范来。让我们看看手册怎么说:


 所有例子中,我们假定你使用缺省的分隔符。Smarty中,所有在分隔符之外的内容被显示为静态内容,或者说不会被改变。一旦Smarty遇见分隔符,它将尝试解释它们,然后在其位置处显示合适的内容。

注释

    模板注释由星号包围,继而由分隔符包围,型如:{* 这是一个注释 *}。Smarty注释不会在最终模板的输出中显示,这点和<!-- HTML comments -->不同。前者对于在模板中插入内部注释有用,因为没有人能看到。;-)

 

模版中的注释
  1. {* 这是Smarty注释,不出现在编译后的输出中 *}  
  2. <html>  
  3. <head>  
  4. <title>{$title}</title>  
  5. </head>  
  6. <body>  
  7.   
  8. {* 另一个单行Smarty注释 *}  
  9. <!-- HTML注释将发送到浏览器 -->  
  10.   
  11. {* 这是一个多行  
  12.    Smarty注释  
  13.    并不发送到浏览器  
  14. *}  
  15.   
  16. {*********************************************************  
  17. 多行注释块,包含了版权信息  
  18.   @ author:         bg@example.com  
  19.   @ maintainer:     support@example.com  
  20.   @ para:           var that sets block style  
  21.   @ css:            the style output  
  22. **********************************************************}  
  23.   
  24. {* 包含了主LOGO和其他东西的头文件 *}  
  25. {include file='header.tpl'}  
  26.   
  27.   
  28. {* 开发注解:$includeFile变量在foo.php脚本中赋值 *}  
  29. <!-- 显示主内容块 -->  
  30. {include file=$includeFile}  
  31.   
  32. {* 该<select>块是多余的 *}  
  33. {*  
  34. <select name="company">  
  35.   {html_options options=$vals selected=$selected_id}  
  36. </select>  
  37. *}  
  38.   
  39. {* 模板的cvs标记。下面的36应该是美元符号。  
  40. 但是在CVS中被转换了。 *}  
  41. {* &#36;Id: Exp &#36; *}  
  42. {* $Id: *}  
  43. </body>  
  44. </html>  

 

Tags: smarty, 注释

smarty中的变量使用

smarty中的变量和平时使用有点区别,比如$aa.bb其实代表的是$aa['bb'],具体如何个使用法,其实在手册里已经有详细说明了

    Smarty可以识别嵌入在双引号中赋值变量,只要变量名只包含数字,字母,下划线和方括号[](参见命名)。如果有其它字符(如句点,对象引用等),变量必须由反引号对`backticks`包含。你不可以嵌入修饰符,它们必须永远在引号之外使用。

实际使用中应该是这样的:

语法例子:
{func var="test $foo test"} <-- 使用$foo
{func var="test $foo_bar test"} <-- 使用$foo_bar
{func var="test $foo[0] test"} <-- 使用$foo[0]
{func var="test $foo[bar] test"} <-- 使用$foo[bar]
{func var="test $foo.bar test"} <-- 使用$foo(不是$foo.bar)
{func var="test `$foo.bar` test"} <-- 使用$foo.bar

{func var="test `$foo.bar` test"|escape} <-- 修饰符在引号外!

实际例子:
{include file="subdir/$tpl_name.tpl"} <-- 将以实际值替换$tpl_name
{cycle values="one,two,`$smarty.config.myval`"} <-- 必须有反引号!

看清楚哦。平时在使用的时候应该是感觉不出问题的,只有用在函数、循环里面,这才会成为使用中的问题。

Tags: smarty, 变量

Smarty的一些常用方法

网上流传的大师兄教程确实给了我们很多方便,只是大师兄教程却不是很全面。或许他给很多设计师们带来了福音,但对程序开发人员来说,并没有什么特别的高深之处。

不过,我还是挺佩服他的,毕竟,他将自己的思想和实现方法提供出来,如果每个人都这样,或许,我们能够再进步一些。呵呵

在工作,仍然是在使用着smarty,也在开发中积累了一点点的心得,逐步写出来,与大家共同分享,当然应该会存有错误,与大家一起改进。

smarty在使用时是需要配置的,最简单的配置方法就是将基本的变量写成一个数组,然后new Smarty之后,foreach一下,由$smarty自行加载,如:

---------------------------------------------

辛苦写了半天,结果超时了。下次补上,今天没时间了。