Created

Feb 01, 2013

ValueMap and his friends

Posted by Feike Visser

In this article I will give you some examples on how to use the ValueMap functionality in your CQ5/AEM6 application.

And I will introduce two good friends of the ValueMap that are really useful.

By default you already have a few ValueMaps available in your components, these are created via the cq:defineObjects tag. In case you use Sightly the same objects are available in your component, this without including anything extra.

This is located in the global.jsp that is by default in the "/libs/foundation" folder. One of the most commonly used one is "properties", this will expose the properties 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 property 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']}

Getting a ValueMap

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

In AEM6 you now also have the method getValueMap() on the Resource.

ValueMap vm = resource.adaptTo(ValueMap.class);

ValueMap vm = resource.getValueMap();

ValueMap vm = ResourceUtil.getValueMap(resource);

Friend 1: ValueMapDecorator

The first friend of the ValueMap is the ValueMapDecorator, this class can be used to create a custom ValueMap that is not based on a resource.
Also this instance of a ValueMap allows you to call the put-method. When you do this on a ValueMap that is based on a resource you will get an UnsupportedOperation-exception.

Here is an example on how you can do this:

ValueMap props = new ValueMapDecorator(new HashMap());
	
props.put("test", "value");

Friend 2: InheritanceValueMap

The second friend of the ValueMap is the InheritanceValueMap, this is a ValueMap that can be used if you have properties defined at multiple-levels and you want to retrieve the correct one. This will avoid you to code a loop that goes through all the parents and checks for a certain property.

For example 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 "inheritedPageProperties" available.

This is how you can create an instance of the InheritanceValueMap:

InheritanceValueMap inVM = new HierarchyNodeInheritanceValueMap(page.getContentResource());

To get an value by inheritance simply call this method:

inVM.getInherited("myproperty", "default");

In this next article I will introduce the ModifiableValueMap, that makes changing of properties really easy.

Happy coding!

@heervisscher

 

Related links:

ValueMap

ValueMapDecorator

InheritanceValueMap 

HierarchyNodeInheritanceValueMap

ResourceUtil

COMMENTS

  • By APK - 10:24 AM on May 31, 2014   Reply
    Many Thanks.
  • By additional reading - 12:47 PM on May 31, 2014   Reply
    Aradoxes experienced in a globe that now has among the biggest older individuals and adolescents places in history. As the globe techniques 7 billion dollars .
    • By tidung.net - 5:04 AM on Jun 02, 2014   Reply
      Thanks.
      • By apk - 11:21 AM on Jun 06, 2014   Reply
        Thanks.
        • By Jasa SEO - 11:30 AM on Jun 08, 2014   Reply
          Many Thanks.
          • By Tattoos - 4:37 PM on Jun 08, 2014   Reply
            Many Thanks.
            • By SEO - 4:02 AM on Jun 09, 2014   Reply
              Einstein. Lorentz had already gotten essential equations suggested, and Poincaré had put in his perform, but Einstein instead of disagreeing for an presentation with regards to deformations of the ether or of issue, simply provided up the.
              • By SEO - 4:03 AM on Jun 09, 2014   Reply
                Einstein. Lorentz had already gotten essential equations suggested, and Poincaré had put in his perform, but Einstein instead of disagreeing for an presentation with regards to deformations of the ether or of issue, simply provided up the.
                • By Standard Assessment Procedure - 7:11 AM on Jun 09, 2014   Reply
                  Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes a purchase.
                  • By responsive landing page design - 10:44 AM on Jun 09, 2014   Reply
                    Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes a purchase.
                    • By event photography - 11:54 AM on Jun 09, 2014   Reply
                      Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes a purchase.
                      • By Best Cash Back - 6:43 AM on Jun 10, 2014   Reply
                        Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes a purchase.
                        • By bestrecumbentbikereviews.net - 6:32 AM on Jun 11, 2014   Reply
                          Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes a purchase.
                          • By properti - 6:46 AM on Jun 12, 2014   Reply
                            Then I wrote Let’s Give Her Some Lifestyle and my greatest manufacturing was Cindy’s Shoes, a present day Cinderella which I am transcription because although I wrote it in 1988 at the age of 20 it’s still appropriate.
                            • By important link - 10:21 AM on Jun 13, 2014   Reply
                              Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes.
                              • By jual apartemen - 2:40 AM on Jun 14, 2014   Reply
                                Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes. Then I wrote Let’s Give Her Some Lifestyle and my greatest
                                • By jual apartemen - 2:40 AM on Jun 14, 2014   Reply
                                  Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes. Then I wrote Let’s Give Her Some Lifestyle and my greatest
                                  • By jual apartemen - 2:41 AM on Jun 14, 2014   Reply
                                    Everything from tables to beds to chairs is customizable. Plus, it automatically sends orders to manufacturers when a customer makes. Then I wrote Let’s Give Her Some Lifestyle and my greatest
                                    • By rumah dijual di purwokerto - 2:44 AM on Jun 21, 2014   Reply
                                      I’m often to blogging and i really respect your content. The article has actually peaks my interest. <a href="http://www.urbanindo.com/cari/rumah/dijual/Purwokerto,%20Jawa%20Tengah">rumah dijual di purwokerto</a>
                                      • By Obat Keputihan - 9:29 AM on Jun 22, 2014   Reply
                                        Many Thanks.
                                        • By he has a good point - 10:26 AM on Jul 05, 2014   Reply
                                          Furthermore, if you need some item or set up details, it is often much much better to find your alternatives online than to get in touch with someone and discuss your needs.
                                          • By airul - 10:54 AM on Jul 06, 2014   Reply
                                            Thanks.
                                            • By Modifikasi Motor Satria FU - 6:34 AM on Jul 13, 2014   Reply
                                              Cindy’s Shoes, a present day Cinderella which I am transcription because although I wrote it in 1988 at the age of 20 it’s still appropriate.
                                              • By the venus factor review - 9:11 AM on Jul 15, 2014   Reply
                                                We were helped by Jerr McKinnon, administrator of coaching and tech support team, and Andrew Richards, item instructor, when using the resources and operating on set ups.
                                                • By url opener - 6:23 PM on Jul 15, 2014   Reply
                                                  This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work...
                                                  • By duplichecker - 8:00 PM on Jul 18, 2014   Reply
                                                    woah! I’m really enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that perfect balance between superb usability and visual appeal. I must say you’ve done a very good job with this.

                                                    ADD A COMMENT