<sling:include>'s resource attribute and paragraphs | Community
Skip to main content
LinearGradient
Level 6
October 16, 2015
Solved

<sling:include>'s resource attribute and paragraphs

  • October 16, 2015
  • 2 replies
  • 1789 views

In some component JSP scripts, I see code similar to

<sling:include resource="<%= par %>" />

In which par is a Paragraph object.

According to the API docs for Paragraph, the toString() method simply "returns a human readable string representation of this resource."

The question, then, is why is this string representation of the Paragraph instance passed to <sling:include>? And what is the general format of the string returned by Paragraph.toString()?

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 PaulMcMahon

The resource attribute of sling:include accepts either a path to a resource or a org.apache.sling.api.resource.Resource object. The com.day.cq.wcm.foundation.Paragraph class implements the org.apache.sling.api.resource.Resource. So in the code about the toString on the Paragraph object isn't being called, it's actually passing in the Paragraph object which the tag accepts because it implements the Resource interface. 

2 replies

PaulMcMahonAccepted solution
Level 8
October 16, 2015

The resource attribute of sling:include accepts either a path to a resource or a org.apache.sling.api.resource.Resource object. The com.day.cq.wcm.foundation.Paragraph class implements the org.apache.sling.api.resource.Resource. So in the code about the toString on the Paragraph object isn't being called, it's actually passing in the Paragraph object which the tag accepts because it implements the Resource interface. 

LinearGradient
Level 6
October 16, 2015

I now remember. But that was the prehistoric way of passing objects to tags. I wonder why the built-in JSPs do not use JSP EL (e.g. ${par}) to pass variables to tags.