Magnolia 5.4 reached end of life on November 15, 2018. This branch is no longer supported, see End-of-life policy.
When using a templating function you pass some parameters and get something back. The types of these arguments and returned objects are the same in many methods, and it is helpful to know these types. Read the Javadoc of typical templating function return types even if you do not know Java. The Javadoc gives you an idea what else you could do with the returned objects.
Node
and ContentMap
Some functions require a Node, whereas others ask for
The type from the JCR API. Since JCR is a tree oriented data structure, this type is useful when you want to find other nodes above (parent, ancestors) or below (children) the current node.
This Magnolia type is a wrapper for the above mentioned JCR node. ContentMap provides easy access to the node properties and is useful when you want to render these properties.
<h1>${content.title}</h1>
content
provides the current content node of the context as info.magnolia.jcr.util.ContentMap
. model.node
provides the current content node of the context as javax.jcr.Node
, see model. If you have no model but need this type, use cmsfn(content)
.cmsfn
provides methods to convert
Both types java.util.List and java.util.Collection either with Node or with ContentMap items might be required as arguments.
java.util.List<info.magnolia.jcr.util.ContentMap>
java.util.List<javax.jcr.Node>
java.util.Collection<javax.jcr.Node>
java.util.Collection<info.magnolia.jcr.util.ContentMap>
Keep in mind that List
extends Collection
. This means that if a Collection is required, you can pass a List, but this does not work the other way round.
cmsfn
provides methods to convert Collection<Node> to List<ContentMap> and from Collection<ContentMap> to List<Node>.
Example: Get the children nodes of the node of the current nodes, convert the returned list of nodes into list of contentMap items and iterate over the list of contentMap items.
[#assign currentNode = cmsfn.asJCRNode(content)] [#assign childrenNodesAsContentMaps = cmsfn.asContentMapList(cmsfn.children(currentNode, "mgnl:page"))] [#list childrenNodesAsContentMaps as child ] <li>${child.title}</li> [/#list]
Item
, Asset
, Folder
, Map
(of an asset), AssetRendition
Some methods in damfn
require a String argument called itemKey
. An item key uniquely identifies an asset among different asset providers.
When JCR is used as asset provider, the item key is composed of jcr:<node-UUID>
, for instance jcr:20d6e4e3-fe53-4b23-8d64-6e67a1c1667f
. Typically you need to get the item key from a content node that is created when a component is saved in the JCR.
Example: A component node contains a reference to an image
Node name | Value |
---|---|
00 |
|
text_files |
|
image | jcr:20d6e4e3-fe53-4b23-8d64-6e67a1c1667f |
imageLocation | left |
subtitle | quam Occidental in |
text | <p>It va esser tam simplic quam Occidental</p> |
In a Java context, if you have an asset and want to know its item key as a String, use:
asset.getItemKey().asString()
In a FreeMarker context, if you have an asset and want to know its item key as a String, use:
[#assign itemKey = myAsset.getItemKey().asString()]
An asset is a digital resource with associated metadata. This interface provides many methods that you may want to call directly within your script.
Example:
[#assign myAsset = damfn.getAsset("jcr", "/photos/dilbert.jpg")!] get the link of an asset to display an image <img src="${myAsset.getLink()}"/> - when the asset is an image or a download link: <a href="${myAsset.getLink()}">Link to the asset</a>
A Folder represents a structural item holding Assets. Depending on the provider, this can be directly mapped to the concept of folders/directories (JCR, FileSystems, and so on); for others this might map to the concept of albums, playlists, sets, and so on.
Example:
[#assign photosFolder = damfn.getFolder("jcr", "/photos")] The folder ${photosFolder.name} contains the following images: <ul> [#list photosFolder.getChildren() as item] <li>${item.getName()}</li> [/#list] </ul>
The DAM templating functions (damfn) provide a method which returns a java.util.Map
containing all the bean properties of the asset.
[#assign myAsset = damfn.getAsset("jcr", "photos/pool.jpg")!] [#assign myAssetMap = damfn.getAssetMap(myAsset)] fileSize: ${myAssetMap.fileSize} folder: ${myAssetMap.caption?string} name: ${myAssetMap.name} title: ${myAssetMap.title} caption: ${myAssetMap.caption} copyright: ${myAssetMap.copyright}
(See
Additionally, the map may contain further nested maps with the properties of classes implementing
metadata
.If the asset is provided by the
dam
), the following submaps are provided on the asset map:Metadata class | Key | Composed key | Example |
---|---|---|---|
info.magnolia.dam.api.metadata.DublinCore | dc |
|
|
| mgnl | metadata.mgnl |
|
[#assign myAsset = damfn.getAsset("jcr", "photos/pool.jpg")!] [#assign myAssetMap = damfn.getAssetMap(myAsset)] width: ${myAssetMap.metadata.mgnl.width} format: ${myAssetMap.metadata.dc.format}
An
The renditions are defined per Theme in the Site. For instance the node /modules/site/config/themes/examples-theme/imaging/variations/small-square
defines in the theme "examples-theme" a rendition named "small-square".
Site
and Theme
The
Example:
[#assign mySite=sitefn.site()]
Example:
[#assign myTheme=sitefn.theme(sitefn.site())]
TemplateDefinition
Read Template definition to get an idea what a template definition is. Some templating functions require a
At least the template definition of the current template is always available as context object - see rendering context object def
.
If you know the type of the returned object of a templating function, you can apply the public methods to the object to retrieve the return value of these methods. This works in almost every case but not for maps such as
Example:
[#assign currentNode = cmsfn.asJCRNode(content)] <i>The path of the underlying jcr node in this context is ${currentNode.getPath()}.</i>
Notes:
currentNode
.getPath()
method which is a public method defined in javax.jcr.Node. It returns a String.The script will print for instance: The path of the underlying jcr node in this context is /home/products.