Created

Jun 10, 2012

Basic CQ5 code examples

Posted by Feike Visser

In this blogpost I will go through some sample code that you can use in your components, all samples are available out of the box.

So there is no need to write custom code in OSGi bundles for these samples.

Make sure you have this line at the beginning of your component:

<%@include file="/libs/foundation/global.jsp" %>

This global.jsp defines a set of global taglibs and makes a number of objects available that you can use via <cq:defineObjects/>. Documentation of the standard taglibs can be found here.

EL notations

In this set you see some samples how to display values from the objects available in your components. Via the EL-notation you can easily access methods from the objects.

display the name of the component

${component.name}

page name

${currentPage.name}

resource page name

${resourcePage.name}

Properties

Next set is about properties and ways to use these in your component.

custom page property

${pageProperties.customProperty}

custom page property (same as above)

${currentPage.properties.customProperty}

property set via the Edit dialog

${properties.myProperty}

property set via the Edit dialog

${properties["myProperty"]}

print a property via cq:text

assigned property value to a var

${newvar}

If and For Each

This set is about doing an If-statement and doing a loop (For Each). 

listing sling selectors

${status.count}: ${item}

number of selectors

${fn:length(slingRequest.requestPathInfo.selectors)}

only show this if the page-name equals 'test'

this is a test page

page name not empty and length > than 10

pagename is not empty and more than 10

Formatting

This set is about formatting, date format and simple String formatting.

length of page name

${fn:length(currentPage.name)}

substring of page name

${fn:substring(currentPage.name,0,5)}

format date

Inheritance

The pageProperties object is an instance of the InheritanceValueMap, this adds two methods to the ValueMap interface:

getInherited( name, type );

getInherited( name, defaultValue);

This way you can get an inherited value with just one method call, no need for any custom code that gets an inherited value.

display the customProperty, via the inherited way

<%= pageProperties.getInherited("customProperty", "no value found") %>

COMMENTS

  • By Mohamed Homaid - 10:13 PM on Jun 28, 2012   Reply
    Nice tips
  • By om - 10:18 AM on Feb 11, 2013   Reply
    This template is really helpful.
  • By diễn đàn seo - 4:20 AM on Apr 17, 2013   Reply
    i like it
  • By feminized seed - 1:14 PM on Apr 17, 2013   Reply
    So this can be used to write the program? I have tried what you have suggested and got an error like code error. I have no idea where it went wrong. I will execute it later and see how it works. Thanks for updating the info.
  • By getonlinegambling - 2:45 PM on Apr 22, 2013   Reply
    You done certain good points there. I did a search on the subject and found nearly all persons will agree with your blog.
  • By nj photo booth - 4:59 PM on Apr 27, 2013   Reply
    Thanks for keeping us informed
  • By wedding photographers nj - 9:41 PM on Apr 27, 2013   Reply
    Its great to see you taking the time to share this information
  • By www.seo-rant.com - 7:50 AM on Apr 30, 2013   Reply
    Thank you for the efforts you been putting on making your site such an interesting and informative place to browse through. I'll be visiting your site again to gather some more valuable information. You truly did a good job...
  • By prom limo rental nyc - 7:52 PM on May 02, 2013   Reply
    These were perfect examples I was having dificulty with my code but I believe I have now figured it out.
  • By additional resource - 9:19 PM on May 11, 2013   Reply
    because of the feet has long been a pair of shoes, but now with a cushion insoleway does not seem suitable for those beautiful high heels, flat shoes, then you can skip to give it a try.