In this arti­cle I will give you some exam­ples on how to use the Val­ueMap func­tion­al­ity in your CQ5/AEM6 application.

And I will intro­duce two good friends of the Val­ueMap that are really useful.

By default you already have a few Val­ueMaps avail­able in your com­po­nents, these are cre­ated via the cq:defineObjects tag. In case you use Sightly the same objects are avail­able in your com­po­nent, this with­out includ­ing any­thing extra.

This is located in the global.jsp that is by default in the “/libs/foundation” folder. One of the most com­monly used one is “prop­er­ties”, this will expose the prop­er­ties of your component.

You can use this like this in your component:

When you want to get the value of a property:

${properties.yourproperty}

<%= properties.get(“yourproperty”, “default value”) %>

${properties[“yourproperty”]

In case you have a few nested nodes you can do the following:

${properties[“node1/node2/myproperty”]}

<%= properties.get(“node1/node2/myproperty”, “default value”) %>

When the prop­erty isn’t found an empty String will be returned.

So there is no need to do checks within the Node.class like you see often:

node1.hasNode(“node2”) && node1.getNode(“node2”).hasProperty(“myproperty”)

Sightly:

${properties.yourproperty}

${properties[“node1/node2/myproperty”]}

${properties[’node1/node2/myproperty’]}

Get­ting a ValueMap

If you want to get a Val­ueMap from a Resource you have, you can do this via .adaptTo(), or use ResourceUtil.getValueMap().
ResourceU­til has some safety checks that will make sure an empty Val­ueMap is returned in case the Resource can’t be adapted.

In AEM6 you now also have the method get­Val­ueMap() on the Resource.

Val­ueMap vm = resource.adaptTo(ValueMap.class);

Val­ueMap vm = resource.getValueMap();

Val­ueMap vm = ResourceUtil.getValueMap(resource);

Friend 1: ValueMapDecorator

The first friend of the Val­ueMap is the Val­ueMapDec­o­ra­tor, this class can be used to cre­ate a cus­tom Val­ueMap that is not based on a resource.
Also this instance of a Val­ueMap allows you to call the put-method. When you do this on a Val­ueMap that is based on a resource you will get an UnsupportedOperation-exception.

Here is an exam­ple on how you can do this:

Val­ueMap props = new ValueMapDecorator(new HashMap());

props.put(“test”, “value”);

Friend 2: InheritanceValueMap

The sec­ond friend of the Val­ueMap is the Inher­i­tance­Val­ueMap, this is a Val­ueMap that can be used if you have prop­er­ties defined at multiple-levels and you want to retrieve the cor­rect one. This will avoid you to code a loop that goes through all the par­ents and checks for a cer­tain property.

For exam­ple if you have a page-tree whereby at every level you can set the color, at page-level you just want to get the color that need to be used for this page.

When you use Sightly there is the object “inherit­ed­Page­Prop­er­ties” available.

This is how you can cre­ate an instance of the InheritanceValueMap:

Inher­i­tance­Val­ueMap inVM = new HierarchyNodeInheritanceValueMap(page.getContentResource());

To get an value by inher­i­tance sim­ply call this method:

inVM.getInherited(“myproperty”, “default”);

In this next arti­cle I will intro­duce the Mod­i­fi­abl­e­Val­ueMap, that makes chang­ing of prop­er­ties really easy.

Happy cod­ing!

@heervisscher