The template at a glance一目了然的模板

The template at a glance一目了然的模板
Page Contents 网页内容

Intro. 前奏。
Examples of directives 指令的例子
The if directive “if指令
The list directive 该清单指令
The include directive include指令
Using directives together 使用指令一起
Dealing with missing variables 处理缺失变量

The simplest template is a plain HTML file (or whatever text file -- FreeMarker is not confined to HTML).最简单的模板是一个普通的HTML文件(或任何文本文件 - FreeMarker是不局限于HTML)。 When the client visits that page, FreeMarker will send that HTML to the client as is.当客户端访问该页面时,FreeMarker会向客户端发送的HTML。 However if you want that page to be more dynamic then you begin to put special parts into the HTML which will be understood by FreeMarker:然而,如果您希望该网页更加动态的,那么你开始投入将由FreeMarker的理解的HTML特殊部位:

${ ... } : FreeMarker will replace it in the output with the actual value of the thing inside the curly brackets. $ {...}:FreeMarker会在输出取代它与实际值的大括号内的事情。 They are called interpolation s.他们被称为插值号 As an example see the very first example .作为一个例子, 看到的第一个例子。

FTL tags (for FreeMarker Template Language tags): FTL tags are a bit similar to HTML tags, but they are instructions to FreeMarker and will not be printed to the output. FTL标签 (FreeMarker模板语言标记):FTL标签有点类似于HTML标签,但他们对FreeMarker的指令,将不被打印输出。 The name of these tags start with # .这些标记的名称以#开头。 (User-defined FTL tags use @ instead of # , but they are an advanced topic.) (用户自定义FTL标签用@代替#,但他们是一个高级主题。)

Comments: Comments are similar to HTML comments, but they are delimited by <#-- and --> . 评论:是类似于HTML注释的评论,但它们所分隔的<# -和- > 。 Anything between these delimiters and the delimiter itself will be ignored by FreeMarker, and will not be written to the output.之间的分隔符和分隔符本身的东西都会被忽略的FreeMarker将不会被写入到输出。

Anything not an FTL tag or an interpolation or comment is considered as static text, and will not be interpreted by FreeMarker; it is just printed to the output as is.任何FTL标签或插值或意见被认为是为静态文本,并不会被FreeMarker的解释,它只是打印输出。

