0

Currently using

 <cq:include script="/apps/components/list-value.jsp" />

but I now need to include a page with param so the counter is limited to within the included page, I am thinking of using:

 <jsp:include page="/apps/components/list-value.jsp" >
    <jsp:param name="counter" value="${counter + 1}" />
 </jsp:include> 

but I keep getting compilation error "org.apache.sling.api.SlingException: Exception during response processing"

Is there equivalent cq: function for this?

user5733033
  • 55
  • 11

1 Answers1

0

Probably you get the error because the resource is not found, jsp:resource isn't prepared to get a resource from inside the JCR. Other bad news is that it isn't possible to pass arbitrary parameters to cq:include nor to sling:include (this is other candidate you could use).

One option you have is to set a request attribute and read that inside your script:

    <%request.setAtributte("counter", anyvalue);%>
    <cq:include script="/apps/components/list-value.jsp" />
    <%request.removeAttribute("counter"); %>  

it is pretty ugly, but it is the only way I know if you want to continue using cq:include or sling:include.

other option that just came to my mind is to have the script as a tag file, then you could just pass a parameter to it. First create a tag file in JCR, say it will be stored at '/path/to/the/dir/with/tag/files/list-value.tag'. Then you can import and use it (note the '/WEB-INF/tags' prefix, it is important):

    <%@taglib prefix="t" tagdir="/WEB-INF/tags/path/to/the/dir/with/tag/files" %>
    <t:list-value counter=${counter + 1} />

as a last option, you could rewrite your code in a way that you don't need to pass parameters to the script.

  • Thanks, I actually tried to render a report dynamically and the counter is for indentation (depending on the depth). If I have 2 headers, each will be indented with same distance. Your answer, removeAttribute, resets the indentation such that the header2 will not have same indentation as header1. I will try the custom taglib solution, not sure this can work since the page makes recursive 'include'. – user5733033 Feb 07 '16 at 22:37
  • Hi, it seems your use case is too complex for the request attibute hack. I think the tag file idea will be better. And tag files does handle recursive calls, I already done that in components of mine. Please, report back your findings. – brunovianarezende Feb 23 '16 at 09:59