Page History
...
FreemarkerRenderer和
JspRenderer
在Configuration配置应用的/modules/rendering/renderers/freemarker
和/jsp
里配置。
...
STK定义它自己的绘制器STKRenderer
,在Configuration配置应用的/modules/standard-templating kit/rendering/renderers/stk
里配置。
...
- 网络应用程序的文件系统。您可以将脚本文件放在
/<CATALINA_HOME>/webapps/<contextPath>/templates
文件夹。 templates
工作区。模板需被使能才能被考虑进去。- 网络应用程序的类路径(classpath)。这是STK模板默认存放的地方。最佳实践是在模块JAR里打包脚本。模块JAR在类路径里好让系统找得到它。
模板加载器
FreeMarker模板加载器在Configuration配置应用的/server/rendering/freemarker/jcr
和/webapp
里配置:
...
开发时,在内容库里编辑脚本有时候很有用。这种方法只对FreeMarker脚本有效。如果您将脚本导入templates
工作区,它们就会在STK > Templates模板里可见可用。所有的STK脚本都遵循这个模式。您可以从以上的加载顺序看出,在templates
工作区里的脚本会覆写从类路径里加载的脚本。一旦被添加或修改,则需要选中Enable template使能模板复选框来强迫Magnolia CMS从内容库中加载模板。
...
STK模板脚本在网络应用程序的类路径中。其副本在STK > Templates模板里就可以编辑。STK脚本是用FreeMarker写的。
...
宏
宏指令使您复用FreeMarker代码片段。参考Templating Kit STK > Templates模板 /templating-kit/components/macros
里STK使用的例子。
...
对FreeMarker来说,这些指令都是由Directives类来完成的。此类可在Configuration配置应用的modules/rendering/renderers/freemarker/contextAttributes/cms/componentClass
里配置。
...
页面上的结果是一个区域栏,一个开始标记和一个结束标记。区域定义里title
属性值在此区域栏里绘制。当编辑者点击New Component框的添加图标时,组件就添加到区域里了。属性值在此区域栏里绘制。当编辑者点击新建组件框的添加图标时,组件就添加到区域里了。
如果区域定义包含一个templateScript
属性,那么由被引用的脚本绘制区域。如果没给定脚本,以下缺省脚本则会被使用:
...
- 像通常一样写作和编译您的Java类。
- 拷贝这个类文件到您的Magnolia网络应用的
WEB-INF/classes
文件夹。 - 转到Configuration配置应用
/modules/rendering/renderers/freemarker/contextAttributes
。 - 在
/contextAttributes
下,创建一个内容节点,如myClass
。用您类的用途命名它。 - 在
myClass
里,创建两个数据节点: componentClass
,设置其值为您放在WEB-INF/classes
里的类的完全相称类名。name
,设置其值为myClass
。
...
Code Block |
---|
[#assign text = stkfn.abbreviateString(text, 80)] |
每个绘制器的模板功能类在Configuration配置应用的/<module>/rendering/renderers/<renderer>/contextAttributes/<tag library>
下配置。注意<tag library>
内容节点和name
数据节点的值与脚本里呈现给方法的句法相匹配。
...
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内建函数FreeMarker内建函数是缀在表达式后的
?
后面的文字,提供有用的功能。 - Freemarker指令FreeMarker指令是可被同化为JSP标签符的代码的
[#blocks]
部分。