With FTL tags you refer to so-called directives . FTL标签,你是指所谓的指令。 This is the same kind of relationship as between HTML tags (eg: <table> and </table> ) and HTML elements (eg, the table element) to which you refer to with the HTML tags.这是HTML标签(如:<table>和</ TABLE>)之间的关系的同类和你所提到HTML标记的HTML元素( 例如 ,表元素)。 (If you don't feel this difference then just take "FTL tag" and "directive" as synonyms.) (如果你不觉得这种差异,然后只取​​“FTL标签”和“指令”作为同义词。)

Examples of directives指令的例​​子
Though FreeMarker has far more directives, in this quick overview we will only look at three of the most commonly used ones.虽然FreeMarker的指令得多,在这个快速概览,我们只会看看三个最常用的。

The if directive “if指令
With the if directive you can conditionally skip a section of the template.随着if指令,你可以有条件地跳过一段模板。 For example, assume that in the very first example you want to greet your boss, Big Joe, differently from other users:例如,假设在第一个例子中,你要迎接你的老板,大乔,不同于其他用户:

  
<html> <HTML>
<head> <HEAD>
  <title>Welcome!</title> <TITLE>欢迎!</ TITLE>
</head> </ HEAD>
<body> <BODY>
  <h1> <H1>
    Welcome ${user} <#if user == "Big Joe"> , our beloved leader </#if> !欢迎$ {USER} <#如果用户==“大乔”,我们敬爱的领袖</#如果>!
  </h1> </ H1>
  <p>Our latest product: <p>我们的最新产品:
  <a href="${latestProduct.url}">${latestProduct.name}</a>! <a href="${latestProduct.url}"> $ {latestProduct.name} </ A>!
</body> </ BODY>
</html> </ HTML>  
 
  

Here you have told FreeMarker that the '', our beloved leader'' should be there only if the value of the variable user is equal to the string "Big Joe" .在这里,你告诉FreeMarker的“,应该是我们敬爱的领袖”,只有当变量用户的值等于字符串“大乔“。 In general, things between <#if condition > and </#if> tags are skipped if condition is false (the boolean value).在一般情况下,事物之间的<#如果条件 >和</>标记#如果被跳过,如果条件为假(布尔值)。

Let's detail the condition used here: The == is an operator that tests if the values at its left and right side are equivalent, and the results is a boolean value, true or false accordingly.让我们详细的条件下使用:==是运营商的测试,如果在其左边和右边的值相等,其结果是一个布尔值,真或假的相应。 On the left side of == I have referenced a variable with the syntax that should be already familiar; this will be replaced with the value of the variable. ==的左边,我引用一个变量的语法应该已经熟悉,这将是变量的值取代。 In general, unquoted words inside directives or interpolations are treated as references to variables.在一般情况下,内部指令或插值加引号的话被视为变量的引用。 On the right side I have specified a literal string.在右边,我指定一个文字字符串。 Literal strings in templates must always be put inside quotation marks.在模板中的文字字符串必须始终放在引号内。

This will print "Pythons are free today!"这将打印“蟒蛇是免费的今天!” if their price is 0:如果他们的价格是0:

  
<#if animals.python.price == 0 > <#如果animals.python.price == 0>
  Pythons are free today!蟒蛇是免费的今天!
</#if> </#“>  
 
  

Similarly as earlier when a string was specified directly, here a number is specified directly ( 0 ).同样早先直接指定一个字符串时,这里是直接指定(0)。 Note that the number is not quoted.请注意,这一数字是没有报价。 If you quoted it ( "0" ), FreeMarker were misinterpret it as a string literal.如果你引用它(“0”),FreeMarker的被误解为一个字符串常量。

This will print "Pythons are not free today!"这将打印“蟒蛇是不是免费的今天!” if their price is not 0:如果他们的价格是不为0:

  
<#if animals.python.price != 0> <#如果animals.python.price!= 0>
  Pythons are not free today!蟒蛇是不是免费的今天!
</#if> </#“>  
 
  

As you may have guessed, != means not equivalent.正如您可能已经猜到了,=表示不等于。

You can write things like this too (using the data-model used to demonstrate hashes ):你可以写这样的东西太多(使用的数据模型,用于演示哈希) :

  
<#if animals.python.price < animals.elephant.price > <#如果animals.python.price <animals.elephant.price>
  Pythons are cheaper than elephants today.蟒蛇是比大象更便宜的今天。
</#if> </#“>  
 
  

With the <#else> tag you can specify what to do if the condition is false.随着其他<#>标记,您可以指定做什么,如果条件为假。 For example:例如:

  
<#if animals.python.price < animals.elephant.price> <#如果animals.python.price <animals.elephant.price>
  Pythons are cheaper than elephants today.蟒蛇是比大象更便宜的今天。
<#else> <其他>
  Pythons are not cheaper than elephants today.蟒蛇是不是今天比大象更便宜。
</#if> </#“>  
 
  

This prints ''Pythons are cheaper than elephants today.'' if the price of python is less than the price of elephant, or else it prints ''Pythons are not cheaper than elephants today.''这将打印“蟒蛇比大象便宜。''如果Python的价格不到大象的价格,否则打印”蟒蛇是不是今天比大象更便宜。

If you have a variable with boolean value (a true/false thing) then you can use it directly as the condition of if :如果你有一个变量,布尔值(真/假的东西),那么你可以直接使用 ,如果条件:

  
<#if animals.python.protected> <#如果animals.python.protected>
  Warning!警告! Pythons are protected animals!蟒蛇是受保护动物!
</#if> </#“>  
 
  

The list directive该清单指令
This is useful when you want to list something.当你要列出的东西,这是非常有用的。 For example if you merge this template with the data-model I used earlier to demonstrate sequences :例如,如果合并与模板数据模型,我之前使用证明序列:

  
<p>We have these animals: <table border=1> <tr><th>Name<th>Price <#list animals as being> <tr><td>${ being .name}<td>${ being .price} Euros </#list> </table> <p>我们有这些动物:<table border=1> <TR> <TH>名<TH>价格<TR> <TD> $ {。名称} <TD> $ {<#被列表动物>价格}欧元</列表> </ TABLE>  
 
  

then the output will be:那么输出将是:

  
<p>We have these animals: <p>我们有这些动物:
<table border=1> <table border=1>
  <tr><th>Name<th>Price <TR> <TH>名<TH>价格
  <tr><td>mouse<td>50 Euros <TR> <TD>鼠标<TD> 50欧元
   <tr><td>elephant<td>5000 Euros <TR> <TD>大象<TD> 5000欧元
   <tr><td>python<td>4999 Euros <TR> <TD>蟒蛇<TD> 4999欧元
</table> </ TABLE>  
 
  

The generic format of the list directive is: 列表指令的通用格式是:

<#list sequence as loopVariable > repeatThis </#list> <#列表序列loopVariable> repeatThis </#列表>

The repeatThis part will be repeated for each item in the sequence that you have given with sequence , one after the other, starting from the first item. repeatThis部分重复序列中的每个项目,你给带序列,一前一后,从第一项开始。 In all repetitions loopVariable will hold the value of the current item.在所有重复loopVariable将保持当前项的值。 This variable exists only between the <#list ...> and </#list> tags.这个变量之间只存在<#列表...>和</#列表>标记。

As another example, we list the fruits of that example data model:作为另一个例子,我们的清单,例如数据模型的成果:

  
<p>And BTW we have these fruits: <p>和顺便说一句,我们有以下成果:
<ul> <UL>
<#list whatnot.fruits as fruit> <#列为水果whatnot.fruits>
<li>${fruit} <LI> $ {水果}
</#list> </#列表>
<ul> <UL>  
 
  

The whatnot.fruits expression should be familiar to you; it references a variable in the data-model . whatnot.fruits表达应该是你熟悉的,它引用了一个数据模型中的变量。

The include directive include指令
With the include directive you can insert the content of another file into the template.随着包括指令,可以插入到模板中的其他文件的内容。

Suppose you have to show the same copyright notice on several pages.假设你有几页上显示的相同的版权声明。 You can create a file that contains the copyright notice only, and insert that file everywhere where you need that copyright notice.您可以创建一个文件,其中包含的版权声明,并到处插入该文件,你需要的版权声明。 Say, you store this copyright notice in copyright_footer.html :说,你存储在copyright_footer.html本版权声明:

  
<hr> <HR>
<i> <I>
Copyright (c) 2000 <a href="http://www.acmee.com">Acmee Inc</a>,版权所有(c)2000 <a href="http://www.acmee.com"> Acmee公司</ A>,
<br> -
All Rights Reserved.保留所有权利。
</i> </ I>  
 
  

Whenever you need that file you simply insert it with the include directive:每当你需要的文件,你只需插入 include指令:

  
<html> <HTML>
<head> <HEAD>
  <title>Test page</title> <TITLE>测试页面</ TITLE>
</head> </ HEAD>
<body> <BODY>
  <h1>Test page</h1> <H1>测试页</ H1>
  <p>Blah blah... <P> BLAH BLAH ...
<#include "/copyright_footer.html"> <#包括“/ copyright_footer.html”>
</body> </ BODY>
</html> </ HTML>  
 
  

and the output will be:输出将是:

  
<html> <HTML>
<head> <HEAD>
  <title>Test page</title> <TITLE>测试页面</ TITLE>
</head> </ HEAD>
<body> <BODY>
  <h1>Test page</h1> <H1>测试页</ H1>
  <p>Blah blah... <P> BLAH BLAH ...
<hr> <HR>
<i> <I>
Copyright (c) 2000 <a href="http://www.acmee.com">Acmee Inc</a>, 版权所有(c)2000 <a href="http://www.acmee.com"> Acmee公司</ A>,
<br> -
All Rights Reserved. 保留所有权利。
</i> </ I>
</body> </ BODY>
</html> </ HTML>  
 
  

If you change the copyright_footer.html , then the visitor will see the new copyright notice on all pages.如果你改变的copyright_footer.html,那么游客将看到在所有网页上的新的版权声明。

Using directives together使用指令一起
You can use directives as many times on a page as you want, and you can nest directives into each other similarly as you can nest HTML elements into each other.只要你想,一个页面上,您可以使用多次指示,并可以巢到对方的指令同样可以嵌套的HTML元素相互转化。 For example this will list the animals and print the name of large animals with bigger font:例如,这将列出动物和大型动物具有更大的字体打印的名称:

  
<p>We have these animals: <p>我们有这些动物:
<table border=1> <table border=1>
  <tr><th>Name<th>Price <TR> <TH>名<TH>价格
  <#list animals as being> <#列出的动物 。
  <tr> <TR>
    <td> <TD>
      <#if being.size == "large"> <font size="+1"> </#if> <#如果being.size ==“大”> <font size="+1"> </#如果>
      ${being.name} $ {being.name}
      <#if being.size == "large"> </font> </#if> <#如果being.size ==“大”> </ FONT> </#如果>
    <td>${being.price} Euros <TD> $ {being.price}欧元
  </#list> </#列表>
</table> </ TABLE>  
 
  

Note that since FreeMarker does not interpret text outside FTL tags, interpolations and comments, it doesn't see the above font tags as badly nested ones.请注意,因为FreeMarker的不解释FTL标签外,插值和注释的文本,它没有看到上述严重嵌套的字体标记。

Dealing with missing variables处理缺失变量
In practice the data-model often has variables that are optional (ie, sometimes missing).数据模型在实践中,往往已是可选的变量(即,有时会丢失)。 To spot some typical human mistakes, FreeMarker doesn't tolerate the referring to missing variables unless you tell them explicitly what to do if the variable is missing.发现一些典型的人为错误,FreeMarker的不容忍指的是缺少变量,除非你明确告诉他们做什么如果变量是丢失。 Here we will show the two most typical ways of doing that.在这里,我们将展示两个最典型的方式这样做。

Note for programmers: A non-existent variable and a variable with null value is the same for FreeMarker, so the "missing" term used here covers both cases. 程序员注意:一个不存在的变量和一个空值的变量是FreeMarker的相同,所以“失踪”一词用在这里包括两种情况 。

Wherever you refer to a variable, you can specify a default value for the case the variable is missing, by following the variable name with a ! and the default value.无论你是指一个变量,你可以指定一个变量缺失的情况下的默认值,以下!和默认值的变量名。 Like in the following example, when user is missing from data model, the template will behave like if user 's value were the string "Anonymous" .在下面的例子一样,当用户丢失数据模型,该模板将像用户的价值,如果字符串“无名氏”。 (When user isn't missing, this template behaves exactly like if !"Anonymous" were not there): (当用户不缺,这个模板的功能完全一样,如果“无名氏”不存在!):

  
<h1>Welcome ${user !"Anonymous" }!</h1> <h1>欢迎$ {用户!“无名氏”!} </ H1>  
 
  

