Dynamic templating (see Section 6.3.10, “Dynamic Templating”) requires the usage of Freemarker,
not JSP, templates. Freemarker templates are imported as JAR files into a blob property of
content of type Template Set
. See CoreMedia Content Application Developer Manual
for more details about templates.
Templates naming and lookup
The view dispatcher of the CAE (see the CoreMedia Content Application Developer Manual for more details) selects the appropriate view template for a content bean according to the following data:
Name of the content bean
The view dispatcher looks for a template whose name starts with the name of the content bean.
Example: The template
CMExample.ftl
is a detail view for the content beanCMExample
.A specific view name
A view name specifies a special view for a content bean. The view is added as a parameter when you include a template in another template via
<cm.include self=self view="asPlacement"/>
.Example: The template
CMExample.specialView.ftl
is a special view for the content beanCMExample
.A specific view variant
A view variant is used, when the look of a rendered view should be editable in the content (see Section 6.3.7, “View Types” for details).
Example: The template
CMExample.[differentLayout].ftl
is a special view of the content beanCMExample
. The view variant must be enclosed in square brackets.
The template name is always in the order content bean name, view name, view variant. The view dispatcher looks for the most specific template.
Freemarker
Escaping HTML output
In CoreMedia Blueprint escaping of output is enabled by default. Auto-escaped are all values printed
with ${value}
. However, there are cases were you need to disable escaping, for
example, when you get HTML code that you want to print as HTML, not escaped HTML.
When you really need to disable auto-escaping (not recommended) you can use the cm.unescape plugin.
Robustness of templates
In order to make sure that the rendering of templates does not fail you have to ensure that FTL template can be rendered although some information is not provided. In order to achieve this FreeMarker adds some functionality to detect if a variable is set and if it contains content.
If you want to
check for existence and emptiness of a hash/variable (null is also considered as empty) you need to use
?has_content
.
If you want to declare a default value for an attribute that could be null or empty use !
followed by the value to be taken if the variable/hash is null.
Example:
${existingPossibleNullVariable!"Does not exists"} <#list existingPossibleNullList![] as item>...</#list>
Example 6.12. Example of a fallback in Freemarker
Freemarker for JSP Developers
As a JSP developer you are familiar with JSPs in general and with writing CAE templates with JSPs. In this section, you will learn about important differences.
Type-Hinting
Type-hinting in JSP or Freemarker templates helps IntelliJ Idea to offer you code completion and to make the templates "green". The syntax of the required comments is different in Freemarker than in JSP:
Comments are marked with
<#-- comment -->
instead of<%-- comment --%>
The annotation is called
@ftlvariable
instead of@elvariable
The attribute that names the typ-hinted object is called
name
instead ofid
The comment must have a single space after the opening comment tag
JSP: <%--@elvariable id="self" type="com.coremedia.blueprint.MyClass"--%> Freemarker: <#-- @ftlvariable name="self" type="com.coremedia.blueprint.MyClass" -->
Example 6.13. Difference between JSP and Freemarker type-hinting comment
Passing Parameters
In JSP files, it was necessary to wrap arguments passed to taglibs or other functionality into
quotes and to give them out via ${]
. In Freemarker, this is no longer necessary.
JSP: <mytaglib:functionality name="${name}" booleansetting=true /> Freemarker: <mymacro.functionality name=name booleansetting=true />
Example 6.14. Passing parameters