Versions Compared

Key

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

...

Magnolia CMS为FreeMarker和JSP提供可立即使用的绘制器。您可以选择偏好的语言,甚至可以在相同的网站上混合使用,在一些模板里使用FreeMarker而另一些里用JSP。 

...

FreeMarker

在Magnolia里,我们更倾向于使用Freemarker,因其灵活性,较清晰的语法,更好的错误报告机制,也因为它不依赖于文件系统。模板不需要导出到文件系统,这意味着您可以将它们存储在内容库中,并像其他资源一样获得,随意添加版本控制,以及添加定制的属性和元数据。在Magnolia里,我们更倾向于使用FreeMarker,因其灵活性,较清晰的语法,更好的错误报告机制,也因为它不依赖于文件系统。模板不需要导出到文件系统,这意味着您可以将它们存储在内容库中,并像其他资源一样获得,随意添加版本控制,以及添加定制的属性和元数据。

样例:绘制页面标题,如果没有标题则绘制名字。

Code Block
<h3>${content.title!content.@name}</h3>

这是Freemarker提供的一些好处:这是FreeMarker提供的一些好处:

  • 它是一种通用的模板语言,不束缚于Java环境。
  • 提供基本的指令,如[if][else]和[list]
  • 有一大套built-ins
  • 可以调用任何Java对象和返回对象的公共方法。
  • 不需要在请求/回复环境中绘制。
  • 提供直接获取节点对象(beans)的方法。
  • 可直接迭代任何扩展一个Java列表的内容集合。
  • 严格的null值处理导致稳定的模板。
  • 需要的话允许使用JSP标签库。

...

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
节点名

modules

 

rendering

 

renderers

 

freemarker

 

contextAttributes

 

Paragraph

class

info.magnolia.rendering.renderer.FreemarkerRenderer

Paragraph

type

freemarker

jsp

 

contextAttributes

 

Paragraph

class

 info.magnolia.rendering.renderer.JspRenderer

Paragraph

type

 jsp

The STK defines its own renderer STKRenderer configured in the Configuration app > STK定义它自己的绘制器STKRenderer,在Configuration应用 > /modules/standard-templating kit/rendering/renderers/stk.里配置。

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node name节点名Value

modules

 

standard-templating-kit

 

renderers

 

stk

 

contextAttributes

 

Paragraph

class

info.magnolia.module.templatingkit.renderers.STKRenderer

The renderType property in pagearea and component definitions defines the template renderer to use. For STK templates the type is stk.

Loading scripts

The system can load Freemarker scripts from three places in this order:

页面区域组件定义里的renderType属性定义使用的模板绘制器。对STK模板来说,类型是stk

加载脚本

系统可以从三个地方加载FreeMarker脚本,以下列顺序:

  1. 网络应用程序的文件系统。您可以将脚本文件放在/<CATALINA_Filesystem of the webapp. You can place a script file in /<CATALINA_HOME>/webapps/<contextPath>/templates folder.文件夹。
  2. templates workspace. The template needs to be enabled to be considered.
  3. Web application's classpath. This is where default STK templates are located. Best practice is to package scripts inside the module JAR. A module JAR resides in the classpath so the system finds it.

Template loaders

  1.  工作区。模板需被使能才能被考虑进去。
  2. 网络应用程序的类路径(classpath)。这是STK模板默认存放的地方。最佳实践是在模块JAR里打包脚本。模块JAR在类路径里好让系统找得到它。

模板加载器

FreeMarker模板加载器在Configuration应用的Freemarker template loaders are configured in the Configuration app > /server/rendering/freemarker/jcr and /webapp:

里配置:

需要的话您也可以写下您自己定制的模板加载器。You can also write your own custom template loader if needed.

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node name节点名Value

server

 

filters

 

IPConfig

 

i18n

 

security

 

rendering

 

freemarker

 

templateLoaders

 

jcr

 

Paragraph

class

info.magnolia.module.inplacetemplating.JcrRepoTemplateLoader

Paragraph

extension

.ftl

Paragraph

workspace

templates

webapp

 

Paragraph

class

 info.magnolia.freemarker.loaders.LazyWebappTemplateLoader
Tip

JSP scripts can only reside on the file system because they require pre-compiling by the server before they can be rendered. JSP scripts are extracted onto the file system during module installation or update

Editing scripts

JSP脚本只能存在于文件系统中,因为它们在被绘制前需要服务器预编译。JSP脚本在模块安装或更新时被解压到文件系统。

编辑脚本

Panel
bgColor#ffffff
titleBGColor#ffffff
titleBest practice最佳实践
Multiexcerpt
MultiExcerptNamestoring scripts

The best practice is to store scripts in your project module. This way the scripts can be stored in a version control system and are automatically part of a controlled software development lifecycle. You can edit them on the file system in your favorite editor that supports syntax highlighting and other helpful features.

...