Getting child pages on the root page
A developer is creating a custom component using sling model on the page and listing all the child pages of the root page. Wants to show all child pages on the root page itself .How can we achieve that?
A developer is creating a custom component using sling model on the page and listing all the child pages of the root page. Wants to show all child pages on the root page itself .How can we achieve that?
@nathes There are two ways:
1. As part of component dialog provide a path browser property to choose root path and read all its child in sling model.
2. Consider the current page as root page where we are dropping this component. Using current page you can iterate its child, get its value and return to Sightly to show on the page.
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Iterator;
@Model(adaptables = { SlingHttpServletRequest.class,
Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class RootPathModel {
@Inject
private Page currentPage;
@PostConstruct
protected void init() {
Iterator<Page> childPages = currentPage.listChildren();
while (childPages.hasNext()) {
Page childPage = childPages.next();
// Collect data, store it in list and return to sightly.
}
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.