[文章内容皆来源于网络 By Jeroen van Bergen
Template engines can be applied to a wide variety of development scenarios where you want to generate text based on specific processing rules, and are often used as the view component in MVC applications. In this article, Jeroen van Bergen explains where template engines fit into your application architecture and shows you some of the operations common to all template engines. Finally, he compares the two leading Java template engines, Velocity and FreeMarker, in terms of power, ease of use, and performance.
模版引擎在开发中运用很广,当你想要基于特定的处理规则来生成一些文本,或者当你想要开发MVC的视图组件的时候,你都可以用到它。在这篇文章中Jeroen van Bergen解释了在你的应用的架构中的何处安放模版引擎,并且还告诉你一些所有模版引擎都通用的操作。最后,他从功能、易用程度、和表现三个方面比较了两个领先的Java模版引擎:Velocity 以及 freemarker.
While Velocity has been the leading template engine for a long time, FreeMarker has begun to overtake it, offering a speed and sophistication you won't always find in Velocity.
虽然 Velocity 在很长一段时间以来都是领先的模版引擎,但FreeMarker已渐渐又取代它的趋势,因为它有着Velocity没有的速度和精巧。
This discussion is primarily intended for readers new to template engines who want to learn what they're used for and get some help choosing between the two leading open source options.
这些讨论主要是针对那些刚接触模版引擎的读者,他们可能想要知道模版引擎是用来干嘛的或者是想要在这两种开源工具的选择上获得帮助。
What is a template engine?
A template engine is a component that takes fixed text and data as input, integrates these following certain processing rules, and outputs a text document containing the data. Template engines are very useful for tasks such as creating dynamic Web pages, documents, and e-mails, and can also be used to generate source code.
模版引擎是一个组件,它可以把固定的文本和数据作为输入,结合一下特定的处理规则,输出包含这些数据的文本。模版引擎对于一些任务非常有用,比如说创建一些动态的网络页面,文件和e-mail,当然它也可以用来生成一些源码。
Template engines are used mostly as the view component in an MVC architecture. The template engine encourages good separation between the actual view logic and the rendered view, which is considered a Good Thing. Using a template engine involves some overhead in building the application, but most developers see a quick return on investment when the application has to be changed. The separation of view from the logic makes it easy to update the look and feel of the application by adjusting the templates, rather than having to re-write the source code. The relationship is similar to that of CSS and HTML, where a change in style requires a small edit to the CSS file, rather than many revisions to the HTML.
模版引擎通常是被用做MVC架构中的视图组件。模版引擎鼓励将实际视图逻辑和渲染的视图作良好的分离,这是件好事。运用模版引擎会在构建应用时带来间接费用,但当应用必须改变的时候,大多数的开发者会立刻看到投资的回报。视图和逻辑的分离让更新应用的外观变的简单,只需要调整模版,而不需要重写源代码,就能达到效果。这个关系就类似于CSS 和 HTML, 只要在CSS文件中做微小改变就能变幻样式,而不用再去对HTML做大幅修改。
Overview of open source Java template engines
As is often the case when developing on the Java platform, you have many Java-based template engines to choose from. I have tried to compile a comprehensive list of all Java template engines that are currently available. Some of these arereal template engines in the sense that they have not been developed with a certain kind of application in mind. Some are specialized engines that aim to do a specific kind of document creation, such as decorating Web pages. If a template engine is not meant for general-purpose use you'll see an explicit description of the type of application it's intended for.
在Java 平台上做开发,会有很多基于Java的模型引擎供你选择。我努力去编译一系列现存的Java模型引擎。有一些引擎就是纯引擎,没有为任何特定的应用而服务,而有一些就针对特定的文件生成,比如说装饰性web页面。如果某个并不是通用的,你会看到它的应用类型的具体描述