April 8, 2020
Estimated Post Reading Time ~

Creating Page with boilerplate or default nodes

When you have a need to create default/boilerplate nodes on every page creation – for instance lets say every page needs to have a disclaimer text component node with some text in it, then the standard approach of <cq:include>..</cq:include> of the text component in the template jsp will not work if you have any of the following additional requirements
a) component holding boilerplate content which needs to be modified in a specific page
b) It is part of the standard template but you may have a need to delete this component for a specific page

There are at least two ways to implement such a need which I will attempt to detail below

Option 1: Use template to create boilerplate nodes – something that we recently discovered is I think is the cleanest and the easiest way to create such nodes.

template .content.xml

The nodes that is required to be created during page creation are placed in the template’s .content.xml file like the following

[code language=”xml”]
<?xml version=”1.0″ encoding=”UTF-8″?>
<jcr:root xmlns:sling=”http://sling.apache.org/jcr/sling/1.0″ xmlns:cq=”http://www.day.com/jcr/cq/1.0″ xmlns:jcr=”http://www.jcp.org/jcr/1.0″
jcr:description=”Create a Page with editable Disclaimer”
jcr:primaryType=”cq:Template”
jcr:title=”Disclaimer Page template”
ranking=”{Long}110″
shortTitle=”Disclaimer Page template”>

<jcr:content
jcr:primaryType=”cq:PageContent”
sling:resourceType=”app/components/customPage”>

<!– Any nodes placed here will be created for every page using this template –>
<par
jcr:primaryType=”nt:unstructured”
sling:resourceType=”foundation/components/parsys”>
<text
jcr:primaryType=”nt:unstructured”
sling:resourceType=”foundation/components/text”
text=”The information contained in this page – [pageTitle to be provided by page authors ] is for general information purposes only.”>
</text>
</par>
</jcr:content>
</jcr:root>
[/code]

template jsp renderer page

[code language=”xml”]
<%@page session=”false”%>
<%include file=”/libs/foundation/global.jsp” %>

<cq:include path=”par” resourceType=”foundation/components/parsys”/&>
[/code]

Using the above template will create a page with the default text that can be editable or deleted by page authors. In crx, you can see the following node structure upon creation of this page



The flexibility of this approach is that default text can be configured differently for different page templates.

Option 2: Create the require node structure programatically – AFAIK there are two ways to do that

a. Subscribe to page creation event and create the node structure as required – Haven’t tried this and not sure how this would work since this is not synchronous (i.e. not really part of the page creation process itself)

b. Hook into the New Page flow by providing custom Command where once the page is created using PageManager, the required nodes can be created using the APIs – This is more work and unnecessary override of the page creation process but does provide very good control


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.