Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to read Dynamic Template structure content in Sling Model of a component using Java ?

Avatar

Level 3

When I iterate content nodes of a page, I cannot find any structure contents in Dynamic Template. How can I read components in Dynamic Template of a page?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

@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.

 

 

Avatar

Employee

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.

 

Avatar

Correct answer by
Community Advisor

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