What is Apache FreeMarker?
Apache FreeMarker is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data. Templates are written in the FreeMarker Template Language (FTL), which is a simple, specialized language (not a full-blown programming language like PHP).
Apache FreeMarker 是一个模版引擎:它是一个Java库,通过模版以及变化的数据来生成文本输出(HTML 网页,电子邮件,configuration文件,源码等等)。模版由 Freemarker 模版语言(FTL)编写,它是一个简单的,专用的语言(并不是PHP那样的成熟的编程语言)。
You meant to prepare the data to display in a real programming language, like issue database queries and do business calculations, and then the template displays that already prepared data. In the template you are focusing on how to present the data, and outside the template you are focusing on what data to present.
在一个真实的编程语言中,你要去准备些供展示的数据,比如发起数据库的请求然后做一些商业的计算,在这之后,模版就展示哪些已经准备好的数据。在模版中,你专注于怎样去展示这些数据,而在模版之外,你专注于你想要展示的这些数据。
This approach is often referred to as the MVC (Model View Controller) pattern, and is particularly popular for dynamic web pages. It helps in separating web page designers (HTML authors) from developers (Java programmers usually). Designers won't face complicated logic in templates, and can change the appearance of a page without programmers having to change or recompile code.
这种方法通常被认为是MVC(Model View Controller)模式,它因为动态网页而相当受欢迎。它帮助分离了网页设计师(HTML作者)和开发者(通常是Java程序员)。设计者不用在模版中面对复杂的逻辑代码,也不用程序员改变或重新编译代码,就可以改变页面的外观。
While FreeMarker was originally created for generating HTML pages in MVC web application frameworks, it isn't bound to servlets or HTML or anything web-related. It's used in non-web application environments as well.
虽然FreeMarker最初的时候是为在MVC网络应用框架生成HTML页面而设计的,它绝不止于servlets或HTML或和网络相关的东西。它也可以运用在无网络应用环境中。
Features
A few highlights of FreeMarker:
FreeMarker 的一些亮点:
-
Powerful template language: Conditional blocks, iterations, assignments, string and arithmetic operations and formatting, macros and functions, including other templates, escaping by default (optional), and many more
- 功能强大的模版语言: 可选择的块,循环,赋值,字符串和算数操作和排版,宏和函数,包含其它模版,可选择性和更多的功能
-
Multipurpose and lightweight: Zero dependencies, any output format, can load templates from any place (pluggable), many configuration options
- 多种功能而轻量级:零依赖,任意的输出格式,可以从任意地方(插件形式)载入模版,许多构造选择
-
Internationalization/localization-aware: Locale sensitive number and date/time formatting, localized template variations.
- 国际化/本土化:场景敏感的数字和日期/时间格式,本土化的模版变化。
-
XML processing capabilities: Drop XML DOM-s into the data-model and traverse them, or even process them declaratively
- 能够处理XML:把XML DOM-s 放入数据模型然后转换他们,或者甚至用说明地方法处理它们。
-
Versatile data-model: Java objects are exposed to the template as a tree of variables through pluggable adapters, which decides how the template sees them.
- 多用途的数据模型: Java 对象通过插件形式的适配器作为变量树展现给模版,这决定了模版怎么看待它们。