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?
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");
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.
Hope this helps....
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.