Page History
...
Code Block |
---|
[#assign text = stkfn.abbreviateString(text, 80)] |
The 每个绘制器的模板功能类在The templating functions classes are configured for each renderer in the Configuration app > Configuration应用的/<module>/rendering/renderers/<renderer>/contextAttributes/<tag library>
. Note that the 下配置。注意<tag library>
content node and the value of the name
data node matches the syntax used to expose the methods in scripts.内容节点和name
数据节点的值与脚本里呈现给方法的句法相匹配。
Advanced Tables - Table Plus | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Freemarker built-ins
FreeMarker provides a powerful set of built-ins. These are used for basic manipulation of data and no Java code is necessary. Built-ins are used with a preceding ?
character. For example ?exists
checks if a value/object exists and ?has_content
checks if a value/object is empty and exists.
Strings
FreeMarker内建函数
FreeMarker提供一套强大的内建函数。这些被用作基本数据操作,不需要任何Java代码。内建函数在使用时前面加一个“?
”字符。例如, ?exists
用于检查一个值或对象是否存在,?has_content
检查一个值或对象是否存在且为空。
字符串
大多数Java字符串在FreeMarker里都能实现并直接使用。例如:substring
,uncap_first
,capitalize,
Most Java String are implemented and can be used directly in Freemarker. Examples: substring
, uncap_first
, capitalize
, date,time,datetime
, ,
ends_with
, ,
html
, ,index_of
, ,last_index_of
, ,length
, ,lower_case
, ,upper_case
, ,contains
, replace
, ,starts_with
, ,trim
.
Booleans
。
布尔值
字符串(使用了布尔值的)将一个布尔值转化为一个字符串。您可以采用以下两种方法使用它:String (when used with a boolean value) converts a boolean to a string. You can use it in two ways:
foo?string
converts the boolean to string using the default strings to represent true and false values.使用缺省字符来代表true和false值来将布尔值转化为字符串。foo?string("yes", "no")
returns the first parameter "yes" if the boolean is true, otherwise the second parameter "no".
Dates
string(
“yes
”,“no
”)
在布尔值为真时返回第一个参数“yes”,否则返回第二个参数“no”。
日期
日期内建函数有多种格式,例如:There are various built-ins for dates with formating capabilities. For example:
Code Block |
---|
[#assign microFormatDate = content.date?string("yyyy-MM-dd") + "T" + ontent.date?string("hh:mm:ss")] |
Expert
专家级内建函数
还有一些内建函数。最常用的有:There are various expert built-ins. The most commonly used are:
has_content
determines if HTML is rendered to avoid empty HTML tags.决定HTML是否被绘制来避免HTML标签符。
Code Block |
---|
[#if content.author?has_content] <p> <cite>${content.author}</cite> </p> [/#if] |
eval
evaluates the passed Freemarker code.估算经过的FreeMarker代码。
Code Block |
---|
[#assign indexString = ('"'+(ctx.indexString!)+'"')?eval] |
Java objects
These rendering context objects are set in AbstractRenderer
and its child classes,
Java对象
这些绘制上下文对象在AbstractRenderer
及它的子类中设置:
content
:当前的content
节点。content
: the current content
node.
Code Block |
---|
${content.header!} |
model
: The example code below corresponds to :下面的样例代码对应模型类的getNavigation()
method of the model class.方法。
Code Block |
---|
${model.navigation!} |
def
: The current page, area or component definition object.:当前页面,区域或组件定义对象。
Code Block |
---|
${def.headingLevel!} |
ctx
: See :参看WebContext
.。
Code Block |
---|
${ctx.user.name!} |
state
: See :参看AggregationState
.。
Code Block |
---|
${state.locale!} |
Checking for null
Null checks stabilize your templates. Freemarker throws an exception if null is encountered. There are two options:
Use the !
character to provide default values. The content after !
is executed.
检查null值
检查null值使您的模板更加稳定。FreeMarker遇到null值使会报告例外。有两种方法来检查:
使用“!
”字符来提供缺省值。“!
”后的内容被执行。
下列代码想要从内容里分配title
,如果不成功,它将返回到该内容节点名(name
)。This code tries to assign title
from content, if not it falls back to the content node's name
.
Code Block |
---|
<meta name="keywords" content="${content.keywords!content.title!content.@name}" /> |
You can also specify the value.您可以指定以下值。
Code Block |
---|
[#if content.keywordsEnabled!false] <meta name="keywords" content="${content.keywords!"These are some keywords"}" /> [/#if] |
Or use the 或使用?has_content
built-in. The example renders the header
in h1
tags if a value exists.内建函数。下例在有值的情况下在h1
标签符例绘制header
。
Code Block |
---|
[#if content.header?has_content] <h1>${content.header}</h1> [/#if] |
Templating examples
模板样例
这里是最常用在您的模板脚本里的FreeMarker样例:Here are the most common Freemarker examples for use in your template scripts:
Code Block |
---|
[#-- Accessing content --] The value of "someProperty": ${content.someProperty} Accessing a child node: ${content.childNode} Accessing the child node collection: ${content?children} Accessing the parent node: ${content?parent} [#-- Special content properties --] The content object is an instance of ContentMap and the following attributes are available: The current node name: ${content.@name} The current node path: ${content.@path} The current node id: ${content.@id} The current node depth: ${content.@depth} The current node node type: ${content.@nodeType} [#-- MetaData --] The creation date: ${content.metaData.creationDate} Metadata.modificationDate: ${content.metaData.modificationDate!" This node has never been modified."} [#-- Component definition --] The current component definition: ${def.name} A component definition property: ${def.style} [#-- Context: ctx --] A request parmeter: ${ctx.myParam} The current user name ${ctx.user.name} The current locale ${ctx.locale} [#-- TemplatingFunctions: cmsfn--] Create a link to a page: ${cmsfn.link(content)} Create a binary link: ${cmsfn.link(content.image)} [#-- Status based rendering --] This is ${cmsfn.authorInstance?string('indeed', 'not')} an author instance. This is ${cmsfn.editMode?string('indeed', 'not')} the edit mode. This is ${cmsfn.previewMode?string('indeed', 'not')} the preview mode. [#-- The Model executed before the paragraph rendering: model --] The parent model: {model.parent} The result of the execute method: ${actionResult} [#-- AggregationState: state --] Entry point of the rendering: ${state.mainContent} Current content node: ${state.currentContent} |
References
Useful links to get started with Freemarker:
参考
对FreeMarker初学者有用的链接:
- Freemarker手册
- Freemarker创建模板快速入门指南
- Freemarker内建函数是后缀在表达式后的?bits部分,提供有用的功能。
- Freemarker指令是可被同化为JSP标签符的代码的
[#blocks]
部分。 - Freemarker manual.
- Freemarker quickstart guide to templating.
- Freemarker built-ins are the ?bits following expressions and providing useful functionality.
- Freemarker directives are
[#blocks]
of code which can be assimilated to jsp tags.