You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Phil Rice <ph...@googlemail.com> on 2011/07/17 11:56:15 UTC

Changing the extension with

I would like to be able to make it so that when I use a
<sling:include> I can request the included resource to have a
different extension. In the following experiment I am trying to get
the .json representation of the children to be included.

I tried the following in sling/servlet/default/hello.jsp (headers
removed for clarity)

     This is the default from hello.jsp</br />
	 <% for (Iterator<Resource> iterator = resource.listChildren();
iterator.hasNext();){ %>
	 <%    Resource child = iterator.next(); %>
	    <ul>
	    <li>ChildPath: <%= child.getPath() %></li>
	    <li>JustTag <sling:include resource="<%= child %>" /></li>
	    <li>With resourceType <sling:include resource="<%= child %>"
resourceType="components/summary" /></li>
	    <li>With replaceSuffix <sling:include resource="<%= child %>"
replaceSuffix="json" /></li>
	    <li>With replaceSelectors <sling:include resource="<%= child %>"
replaceSelectors="json" /></li>
	    <li>With addSelectors <sling:include resource="<%= child %>"
addSelectors="json" /></li>
            </ul>
	 <% } %>

I have a hello.jsp in components/hello.jsp and another in
components/summary/hello.jsp

When I view a page with one child (and the page didn't have a
resourceType) I get the following

This is the default from hello.jsp
ChildPath: /content/mynode/ChildNode
JustTag This is the default from hello.jsp
With resourceType This is the components/summary/hello.jsp
With replaceSuffix This is the default from hello.jsp
With replaceSelectors This is the default from hello.jsp
With addSelectors This is the default from hello.jsp

So what is happening is that the extension of the original query is
being propogated through the <sling:include> tag. By specifying the
sling:resourceType I can actually execute arbitray scripts, as long as
they have the same extension type as the original query.

Is there any mechanism for getting the html that I would have got from
 <childNode>.json and including it in the output?

Re: Changing the extension with

Posted by Phil Rice <ph...@googlemail.com>.
Thank you that worked perfectly

On Sun, Jul 17, 2011 at 12:15 PM, Julian Sedding <js...@gmail.com> wrote:
> Hi Phil
>
> You can simply do <sling:include path=".json"/> if you want the
> current resource rendered as json or in your case <sling:include
> path="<%=child.getPath()%>.json"/> (optionally with resourceType
> attribute).
>
> Note: the default json rendering servlet sets the Content-Type header
> to application/json. This can cause your HTML to be returned with the
> incorrect content type. You can mitigate this by explicitly flushing
> the output stream before the include.
>
> Regards
> Julian
>
>
> On Sun, Jul 17, 2011 at 11:56 AM, Phil Rice
> <ph...@googlemail.com> wrote:
>> I would like to be able to make it so that when I use a
>> <sling:include> I can request the included resource to have a
>> different extension. In the following experiment I am trying to get
>> the .json representation of the children to be included.
>>
>> I tried the following in sling/servlet/default/hello.jsp (headers
>> removed for clarity)
>>
>>     This is the default from hello.jsp</br />
>>         <% for (Iterator<Resource> iterator = resource.listChildren();
>> iterator.hasNext();){ %>
>>         <%    Resource child = iterator.next(); %>
>>            <ul>
>>            <li>ChildPath: <%= child.getPath() %></li>
>>            <li>JustTag <sling:include resource="<%= child %>" /></li>
>>            <li>With resourceType <sling:include resource="<%= child %>"
>> resourceType="components/summary" /></li>
>>            <li>With replaceSuffix <sling:include resource="<%= child %>"
>> replaceSuffix="json" /></li>
>>            <li>With replaceSelectors <sling:include resource="<%= child %>"
>> replaceSelectors="json" /></li>
>>            <li>With addSelectors <sling:include resource="<%= child %>"
>> addSelectors="json" /></li>
>>            </ul>
>>         <% } %>
>>
>> I have a hello.jsp in components/hello.jsp and another in
>> components/summary/hello.jsp
>>
>> When I view a page with one child (and the page didn't have a
>> resourceType) I get the following
>>
>> This is the default from hello.jsp
>> ChildPath: /content/mynode/ChildNode
>> JustTag This is the default from hello.jsp
>> With resourceType This is the components/summary/hello.jsp
>> With replaceSuffix This is the default from hello.jsp
>> With replaceSelectors This is the default from hello.jsp
>> With addSelectors This is the default from hello.jsp
>>
>> So what is happening is that the extension of the original query is
>> being propogated through the <sling:include> tag. By specifying the
>> sling:resourceType I can actually execute arbitray scripts, as long as
>> they have the same extension type as the original query.
>>
>> Is there any mechanism for getting the html that I would have got from
>>  <childNode>.json and including it in the output?
>>
>

Re: Changing the extension with

Posted by Julian Sedding <js...@gmail.com>.
Hi Phil

You can simply do <sling:include path=".json"/> if you want the
current resource rendered as json or in your case <sling:include
path="<%=child.getPath()%>.json"/> (optionally with resourceType
attribute).

Note: the default json rendering servlet sets the Content-Type header
to application/json. This can cause your HTML to be returned with the
incorrect content type. You can mitigate this by explicitly flushing
the output stream before the include.

Regards
Julian


On Sun, Jul 17, 2011 at 11:56 AM, Phil Rice
<ph...@googlemail.com> wrote:
> I would like to be able to make it so that when I use a
> <sling:include> I can request the included resource to have a
> different extension. In the following experiment I am trying to get
> the .json representation of the children to be included.
>
> I tried the following in sling/servlet/default/hello.jsp (headers
> removed for clarity)
>
>     This is the default from hello.jsp</br />
>         <% for (Iterator<Resource> iterator = resource.listChildren();
> iterator.hasNext();){ %>
>         <%    Resource child = iterator.next(); %>
>            <ul>
>            <li>ChildPath: <%= child.getPath() %></li>
>            <li>JustTag <sling:include resource="<%= child %>" /></li>
>            <li>With resourceType <sling:include resource="<%= child %>"
> resourceType="components/summary" /></li>
>            <li>With replaceSuffix <sling:include resource="<%= child %>"
> replaceSuffix="json" /></li>
>            <li>With replaceSelectors <sling:include resource="<%= child %>"
> replaceSelectors="json" /></li>
>            <li>With addSelectors <sling:include resource="<%= child %>"
> addSelectors="json" /></li>
>            </ul>
>         <% } %>
>
> I have a hello.jsp in components/hello.jsp and another in
> components/summary/hello.jsp
>
> When I view a page with one child (and the page didn't have a
> resourceType) I get the following
>
> This is the default from hello.jsp
> ChildPath: /content/mynode/ChildNode
> JustTag This is the default from hello.jsp
> With resourceType This is the components/summary/hello.jsp
> With replaceSuffix This is the default from hello.jsp
> With replaceSelectors This is the default from hello.jsp
> With addSelectors This is the default from hello.jsp
>
> So what is happening is that the extension of the original query is
> being propogated through the <sling:include> tag. By specifying the
> sling:resourceType I can actually execute arbitray scripts, as long as
> they have the same extension type as the original query.
>
> Is there any mechanism for getting the html that I would have got from
>  <childNode>.json and including it in the output?
>