You can ask whether a variable isn't missing by putting ?? after its name.你可以要求通过把一个变量是否是不缺?后它的名字。 Combining this with the already introduced if directive you can skip the whole greeting if the user variable is missing:结合已经出台,如果指令可以跳过整个的问候,如果用户变量缺少这样的:

  
<#if user?? ><h1>Welcome ${user}!</h1></#if> <#如果用户?> <h1>欢迎$ {USER}!</ H1> </#如果>  
 
  

Regarding variable accessing with multiple steps, like animals.python.price , writing animals.python.price!0 is correct only if animals.python is never missing and only the last subvariable, price , is possibly missing (in which case here we assume it's 0 ).关于变量与多个步骤, 例如 animals.python.price,访问, 写作animals.python.price 0是正确的只有 animals.python是从来不缺少,只有最后的子变量,价格,可能是缺少(在这种情况下,我们在这里假定它的0)。 If animals or python is missing, the template processing will stop with an "undefined variable" error.如果缺少动物或 Python,模板处理将停 ​​止与“未定义的变量”的错误。 To prevent that, you have to write (animals.python.price)!0 .为了防止这种情况,你必须写(animals.python.price 0)。 In that case the expression will be 0 even if animals or python is missing.在这种情况下,表达式将0动物或 Python,即使丢失。 Same logic goes for ?? ; animals.python.price?? versus (animals.python.price)?? .同样的逻辑??animals.python.price与(animals.python.price)? 。
Next page: Values, Types 下一页:价值型 Previous page 前一页 Parent page 父页 Contents 目录
You are here: Book > Template Author's Guide > Getting Started > The template at a glance 您的位置: 图书 > 模板作者的指南 > 入门 >一目了然模板 
FreeMarker Manual -- For FreeMarker 2.3.18 FreeMarker的手册-对于FreeMarker的2.3.18
HTML generated: 2011-05-22 08:01:37 GMT 生成的HTML:2011年5月22日8时01分37秒GMT   



英语原文:
The template at a glance

你可能感兴趣的:(template)