How to read Dynamic Template structure content in Sling Model of a component using Java ? | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

Hello @fulongt37334138 

 

If you are trying to identify that the resource is rendered from a template or a Page, then you can create a utility function like this 

public static boolean isTemplateResource(Resource resource){ if (resource == null) { return false; } String path = resource.getPath(); boolean isTemplatePath = (path.startsWith("/conf/") && path.contains("/settings/wcm/templates/")); return resource instanceof TemplatedResource || isTemplatePath; }

 

It would really be helpful, if you can help us understand the use-case. Just FYI, page and templates are tied via a cq:template property. See if that helps !

 

3 replies

A_H_M_Imrul
Community Advisor
Community Advisor
October 6, 2023

@fulongt37334138 

The content of cq:page does not contain information about structure (if you are referring to /conf/${project-name}/settings/wcm/templates/${template-name}/structure)

Can you please share some more insight about your business expectation, probably will able to help better.

 

 

joerghoh
Adobe Employee
Adobe Employee
October 7, 2023

Hi,

 

I don't understand why you need to read the structure of the template in your own code. In the majority of all cases it should be sufficient to let AEM drive the resolution of the components and just implement the rendering of these components.
Deducing the structure of the page (that means the component hierarchy) is non-trivial and I am not sure if there's an official API for it.

 

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
October 9, 2023

Hello @fulongt37334138 

 

If you are trying to identify that the resource is rendered from a template or a Page, then you can create a utility function like this 

public static boolean isTemplateResource(Resource resource){ if (resource == null) { return false; } String path = resource.getPath(); boolean isTemplatePath = (path.startsWith("/conf/") && path.contains("/settings/wcm/templates/")); return resource instanceof TemplatedResource || isTemplatePath; }

 

It would really be helpful, if you can help us understand the use-case. Just FYI, page and templates are tied via a cq:template property. See if that helps !

 

Aanchal Sikka