close

Filter

loading table of contents...

Frontend Developer Manual / Version 2406.0

Table Of Contents

4.8 Templates

Dynamic templating (see Section 5.4.10, “Dynamic Templating” in Blueprint Developer Manual ) 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 Content Application Developer Manual for more details about templates.

Templates Naming and Lookup

The view dispatcher of the CAE (see the Content Application Developer Manual for more details) selects the appropriate view template for a content bean according to the following data:

  1. 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 bean CMExample.

  2. 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="asContainer"/>.

    Example: The template CMExample.specialView.ftl is a special view for the content bean CMExample.

  3. A specific view variant

    A view variant is used, when the look of a rendered view should be editable in the content (see Section 5.4.7, “View Types” in Blueprint Developer Manual for details).

    Example: The template CMExample.[differentLayout].ftl is a special view of the content bean CMExample. 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 templates is enabled to prevent output that allows cross-site scripting (XSS) attacks. The default output format for all templates is set to HTML. See FreeMarker online documentation for details.

In special cases, it might be necessary to disable escaping. For this purpose, FreeMarker provides the directive <#noautoesc/> or built-in for Strings ?no_esc.

Caution

Caution

Note that disabling HTML escaping can lead to cross-site scripting (XSS) vulnerabilities if a templates outputs unchecked data like user input that may contain scripts.

Robustness of Templates

In order to make sure that the rendering of templates does not fail you have to ensure that FTL templates 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 exist"}

<#list existingPossibleNullList![] as item>...</#list>
    

Example 4.16. 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 differs between FreeMarker and 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 of id

  • 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 4.17. Difference between JSP and FreeMarker type-hinting comment


Caution

Caution

Code completion only works out-of-the-box when using the CoreMedia Blueprint workspace. In addition to this you need to enable the Maven profile code-completion in your IDE.

Passing Parameters

In JSP files, it was necessary to wrap arguments passed to taglibs or other functionality into quotes and to print 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 4.18. Passing parameters


Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.