Emulating requestScope Variables In Sightly For AEM

For one of our clients, that we are doing an AEM implementation for, we have certain pages that we get from a feed and stick in the jcr under each page. The pages have a bunch of components inside.

-page
-jcr:content
-course-data
-authors
-some-guy
[name = Some Guy]
[etc]
-someone-else
[name = ]
[etc]

We need to prep some of the data by putting it into collections that are easy to spin through and hold them in a bean. But we have to pass that data to components on the page. We don’t want to make the bean a bunch of times.

In JSTL you can easily change the scope of variables:


which is good for passing stuff between JSPS.
Sightly does not have an easy way to do this, so I made one.

Course.java – Use API class that sets up the bean:

package apps.WEBSITE.components.pages.course_page.Course;
public class Course extends WCMUse {
    @Override
    public void activate() throws Exception {     
        getRequest().setAttribute("courseObject", "a big bean");
    }
}

course-page.html – top level call to the bean Use API


    whatever components and stuff
    

author-component.html – some sub component on the page

loop on ${requestUtil.attributes['courseObject'].authors}

RequestUtil.java – util to get attrbutes out of the request

package com.WEBSITE.sightlyutils;
public class RequestUtil extends WCMUse {
    private Map<String, Object> attributes;
    @Override
    public void activate() throws Exception {
        attributes = new HashMap<String, Object>();
        String attributeName = get("attributeName", String.class);
        attributes.put(attributeName, getRequest().getAttribute(attributeName));
    }
    public Object getAttributes() {
        return attributes;
    }
}

Here are some additional resources from Stack Overflow if you are interested.
http://stackoverflow.com/questions/25404863/aem-6-0-additional-parameters-when-using-data-sly-resource/28177204#28177204
http://stackoverflow.com/questions/25430981/are-there-requestscope-variables-in-sightly