close

Filter

loading table of contents...

Frontend Developer Manual / Version 2404

Table Of Contents

4.9 Sharing FreeMarker Functionality

FreeMarker templates can be shared among other packages in the Frontend Workspace to reuse functionality. In most cases you may want to utilize FreeMarker functions or macros to define a functionality so that it can be imported by another template using the import directive. These templates will be referred to with the term FreeMarker Library.

Location of FreeMarker Libraries

Shared templates should be located in src/freemarkerLibs of your package to be handled specially by our theme build process. They may have any valid file name but CoreMedia suggests naming them after the functionality or package it is provided by using dash-case to separate words.

Note

Note

Some of the CoreMedia packages provided in the frontend workspace already contain FreeMarker libraries. They are documented in the Section 6.5, “CoreMedia FreeMarker Facade API”.

Importing a FreeMarker Library

Importing a FreeMarker library in the same package it is provided by is straight forward. Assuming you have a FreeMarker library named src/freemarkerLibs/my-lib.ftl which provides a function named "calculateSomething" and a macro called "renderSomething" it can be imported from another template within the same package using the following code:

<#import "../../freemarkerLibs/my-lib.ftl" as myLib />

<#assign result=myLib.calculateSomething() />
<@myLib.renderSomething />
    

Example 4.19. Import from src/templates/com.coremedia.blueprint.common.contentbeans/CMArticle.ftl using relative path


In case you want to reference a FreeMarker Library from another package you first need to add a dependency to the other package in its package.json. Assuming the FreeMarker library of the previous example is in a package named "my-freemarker-lib" the template can then be imported with the following code:

<#import "*/node_modules/other-package/src/freemarkerLibs/my-lib.ftl" as myLib />

<#assign result=myLib.calculateSomething() />
<@myLib.renderSomething />
    

Example 4.20. Import from any other template using acquisition


Note

Note

The acquisition feature of FreeMarker's include and import directives are used here to achieve the same lookup mechanism that Node.js uses. When building a theme these paths are automatically rewritten so they represent the actual location in the JAR file that is uploaded into the blog property of the Template Set (see Section 4.8, “Templates”).

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

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