原文传送门

1. Template + data-model = output

data-model是一个树状模型,通常是一个java对象。


2.data-model 入门

hashes(散列):目录变量(the root, misc)

scalars(标量):存储单个值的变量(size, price, name and foo)

sequences(序列):They store subvariables like hashes, but here subvariables doesn't have a name, they are just items in a list. (数组?)(animals)

(root)
  |
  +- animals
  |   |
  |   +- (1st)
  |   |   |
  |   |   +- name = "mouse"
  |   |   |
  |   |   +- size = "small"
  |   |   |
  |   |   +- price = 50
  |   |
  |   +- (2nd)
  |   |   |
  |   |   +- name = "elephant"
  |   |   |
  |   |   +- size = "large"
  |   |   |
  |   |   +- price = 5000
  |   |
  |   +- (3rd)
  |       |
  |       +- name = "python"
  |       |
  |       +- size = "medium"
  |       |
  |       +- price = 4999
  |
  +- misc
     |
      +- foo = "Something"

scalars的数据类型:

  • String
  • Number
  • Date-like: Either a date-time (stores a date with time of the day), or a date (no time of day), or a time (time of day, no date).
  • Boolean

Summary

  • data-model呈现树状结构;
  • Scalars store a single value. The value can be a string or a number or a date-time/date/time or a boolean.
  • Hashes are containers that store other variables and associate them with a unique lookup name.
  • Sequences are containers that store other variables in an ordered sequence. The stored variables can be retrieved via their numerical index, starting from 0.

3. template 入门

3.1 基本语法

  • 插值${...}:FreeMarker将在输出中将其替换为大括号内的表达式的实际值。
  • FTL标签(FTL=FreeMarker Template Language,FreeMarker模板语言):类似于HTML标签(but they are instructions to FreeMarker and will not be printed to the output)。标签名以#开头(用户定义的FTL标签使用@替代#)。
  • 注释:包含在<#---->之间。不会输出到最终的HTML文件中,因为FreeMarker在编译的时候会跳过这些注释。

    除了以上3中语法中的内容都会被视为静态文本,即FreeMarker编译时不会处理这些文本,而是直接输出到HTML文件中。

3.2 常用指令

使用FTL标签可以引用所谓的指令。

3.2.1 if指令

例子:

Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>!

如果条件为false,<#if condition><#if>之间的内容将会被跳过。

配合<#else>指令可以指定条件不成立时执行的内容。

使用<#elseif>进一步细化条件。

3.2.2 list指令

语法:

<#list sequence as loopVariable>repeatThis</#list>

使用示例一:

<ul>
<#list misc.fruits as fruit>
  <li>${fruit}
</#list>
</ul>

使用示例二:

<#list misc.fruits>
  <ul>
    <#items as fruit>
      <li>${fruit}
    </#items>
  </ul>
</#list>

示例一的缺点:如果misc.fruits的length刚好为0,一对空的ul标签仍会输出到最终的HTML文件中。

示例二避免了这个问题。如果 misc.fruits的length为0,list中的所有内容都将被跳过,因此ul标签不会出现在最终的HTML文件中。

如果只是想使用某个符号将sequence 中的各项数据分隔开显示,可以使用<#sep>指令。

示例三:

<#--Template-->
<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, </#list>
<p>Fruits: orange, banana

可是!如果这里的 misc.fruits 的 length 又为0呢??

一个<#list>指令就像一个<#if>指令,可以有一个<#else>指令,当 sequence 的length为0时执行。

示例四:

<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, <#else>None</#list>

事实上,示例四有个更简单的写法:

<p>Fruits: ${fruits?join(", ", "None")}

3.2.3 include指令

使用include指令,您可以将另一个文件的内容插入到模板中。

示例:

<#include "/copyright_footer.html">

3.3 指令的混合使用

各种指令可以混合使用

3.4 使用内置函数(bulit-ins)

所谓的内置函数就像子变量(或者说,类似Java中的方法)不是来自data-model,而是由FreeMarker添加到value。

为了区分一个子变量是来自FreeMarker而不是来自data-model,我们访问这个子变量时需要使用问号(?)来替代点(.)。

以下是一些使用例子:

  • user?upper_case:返回全部大写的user值
  • animal.name?cap_first:返回首字母大写的animal.name值
  • user?length:返回user值(一个字符串)中字符个数
  • animals?size:返回animals序列的长度
  • 如果你在<#list animals as animal>和相应的<#list>之间:
    • animal?index:返回当前animal的index值(从0开始)
    • animal?counter:类似于index值,但是下标从1开始
    • animal?item_parity:给出字符串“odd”或“even”,这取决于当前的计数器奇偶校验。这通常用于用交替颜色着色行,如:<td class="${animal?item_parity}Row">

      一些内置函数需要更多的参数来指定行为,如:
  • animal.protected?string("Y", "N") return the string "Y" or "N" depending on the boolean value of animal.protected.
  • animal?item_cycle('lightRow', 'darkRow') is the more generic variant of item_parity from earlier.
  • fruits?join(", "): converts the list to a string by concatenating items, and inserting the parameter separator between each items (like "orange, banana")
  • user?starts_with("J") gives boolean true of false depending on if user starts with the letter "J" or not.

    如果需要反复调用更多内置函数,可以使用链接语法。如:<p>${fruits?join(", ")?cap_first}</p>(fruits=["apple","banana"])的HTML输出为<p>Apple, banana</p>

3.5 处理缺失的变量

FreeMarker不允许引用缺少的变量,除非你明确指出如果变量丢失了该怎么办。

注意:在FreeMarker中,缺失的变量和值为null的变量是一样的。

两种典型的处理:

