Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

<sling:include>'s resource attribute and paragraphs

Avatar

Level 7

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()?

1 Accepted Solution

Avatar

Correct answer by
Level 8

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. 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 8

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. 

Avatar

Level 7

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.