Skip to content

Instantly share code, notes, and snippets.

@UberNick
Last active December 22, 2015 15:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UberNick/6492960 to your computer and use it in GitHub Desktop.
Save UberNick/6492960 to your computer and use it in GitHub Desktop.
<%@include file="/libs/foundation/global.jsp" %>
<cq:include path="myComponent" resourceType="matelli/components/content/myComponent" />
<%
/** Option 1: Simply direct reference */
//String componentPath = "/content/myPage/jcr:content/myComponent"; //path to component
//Node node = resourceResolver.getResource(componentPath).adaptTo(Node.class);
//String title = node.getProperty("jcr:title").getString();
//String text = node.getProperty("jcr:text").getString();
/** Option 2: Direct reference, check for resource existence */
//String componentPath = "/content/myPage/jcr:content/myComponent"; //path to component
//Resource myResource = resourceResolver.getResource(componentPath);
//String title = "", title = "";
//if (!Resource.RESOURCE_TYPE_NON_EXISTING.equals(myResource.getResourceType())) {
// Node node = myResource.adaptTo(Node.class);
// title = node.getProperty("jcr:title").getString();
// text = node.getProperty("jcr:text").getString();
//}
/** Option 3: Page-relative reference */
//Page myPage = currentPage; //reference to whatever page contains the component";
//String componentPath = myPage.getPath() + "/jcr:content/myComponent"; //path to component
//String title = node.getProperty("jcr:title").getString();
//String text = node.getProperty("jcr:text").getString();
/** Option 4: Page-relative reference with getContentResource, and hasProperty check */
Node node = currentPage.getContentResource("myComponent").adaptTo(Node.class);
String title = node.hasProperty("jcr:title") ? node.getProperty("jcr:title").getString() : "default title";
String text = node.hasProperty("jcr:text") ? node.getProperty("jcr:text").getString() : "default text";
%>
Component's title: <%= title %> <br/>
Component's text: <%= text %> <br/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment