Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. main脚本包含一个cms.init指令,嵌入了编写实例上所需的CSS和JavaScript。
  2. cms.area指令调用即将绘制的区域。该指令通过名字来辨别区域,在此例中为指令调用即将绘制的区域。该指令通过名字来辨认区域,在此例中为extras。如果该区域有子区域,您需要一个分开的脚本来调用要绘制的子区域。然后,如果该区域只包含组件,那么您不需要区域脚本。。如果该区域有子区域,您需要一个单独的脚本来调用要绘制的子区域。然而,如果该区域只包含组件,那么您不需要区域脚本。
  3. 页面的脚注同样使用页面的页脚同样使用cms.area指令来绘制。该区域不含子区域,只有组件。

添加您自己的指令

您可以添加自己的指令。以下步骤使您类中的Java方法和功能对模板脚本可用:您可以添加自己的指令。执行以下步骤,可以使您自己的类中的Java方法和功能为模板脚本所用:

  1. 像通常一样写作和编译您的Java类。
  2. 拷贝这个类文件到您的Magnolia网络应用的拷贝这个类文件到您的Magnolia Web应用程序的WEB-INF/classes文件夹。
  3. 转到配置应用/modules/rendering/renderers/freemarker/contextAttributes
  4. /contextAttributes下,创建一个内容节点,如myClass。用您类的用途命名它。。可以用类的用途命名。
  5. 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
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse

 

 

节点名

modules

 

rendering

 

renderers

 

freemarker

 

contextAttributes

 

cms

 

cmsfn

 

Paragraph

componentClass

info.magnolia.templating.functions.TemplatingFunctions

Paragraph

name

cmsfn

Paragraph

class

info.magnolia.rendering.renderer.FreemarkerRenderer

Paragraph

type

freemarker

jsp

 

listeners

 

Paragraph

version

5.0.2

standard-templating-kit

 

renderers

 

stk

 

contextAttributes

 

cms

 

cmsfn

 

stkfn

 

Paragraph

componentClass

info.magnolia.module.templatingkit.functions.STKTemplatingFunctions

Paragraph

name

stkfn

damfn

 

Paragraph

class

info.magnolia.module.templatingkit.renderers.STKRenderer

 

 

FreeMarker内建函数

FreeMarker提供一套强大的内建函数。这些被用作基本数据操作,不需要任何Java代码。内建函数在使用时前面加一个“。它们可用作基本数据操作,不需要任何Java代码。内建函数在使用时前面加一个“?”字符。例如, ?exists用于检查一个值或对象是否存在,?has_content检查一个值或对象是否存在且为空。

...

大多数Java字符串在FreeMarker里都能实现并直接使用。例如:substringuncap_firstcapitalizedate,time,datetimeends_withhtmlindex_oflast_index_oflengthlower_caseupper_casecontainsreplacestarts_withtrim

布尔值

字符串(使用了布尔值的)将一个布尔值转化为一个字符串。您可以采用以下两种方法使用它:使用了布尔值的字符串将一个布尔值转化为一个字符串。您可以通过以下两种方法使用它:

  • foo?string使用缺省字符来代表使用默认字符来代表truefalse值来将布尔值转化为字符串。值,将布尔值转化为字符串。
  • 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初学者有帮助: