Search code examples
aemslingsightly

Given a Path get a reference to the Resource in Sightly


The component dialog has a pathfield widget where the authors can set a page path. In the Sightly component, I would like to look up that page resource and get (and display) properties from it.

The dialog...

<linkedPathLocation jcr:primaryType="cq:Widget"
              fieldLabel="Linked Path"
              name="./linkedPathLocation"
              xtype="pathfield"
              fieldDescription="Select a page. URL, Title, Description and Image are properties of the selected page"/>   

The component code I would like to work (it's not).

  <div class="row" data-sly-resource.page = "${properties.linkedPathLocation}">
    <h1 >${page.title}</h1>
    <p>${page.description}</p>
  </div>

My question: Is there a way in Sightly to resolve and use some resource from a given path? If not, I could create a USE-API class and to do the following...

Page page = resourceResolver.resolve("/path/to/resource").adaptTo(Page.class); 

Solution

  • What you are trying to do, in essence, is render a Resource within the context of the rendering of another Resource. data-sly-resource seems the appropriate attribute to use, but instead of attempting to nest additional elements into the element containing the data-sly-resource you should define another Sightly .html file which dictates how the nested resource is to be rendered.

    Let us say that your Resource is of type application/components/content/type. Within type.html you might have the following statement

    <sly data-sly-resource="${properties.linkedPathLocation} @ resourceType='application/components/content/type/subtype' />
    

    You would then be able to define /apps/application/components/content/type/subtype/subtype.html containing the rendering to produce which would be invoked in the context of the Resource identified by your path.