示例一:

<h1>Welcome ${user!"visitor"}!</h1>

紧跟在感叹号(!)后面的值为默认值,当且仅当user变量值为null或丢失时,将user变量值显示为"visitor"。

示例二:

<#if user??><h1>Welcome ${user}!</h1></#if>

配合if指令使用,当user变量值为null或丢失时跳过这部分内容。

注意:使用(animals.python.price)!0来替代animals.python.price!0,以防 animals 或 animals.python 的值为null或丢失。前者可以在 animals 或 animals.python 或 animals.python.price 为null或丢失时显示0,而后者只能在animals.python.price为null或丢失时显示0(animals 或 animals.python 为null或丢失时报错“undefined variable”)。同理地,使用(animals.python.price)??替代animals.python.price??

3.6 转义为HTML,XML和其他标记(特殊符号)

如果做了合适的配置(配置由programmers修改),FreeMarker可以将${...}中的值自动转义。

推荐的做法:使用ftlh文件来激活HTML的自动转义,使用ftlx来激活XML的自动转义。

如果ftl文档目前没有自动转义且不能修改配置,则可以在ftl文档首行加入下面这行代码来实现自动转义:

<#--生成HTML文件-->
<#ftl output_format="HTML">
<#--生成XML文件-->
<#ftl output_format="HTML">

如果要打印的字符串值是故意包含特殊符号,需要这样禁止自动转义:${value?no_esc}

【翻译】FreeMarker——入门的更多相关文章

  1. Freemarker入门案例

    Freemarker入门案例 首先需要到freemarker官方下载freemarker的jar包,导入到项目中,如:freemarker-2.3.19.jar 1.先建个freemarker的工具类 ...

  2. freemarker入门实例

    freemarker入门实例 1.设计思路 (1)新建Maven Project (2)生成freemarker模板 (3)写freemarker页面ftl文件 (4)写测试文件 2.新建Maven ...

  3. 网页静态化技术--Freemarker入门

    网页静态化技术:为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说,至少几百万个商品,每个商品又 ...

  4. Freemarker入门

    Freemarker入门 工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId& ...

  5. Freemarker入门Demo

    1:工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...

  6. freeMarker入门示例

    1.创建Web项目freeMarkerDemo. 2.添加jar包---freemarker-2.3.9.jar. 3.在WebContent目录下新建templates文件夹,用于放置模板文件ftl ...

  7. freemarker入门实例与源码研究准备工作

    首先去freemarker官网下载源码jar包,本文是基于freemarker-2.3.21.tar.gz进行研究的.解压源码包,找到freemarker的源码部分导入eclipse工程中.需要注意的 ...

  8. [翻译] Autofac 入门文档

    原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: 用控制反转(IoC)的思想 ...

  9. [翻译]lpeg入门教程

    原文地址:http://lua-users.org/wiki/LpegTutorial 简单匹配 LPeg是一个用于文本匹配的有力表达方式,比Lua原生的字符串匹配和标准正则表达式更优异.但是,就像其 ...

随机推荐

  1. php与mysql的常规使用

    <?php header("Content-type:text/html;charset=GBK"); /* 通常,php网页中完成有关数据库的操作,首先,需要如下代码: $ ...

  2. NodeJs中process.cwd()与__dirname的区别

    process.cwd() 是当前执行node命令时候的文件夹地址 ——工作目录,保证了文件在不同的目录下执行时,路径始终不变__dirname 是被执行的js 文件的地址 ——文件所在目录 Node ...

  3. 提问!同一ajax请求获取的图片路劲,在谷歌浏览器能正确展示图片,在火狐浏览器则显示路径undefined

    今天的工作学习之路遇见一个奇葩的问题,作为初级攻城狮的小生实在不知如何解决,都已经壁咚度娘一整天了,都未能解决问题,实属无奈,一开始认为是浏览器兼容的问题,但左看右看,也不是,也尝试过是不是页面加载与 ...

  4. 关于Trie KMP AC自动机

    个人认为trie,KMP,AC自动机是思想非常明确的,AC自动机的性质是与KMP算法的思想类似的(失配后跳转) 而KMP是线性的,AC自动机是在tire树上跑KMP,为方便那些不会用指针的小朋友(我也 ...

  5. 会话管理(Cookie/Session技术)

    什么是会话:用户打开浏览器,点击多个超链接,访问服务器的多个web资源,然后关闭浏览器,整个过程就称为一个会话: 会话过程需要解决的问题:每个用户在使用浏览器与服务器进行会话的过程中,都可能会产生一些 ...

  6. IE6 margin 双倍边距解决方案

    一.什么是双边距Bug? 先来看图: 我们要让绿色盒模型在蓝色盒模型之内向左浮动,并且距蓝色盒模型左侧100像素.这个例子很常见,比如在网页布局中,侧边栏靠左侧内容栏浮动,并且要留出内容栏的宽度.要实 ...

  7. PHP开发人员对JAVA的WEB开发入门(初版-已废弃)

    最近准备对其他部门PHP开发的童鞋做一个对JAVA的培训.知己知彼,百战不殆,我要先了解点PHP,才能确认他们的基础,达到好的授课效果. PHP(原始为Personal Home Page的缩写,后正 ...

  8. sass或scss入门

    1.sass环境搭载: 安装ruby 安装sass 安装compass 配置webstorm 如果只是使用sass的话,就配置sass命名监听就好了 如图: sass目录如下: 如果配置了compas ...

  9. 日历组件的使用,bootstrap-datetimepicker

    官方文档:http://www.bootcss.com/p/bootstrap-datetimepicker/ .html <input name="createdTimeEnd&qu ...

  10. Java内存泄露实例