3

I'm using Adobe CQ5. I have made two components that are independent of each other. However, I want to use data from one component in the other one. How do I achieve this? How can I make two components interact with each other?

Thanks in advance.

1
  • 1
    could you be more specific? Do you mean accessing the jcr node of another component ?making them interact through JavaScript ? Jan 24, 2014 at 8:01

3 Answers 3

3

There are a number of options, depending on how closely the components are coupled (e.g. will both always be placed on the page at the same time? Are they hard-wired into the template, or placed by the editors, etc.)

If both components are on the same page, the simplest is probably to set a variable within one component & read it in another, e.g.:

foo.jsp

<c:set var="globalFooTitle" value="${properties.title}" scope="request"/>

bar.jsp

<c:out value="${globalFooTitle}">[Optional fallback value]</c:out>
4
  • it will not always work - it depends on which component is processed first
    – zacheusz
    Aug 19, 2014 at 21:37
  • 1
    @zacheusz Yes, you can't read a property that hasn't been set yet — it's still parsing the scripts sequentially. Aug 20, 2014 at 7:53
  • 1
    This is the best approach I've ever met yet to pass parameters from one component to another at run-time. However, I'd rather add <c:set var="globalParam" value="${null}" /> right after usage to avoid side effects on other components that could share the same approach.
    – Micik
    Oct 10, 2014 at 6:49
  • 1
    @Micik, that's true — hopefully this approach is more of a rarity, but if you commonly need to do something along these, you'd probably also need to think through a structure for name-spacing things Oct 10, 2014 at 8:13
2

While there is nothing wrong with @kmb's answer, I almost always prefer to use higher level Apis than lower level ones. In the case of CQ, this means using the sling resource API instead of the JCR node API.

That would look something like this, assuming the same structure of the 2 components on a single page as he laid out.

Resource r = resourceResolver.getResource(currentResource, "../component2");
if(r != null) {
    ValueMap props = r.adaptTo(ValueMap.class);
    String somePropertyValue = props.get("someProperty", "");
}
0

You can use javax.jcr.Node and javax.jcr.Property interface to get properties of another component. For example, you have added component1 and component2 to page1. In the repository you should have structure similar to this:

/content
    /project
        /page1
            /jcr:content
                /parsys
                    /component1
                        /...some properties
                    /component2
                        /...some properties

If you want to get properties of component2 while in component1, you can use something like:

Node parsys = currentNode.getParent();
if(parsys.hasNode("component2")) {
    Node component2 = parsys.getNode("component2");
    if(component2.hasProperty("someProperty"))
      Property someProperty = component2.getProperty("someProperty");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.