6 Replies Latest reply on Jun 27, 2012 4:57 PM by Clookes

    How do I use <sling:include>?

    Clookes Level 1



      I am trying to <sling:include> a content node wrapped inside <div> on a jsp.


      For example I have a textimage content node:  "/content/company/en/product/jcr:content/par/textimage"


      I can access the html content by hitting http://localhost:4502/content/comapny/en/product/jcr:content/part/textimage.html


      But now, I would like those HTML content to be rendered inside another JSP like this, so I tried using <sling:include>


      <%@ taglib uri="http://cms.ecom.shc.com/bundles/commerce/global/trends/common" prefix="content" %>

      <%@include file="/apps/commerce/global-commerce/global.jsp" %>

      <%@page session="false"

              trimDirectiveWhitespaces="true" %>


      <div><sling:include path="/content/verticalpages/fashiontrends/en/trends/prom-shop/jcr:content/par/textimage" /></div>



      However, I am only seeing empty <div> where the <sling:include> is, no HTML content was returned.


      What am I doing wrong?



      Thanks in advance.

        • 1. Re: How do I use <sling:include>?
          msulliva Level 2

          I'm going to assume some typos, but it looks like you have your paths confused.


          In your Sling Include statement, you're including:


          /content/verticalpages/fashiontrends/en/trends/prom-shop/jcr:co ntent/par/textimage


          but you're pulling up:




          I would think that using the following would work (assuming that part was a typo for par and comapny was a typo and you mean company:


          <sling:include path="/content/company/en/product/jcr:content/par/textimage" />

          • 2. Re: How do I use <sling:include>?
            Clookes Level 1

            Oops, sorry about the typo, It actually meant I did use


            <sling:include path= "/content/company/en/product/jcr:content/par/textimage"/> to try to pull the content, but it doens't pulling the content and is not returning anything.

            • 4. Re: How do I use <sling:include>?
              Clookes Level 1

              That is correct, "http://localhost:4502/content/company/en/product/jcr:content/par/textimage.html" renders fine.


              I just added a 'getHtml.jsp" in "/apps/company/components/textimage" and change the <sling:include> to


              <sling:include path="/content/company/en/product/jcr:content/par/textimage.getHtml"/></div>


              And that worked. 


              However, getHtml.jsp is a simplified version of textimage.jsp, so maybe some stuff was preventing it from rendering properly in the original include without ".getHtml"..

              • 5. Re: How do I use <sling:include>?

                First, check out the OSGi console's "Recent Requests" to debug the issue (/system/console/requests). It should give you a complete break down of the request to the page, including useful debug info about how your sling:include is being resolved to a resource in the system (e.g. the resource path, selectors and extensions used, what it thinks it resolved to, what script it's trying to use, etc).


                Second, try either adding a .html to your absolute include path or else try using a relative path; when using a full, absolute path I'm wondering if Sling is not carrying the extension over from the original request like it normally would. So an include on "/content/company/en/product/jcr:content/par/textimage" might find the resource, but not know what to do with it because it's dropped the html extension that it should have carried over from the original request. Using the recent requests output should clearly indicate if this is happening. This would make sense  with the fact that adding .getHtml works with your custom getHtml script, because that's adding in a selector that Sling can use to resolve the correct script.


                Third, you could try using the resource attribute of the sling:include rather than the path attribute. You would first have to fetch this child par/textimage resource from the current resource object (either getChild or using the resource resolver, not sure which would be required).


                Fourth, is there a specific reason you aren't using the cq:include tag instead?

                • 6. Re: How do I use <sling:include>?
                  Clookes Level 1

                  It's a complicated requirement.  But, bascally, I need to loop through all the components on a page, get the HTML of each component as a String, and then append them all togther in a JSON response.  So, I have to dynamically general the JSP (using <sling:include>) to render the HTML, and output that as a String and pass that back to the original JSON request.


                  I would use <cq:include> if it works, but it doesn't, not sure what the diff is between them but <sling:include> works for now.