May 13, 2020
Estimated Post Reading Time ~

AEM Templates in Details

In this post, I will explain the most useful properties of templates or you can say the most useful features provided by templates. Most of AEM developers familiar with these properties but don’t know how to use them. In this post, I will explain all of these properties in detail.

Agenda
  1. Allowed Paths Property (allowedPaths)
  2. Allowed Templates Property (cq:allowedTemplates)
  3. Allowed Parents Property (allowedParents)
  4. Allowed Children Property (allowedChildren)
Use of these Properties
“All of these properties are used to hide and show different templates at different page hierarchies at the time of page creation. For example – some templates are visible only under /content directory some are visible for page creation under /content/geometrixx hierarchy some are visible under /content/geometrixx/en hierarchy and some are visible at every level of page hierarchy.”

Note: These properties are a very important part of the AEM component developer certification exam. So let’s play with these properties.

allowedPaths
1). This property is a template level property so it must be defined at the template node.
2). The type of this property is a string array.
3). Its value will be the path under which this template will be visible for page creation in siteadmin. 

i.e. for showing a template under /content directory, we have to add this property with a value of /content(/.*)?. This is a regex string that represents that this template is visible under each and every page in /content directory.

If you change it’s value as /content/geometrixx/en/? then this template will be visible under /content/geometrixx/en page not under it’s child pages. i.e. this template will not be visible under /content/geometrixx/en/toolbar page.

So for the base template, you must define this allowedPaths property as /content(/.*)?, So that this template is visible for page creation.

Note: This property is required only for the top-level page creation. If you don’t provide this property then this template will not be visible in siteadmin. This property is also required if you don’t add cq:allowedTemplates property at top-level page jcr:content node. 
If you provide cq:allowedTemplates property at the top-level page then for all child templates, you don’t need to add allowedPaths property. This property will be explained in detail in this post.

For explaining these properties my Project structure is




At homepage node, I have added a property named as allowedPaths with a value /content/? so that this template will be visible in the siteadmin page creation dialog.
Now add one more property named as cq:allowedTemplates on jcr:content node of this template. 
I will explain this property after creating a page. Value of this property as /apps/blog/templates/.*

This will look like


Now go to siteadmin and create a new page using this template.

Note: All allowedPaths property is required only for the template using that you are going to create your website top-level page. For ex. /content/geometrixx , /content/geometrixx-outdoors etc pages.

cq:allowedTemplates 
1). This property is a page-level property, so it must be defined on the jcr:content node of the template. 
2). The type of this property is a string array.
3). Its value will be the path of templates that will be visible for page creation under the current page in siteadmin.
This property provides the restrictions on all templates that will be visible under the page created using this template. For example –
I have added this property on my homepage template with a value of /apps/blog/template/.* , it means that under the page created using the homepage template, only those templates will be visible those are present in /apps/blog/template directory. 
If you define a particular template path then only that particular template will be displayed for ex. if the value of this property is /apps/blog/template/templateRes then only this template will be visible.

You can think it as the first AEM check cq:allowedTemplates property and select a template group that can be displayed under the page created using this template.
Then AEM checks for allowedPaths property of these selected template groups and checks that path present in this property matches the path of the page, yes or not, if yes then this template will be displayed else not. In short

allowedParents
1). This property is a template level property so it must be defined at the template node. 
2). The type of this property is a string array.
3). Its value will be the path of templates that can behave as the parent of this template at the time of page creation in siteadmin.

This property is used to add another level of restriction i.e. if your templates satisfy both of the above-defined criteria or they are not present or defined empty then this property will be checked.
If this property exists then AEM will check the value of this property. This property can have the path of the templates that can behave as a parent of this template.


now the criteria for showing a template becomes 

cq:allowedTemplates (Parent Page) + allowedPaths (Parent Template) + allowedParents (Child Template) = template to be displayed.


allowedChildren
1). This property is a template level property so it must be defined at the template node.

2). The type of this property is a string array.
3). Its value will be the path of the templates that are allowed or visible for page creation under this template in siteadmin. 

This is a template level property. In my case, I have added this property to my homepage template.
If the above-defined properties and this property are not empty then a new level of restrictions will be added for showing templates in siteadmin.



So you can say the final template list will be shown on the basis of this criteria 
cq:allowedTemplates (Parent Page) + allowedPaths (Parent Template) + allowedParents (Child Template) + allowedChildren (Parent Template) = template to be displayed.



I tried my level best for making clear these properties and I have pushed the code related to this POC on my git repository and If I am not clear at any point, you can contact me. All details are listed below. Your views are welcome.

Reference documentation
https://docs.adobe.com/docs/en/cq/5-6-1/developing/templates.html

Github repository link
https://github.com/vietankur009/blog.git


By aem4beginner

No comments:

Post a Comment

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