navfn
navfn
templating functions allow you to create navigation for your
site. There are methods to access standard pages and also content stored
in apps and rendered on virtual pages. The navfn
templating function
library is included in the MTE
module.
Accessing ancestors
Getting the root page
Returns the highest ancestor page of type mgnl:page for the provided content - which is the root page.
Method signature
ContentMap
rootPage(ContentMap content)
Usage
navfn.rootPage(content)
[#assign navigationRootPage = navfn.rootPage(content)!]
Returns the ancestor of the provided content at the specified depth if the ancestor is of type mgnl:page.
Method signature
ContentMap
ancestorPageAtLevel(ContentMap content, int depth)
Argument
Argument | Description |
---|---|
|
required The node whose ancestors you want to get. |
|
required The page depth. An ancestor of depth x is the page that is x levels down along the path from the root page to this page. e.g. depth == 1 would return the root page of this page, depth == 2 would return the child page of the root page to this page, etc. |
Accessing children
Getting child pages of a page
Returns the list of child nodes of type mgnl:page which aren’t hidden in navigation of the the provided page as a list of content maps.
Usage
navfn.navItems(page)
<!-- render links to all pages on "level 1" -->
[#assign navParentItem = navfn.rootPage(content)!]
[#if navParentItem??]
[#assign navItems = navfn.navItems(navParentItem)]
[#list navItems as navItem]
<a href="${cmsfn.link(navItem)!}">${navItem.navigationTitle!navItem.title!navItem.@name}</a> |
[/#list]
[/#if]
Getting child nodes by workspace, path and node type
Returns the list of child nodes with specific node type which aren’t hidden in navigation from a defined parent as a list of content maps. The workspace and parent path define the location of the parent. Use this method to render navigations for content stored in content apps on workspaces distinct from website.
Method signature
List<``ContentMap
> navItemsFromApp(String workspace, String parentPath, String nodeType)
Checking templates
Checking for a specified template
Checks whether the given page has the specified template.
Checking for a specified template type
Checks whether the given page has the specified template type.
Accessing URLs
Getting a page URL with a selector
Returns a page URL with a selector (delimited by the ~ [tilde]
character) identifying the content to be rendered. This relies on
Magnolia’s
selector
mechanism, for example
https://demopublic.magnolia-cms.com/tour-typebeach.html
Getting a page URL with a parameter
Returns a page url with a parameter identifying the content to be
rendered.
A link of this type is produced
http://mysite/mypage.html?parameterName=mycontent
where `mypage' is
the node name of the target page, `mycontent' the node name of the
content.
Method signature
String linkWithParameter(ContentMap page, ContentMap content)
String linkWithParameter(ContentMap page, ContentMap content, String parameterName)
Argument
Argument | Description |
---|---|
|
required The page whose URL you want to get. |
|
required The content from the content app. For example contact, tour, etc. |
|
optional, default is <workspace-name> The parameter name. |
Usage
navfn.linkWithParameter(page, content)
<a href="${navfn.linkWithParameter(page, navContentItem)!"#"}">${navContentItem.lastName!navContentItem.@name}</a>
navfn.linkWithParameter(page, content, parameterName)
<a href="${navfn.linkWithParameter(page, navContentItem, "contacts")!"#"}">${navContentItem.lastName!navContentItem.@name}</a>
Checking navigation items
Checking for current page
Checks whether navigation item is the currently displayed content.
Checking for ancestor of a nav item
Checks whether navigation item is the ancestor of the current page.
Checking if content should be hidden in navigation
Checks whether the given content should be hidden in navigation. The
hideInNav
property of the given content is used.
Checking if content should not be hidden in navigation
Checks whether the given content should not be hidden in navigation. The
hideInNav
property of the given content is used.
Checking for property values
This is small helper method which checks if a given property is true or false. The method is needed because in a FreeMarker template you don’t know if a property is stored as boolean or a string or if it exists at all.
The method avoids using checks like:
[#if navItem.showInNav?has_content && navItem.showInNav?string == "true"]