Getting the path of the current content in Magnolia CMS

Getting the path of the current content in Magnolia CMS

It sometimes may be helpful to get the path of the node currently being rendered by Magnolia. To do so, depending on where you are, there are the following methods:

Inside a Freemarker template

<!-- given the content (info.magnolia.cms.core.Content) -->
[#assign path = content.@handle!]

<!-- given the aggregation state (info.magnolia.cms.core.AggregationState) -->
[#assign path = state.currentContent.@handle!]

<!-- given nothing we can use the class info.magnolia.context.MgnlContext -->
[#assign path = ctx.aggregationState.currentContent.@handle!]

Inside a Java class

//given the content (info.magnolia.cms.core.Content)
String path = content.getHandle();

//given the aggregation state (info.magnolia.cms.core.AggregationState)
String path = state.getCurrentContent().getHandle();

//given nothing we can use the class info.magnolia.context.MgnlContext
String path = MgnlContext.getAggregationState().getCurrentContent().getHandle();