<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My Home(1 words)</title>

{literal}
<style type="text/css">
p {
margin: 5px;
}
</style>
{/literal}
</head>
<body bgcolor="#00ffff">
<center><h2>this is the header.tpl here</h2></center> <p>Hi, Jason Gilmore(2). Welcome to the wonderful world of Smarty.</p>
<p>Submitted on: Feb 24, 07</p>
<p>Author: <strong>Anonymous</strong></p>
<p><i> Snow  Expected  in Northeast </i></p>
<p>
OH: Snow expected in the Northeast o...<br />
CA: Sunny and warm weather expected ...<br />
NY: Softball-sized hail reported in ...<br />
</p>
<p>
<p>No entries matching your query were found.</p>
</p>

<center><h2>this is the footer.tpl here</h2></center>
</body>
</html>
没有{literal}这段CSS代码时显示的页面:
this is the header.tpl here
Hi, Jason Gilmore(2). Welcome to the wonderful world of Smarty.Submitted on: Feb 24, 07Author: AnonymousSnow Expected in Northeast OH: Snow expected in the Northeast o...
CA: Sunny and warm weather expected ...
NY: Softball-sized hail reported in ...No entries matching your query were found.
this is the footer.tpl here
句与句之间是有空格的(因为用了<p>);但多了{literal}这段CSS后却执行了CSS语句(变得句与句之间的空格为5px)。为什么会这样呢?不是说{literal}内的语句不会执行,只会原本显示的吗?

解决方案 »

  1.   

    如果你没用literal,会把大括号里面的数据当做变量解析,所以CSS,特别是javascript等需要用到大括号{}的地方都加上这个标签!
    Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 {literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示.
      

  2.   


    但用{literal}与不用没有什么区别:结果都是一样的,执行css中的margin: 5px的语句
      

  3.   

    不是说{literal}内的语句不会执行,只会原本显示的吗?
    是的,是这样的
    不过你需要清楚的知道这个“语句”是指 Smarty 的语法成分
    也就是说在 {literal} 中,任何 Smarty 成分都不会被解析
      

  4.   

    你理解错了,加上literal标签只是不解析{xxx}这种东西,因为,smarty解析函数时候是这种格式,并没有说不执行里面的东西,你以为是注释啊