Expand my Community achievements bar.

How to use a Sling Model to adapt a Resource defined in an Editable Template?

Avatar

Level 4

I have a simple sling model, the important part looks like this:

This works fine if I use HTL's <data-sly-use="..."> in a component to access resources under the page where the component is used.

However, if my component is used in the structure of an editable template, this breaks since the resources I want are in the template node, not the page node.

Is there a way to access the template resource node instead?

4 Replies

Avatar

Administrator

Checking this with the Team.



Kautuk Sahni

Avatar

Level 10

You can bind nodes in the AEM JCR like this. Create a Resource that points to the node in the AEM JCR - an editable template in your example:

Resource resource = resolver.getResource("/content/testsling/slingmodel");

  ValueMap valueMap=resource.adaptTo(ValueMap.class);

  response.getWriter().write("Output from ValueMap is First Name: "+valueMap.get("firstName").toString()+" Last Name: "+valueMap.get("lastName").toString()+" Technology: "+valueMap.get("technology").toString()+"");

  UserInfo userInfo = resource.adaptTo(UserInfo.class);

  response.getWriter().write("Output from Sling Model is First Name: "+userInfo.getFirstName()+" Last Name: "+userInfo.getLastName()+" Technology: "+userInfo.getTechnology());

Using this - you can base a Sling Model on JCR nodes.

Adobe Experience Manager Help | Creating Adobe Experience Manager 6.3 Sling Model Components

Hope this helps....           

Avatar

Community Advisor

Hey,

Try to adapt sling model with {SlingHttpServletRequest.class} and use @ValueMapValue annotation over fields.

Thanks,

Himanshu

Avatar

Level 4

I looked into @ValueMapValue and @Via but did not see anything that would automatically "walk" the JCR back to the template. As a work-around I hard-coded a JCR path as in smacdonald2008​'s suggestion, but would like to refactor this for better maintainability/re-use.