catfn

catfn templating functions get content by category. For example, you can retrieve all pages tagged with travel. Categories are typically rendered as links on the website. They help visitors discover related content.

Access categorized content

Categories of node

Returns all category nodes linked to a given node.

Method signature

List<Node> getCategories(Node node)

List<Node> getCategories(Node node, String propertyName)

Arguments

Argument Description

node

required

Node whose categories you want to get.

propertyName

optional, default is categories

Categories with a specified property name.

Returns

List<Node>

A list of category node items.

Usage

catfn.getCategories(node, propertyName)

This page belongs to the categories:
[#assign allCategories = catfn.getCategories(cmsfn.asJCRNode(content))]
[#list cmsfn.asContentMapList(allCategories) as cat ]
  ${cat.name},
[/#list]

Gets all pages with categories related to a given category. Related categories are referenced in the Categories app.

Method signature

Collection<Node> getRelatedCategories(String name)

Arguments

Argument Description

name

required

Category name.

Returns

Collection<Node>

A collection of node items marked with related categories.

Usage

catfn.getRelatedCategories(name)

[#assign myCategory = "biochemistry"]
[#assign relatedCategories = catfn.getRelatedCategories(myCategory)!]
[#if relatedCategories?? && relatedCategories?size>0]
  ${myCategory} has the following related categories:<br/>
  [#list relatedCategories as cat ]
     ${cat.name}
     [#if cat_has_next], [/#if]
   [/#list]
[#else]
  ${myCategory} has no related categories
[/#if]

Content for given category

Gets a collection of content node items for a given category.

This function returns content only from the website workspace. It does not work with other workspaces. If you need to get categorized content from another workspace, write a custom model.

See RelatedToursModel.java for an example.

Method signature

Collection<Node> getContentByCategory (Node node, String name)`

Arguments

Argument Description

node

required

Root node under which you want to look for content (for example, the root page of a site).

name

required

Category name.

Returns

Collection<Node>

A collection of node items assigned the given category.

Usage

catfn.getContentByCategory(node, name)

Aggregating content for a certain category which is applied to page nodes (start node is root node)<br/>
The following pages are tagged with category "sports":
[#assign rootPage = cmsfn.asJCRNode(cmsfn.root(content, "mgnl:page"))!]
[#assign sportsContent = catfn.getContentByCategory(rootPage, "sports")]
[#if sportsContent??]
    [#list sportsContent as catNode ]
        [#if catNode.isNodeType("mgnl:page")]
            [#assign link=cmsfn.link(catNode)]
            [#assign title=cmsfn.asContentMap(catNode).title!catNode.getName()]
            <a href="${link}">${title}</a><br/>
        [/#if]
    [/#list]
[/#if]

Nodes by category identifier

Performs a JCR query call in the website workspace to get all nodes with a given category identifier under a specified page path. The method does not filter for a certain node type. If required, you have to filter for node types when iterating over the list.

Method signature

Collection<Node> getContentByCategoryIdentifier(String sitePath, String identifier)

Arguments

Argument Description

sitePath

required

Page path.

identifier

required

Category identifier.

Returns

Collection<Node>

A collection of node items for the given category identifier and path.

Usage

catfn.getContentByCategoryIdentifier(sitePath, identifier)

[#assign frisbeeCatUuid = "32708be2-6c62-4cca-8eb5-d95e9261011f"]
[#assign categorizedNodes = catfn.getContentByCategoryIdentifier("/", frisbeeCatUuid)]
[#if categorizedNodes?? && categorizedNodes?size>0]
Our website contains some pages about "frisbee":<br/>
[#list categorizedNodes as node ]
    [#if node.isNodeType("mgnl:page")]
        [#assign contentMap = cmsfn.asContentMap(node)]
        ${contentMap.title!node.getName()}<br/>
    [/#if]
[/#list]
[#else]
No pages found related to the category with the uuid "${frisbeeCatUuid}"
[/#if]

Category workspace

Gets the name of the category workspace. When using a search method where you have to provide the workspace name, use this function instead of a hard-coded literal value.

Method signature

String getCategorizationRepository()

Returns

String

Usage

catfn.getCategorizationRepository()

[#assign myCatNode = cmsfn.nodeByPath("/all/sports/frisbee", catfn.getCategorizationRepository())!]

Category node by name

Gets the node representing the category defined by categoryName.

Method signature

Node getCategoryNodeByName(final String categoryName)

Arguments

Argument Description

categoryName

required

Category name.

Returns

Node

Usage

catfn.getCategoryNodeByName(categoryName)

[#assign frisbeeNode = catfn.getCategoryNodeByName("frisbee")!]
[#if frisbeeNode??]The node was created by ${cmsfn.metaData(cmsfn.asContentMap(frisbeeNode), "mgnl:createdBy")!"Mr. X"}[/#if]

Get page based on template type and subtype

Gets a page based on the defined type and subtype properties in the page definition.

You can provide any values for type and subtype. Use this function, for example, to find a custom category page that you have defined yourself.

You can limit the search to a specific site or branch by passing the root page node in the siteRoot argument.

Method signature

Node getContentByTemplateCategorySubCategory(Node siteRoot, String type, String subtype)

Arguments

Argument Description

type

required

Value of the type property in the page definition.

subtype

required

Value of the subtype property in the page definition.

siteRoot

required

Root node under which you want to search for the page.

Returns

Node

A node item matching the given type and subtype.

Usage

catfn.getContentByTemplateCategorySubCategory(siteRoot, type, subtype)

[#assign rootPage = cmsfn.root(content, "mgnl:page")! ]
[#assign siteRoot = cmsfn.asJCRNode(rootPage)! ]

${catfn.getContentByTemplateCategorySubCategory(siteRoot, "feature", "tutorial")!}
Related topics
Feedback

DX Core

×

Location

This widget lets you know where you are on the docs site.

You are currently perusing through the DX Core docs.

Main doc sections

DX Core Headless PaaS Legacy Cloud Incubator modules