Page History
...
main
脚本包含一个cms.init
指令,嵌入了编写实例上所需的CSS和JavaScript。cms.area
指令调用即将绘制的区域。该指令通过名字来辨别区域,在此例中为指令调用即将绘制的区域。该指令通过名字来辨认区域,在此例中为extras
。如果该区域有子区域,您需要一个分开的脚本来调用要绘制的子区域。然后,如果该区域只包含组件,那么您不需要区域脚本。。如果该区域有子区域,您需要一个单独的脚本来调用要绘制的子区域。然而,如果该区域只包含组件,那么您不需要区域脚本。- 页面的脚注同样使用页面的页脚同样使用
cms.area
指令来绘制。该区域不含子区域,只有组件。
添加您自己的指令
您可以添加自己的指令。以下步骤使您类中的Java方法和功能对模板脚本可用:您可以添加自己的指令。执行以下步骤,可以使您自己的类中的Java方法和功能为模板脚本所用:
- 像通常一样写作和编译您的Java类。
- 拷贝这个类文件到您的Magnolia网络应用的拷贝这个类文件到您的Magnolia Web应用程序的
WEB-INF/classes
文件夹。 - 转到配置应用
/modules/rendering/renderers/freemarker/contextAttributes
。 - 在
/contextAttributes
下,创建一个内容节点,如myClass
。用您类的用途命名它。。可以用类的用途命名。 - 在
myClass
里,创建两个数据节点: componentClass
,设置其值为您放在,设置值为您放在WEB-INF/classes
里的类的完全相称类名。name
,设置其值为,设置值为myClass
。
这就让您能够使用这样,您就能够使用${myClass.myMethod()
} FreeMarker句法,从模板获得FreeMarker句法从模板获得myClass
里所有的静态方法。里所有的静态方法了。
模板功能
TemplatingFunctions
包含了您可以在模板中使用的有用方法。这些方法表现为包含了您可以在模板中可用的有用方法,这些方法以cmsfn
。出现。例如,decode
方法从属性(properties)上移除HTML换码,就是这样的一个例子,下列代码片段展示了它在方法可以从属性(properties)上移除HTML换码,下列代码片段展示了这个方法在stkTextImage
组件脚本中的使用。
Code Block |
---|
[#if content.text?has_content] ${cmsfn.decode(content).text} [/#if] |
DamTemplatingFunctions
提供到Assets
的直接权限以及定义有用的方法。这些方法表现为的直接权限以及定义有用的方法,这些方法以damfn
。参考出现。更多信息参考DAM模板以获得更多信息。例如:。例如:
Code Block |
---|
[#assign asset = damfn.getAssetForId(content.link)][#assign assetMap = damfn.getAssetMapForAssetId(content.link)] |
STKTemplatingFunctions
使附加的方法在STK模板离都可以使用。这些方法表现为使附加的方法在STK模板里都可以使用,这些方法以stkfn
。以下出现。以下promos
组件脚本片段包含了abbreviateString
示例。
...
每个绘制器的模板功能类在配置应用的/<module>/rendering/renderers/<renderer>/contextAttributes/<tag library>
下配置。注意<tag library>
内容节点和name
数据节点的值与脚本里呈现给方法的句法相匹配。数据的节点值与脚本里呈现方法的句法相匹配。
Advanced Tables - Table Plus | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
FreeMarker内建函数
FreeMarker提供一套强大的内建函数。这些被用作基本数据操作,不需要任何Java代码。内建函数在使用时前面加一个“。它们可用作基本数据操作,不需要任何Java代码。内建函数在使用时前面加一个“?
”字符。例如, ?exists
用于检查一个值或对象是否存在,?has_content
检查一个值或对象是否存在且为空。
...
大多数Java字符串在FreeMarker里都能实现并直接使用。例如: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
。
布尔值
字符串(使用了布尔值的)将一个布尔值转化为一个字符串。您可以采用以下两种方法使用它:使用了布尔值的字符串将一个布尔值转化为一个字符串。您可以通过以下两种方法使用它:
foo?string
使用缺省字符来代表使用默认字符来代表true和false值来将布尔值转化为字符串。值,将布尔值转化为字符串。foo?string(
“yes
”,“no
”)
在布尔值为真时返回第一个参数“yes”,否则返回第二个参数“no”。 在布尔值为真时返回第一个参数“yes”,否则,返回第二个参数“no”。
日期
日期内建函数有多种格式,例如:
Code Block |
---|
[#assign microFormatDate = content.date?string("yyyy-MM-dd") + "T" + ontent.date?string("hh:mm:ss")] |
专家级内建函数
has_content
决定HTML是否被绘制来避免HTML标签符。决定HTML是否被绘制,以避免HTML标签。
Code Block |
---|
[#if content.author?has_content] <p> <cite>${content.author}</cite> </p> [/#if] |
eval
估算经过的FreeMarker代码。估算传递的FreeMarker代码。
Code Block |
---|
[#assign indexString = ('"'+(ctx.indexString!)+'"')?eval] |
...
Code Block |
---|
${state.locale!} |
检查null值
检查null值使您的模板更加稳定。FreeMarker遇到null值使会报告例外。有两种方法来检查:检查null值使您的模板更加稳定,因为FreeMarker遇到null值使会报告例外。检查有两种方法:
使用“第一种方法是,使用“!
”字符来提供缺省值。“”字符来提供缺省值,“!
”后的内容被执行。
下列代码想要从内容里分配例如,下列代码想要从内容里分配title
,如果不成功,它将返回到该内容节点名(name
)。
Code Block |
---|
<meta name="keywords" content="${content.keywords!content.title!content.@name}" /> |
您可以指定以下值。您也可以指定值:
Code Block |
---|
[#if content.keywordsEnabled!false] <meta name="keywords" content="${content.keywords!"These are some keywords"}" /> [/#if] |
或使用第二种方法是使用?has_content
内建函数。下例在有值的情况下在h1
标签符例绘制标签里绘制header
。
Code Block |
---|
[#if content.header?has_content] <h1>${content.header}</h1> [/#if] |
模板样例
这里是最常用在您的模板脚本里的FreeMarker样例:这是最常用在您的模板脚本里的FreeMarker样例:
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} |
参考
对FreeMarker初学者有用的链接:以下链接里的内容对FreeMarker初学者有帮助:
- FreeMarker手册
- FreeMarker创建模板快速入门指南
- FreeMarker内建函数是缀在表达式后的
?
后面的文字,提供有用的功能。 - FreeMarker指令是可被同化为JSP标签符的代码的
[#blocks]
部分。