Created
Basic CQ5 code examples
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") %>
Reference
Here a list of useful references regarding taglibs:
http://dev.day.com/docs/en/cq/current/howto/taglib.html
http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/
http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/wcm/api/Page.html
COMMENTS
-
Nice tips
-
This template is really helpful.
-
i like it
-
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.
-
You done certain good points there. I did a search on the subject and found nearly all persons will agree with your blog.
-
Thanks for keeping us informed
-
Its great to see you taking the time to share this information
-
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...
-
These were perfect examples I was having dificulty with my code but I believe I have now figured it out.
-
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.
Comments (10)