Use hash tables

c-siems's Avatar

c-siems

02 Aug, 2016 07:53 PM

With the absence of labels in radio-items of data definitions, I wondered if I could use a hash table in Velocity to translate radio-item values into CSS class names?

For example, if the XML is
<category>Security Update</category>

Using key -> value notation, the hash table would be

'Security Update' -> 'update-top-border'

What would the velocity be to output the HTML?

<div class="callout update-top-border" data-equalizer-watch>

  1. 1 Posted by Wing Ming Chan on 02 Aug, 2016 09:47 PM

    Wing Ming Chan's Avatar

    Hi,

    Assuming that you can retrieve the data, say, using the Locator tool, you can create a hashmap in a format, to be included and consumed by another format. See https://github.com/wingmingchan/velocity/blob/master/tutorials/01%2... for an example on how to use methods like putAll and get. Also see http://www.upstate.edu/cascade-admin/formats/velocity/api-documenta... and https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html for the API documentation.

    Wing

  2. 2 Posted by Ryan Griffith on 03 Aug, 2016 12:34 PM

    Ryan Griffith's Avatar

    Hi,

    If you're dealing with a small subset and don't mind maintaining the values in an additional place, you can go simple and define your map as such:

    #set ($myHashMap = {
      "Security Update": "update-top-border",
      "Radio Choice": "css-class",
      ...
    })
    

    You would then use that map:

    #set ($radioValue = ...)
    #set ($cssClassName = $myHashMap.get($radioValue))
    #if (!$_PropertyTool.isNull($cssClassName))
      <div class="callout ${cssClassName}" data-equalizer-watch>
        ...
      </div>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  3. 3 Posted by c-siems on 03 Aug, 2016 01:21 PM

    c-siems's Avatar

    Wing, thanks for the resources. Your Velocity tutorials are easy enough for me to understand.

    Thanks Ryan, I think that's what I need.

  4. 4 Posted by Ryan Griffith on 03 Aug, 2016 01:28 PM

    Ryan Griffith's Avatar

    Not a problem at all!

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  5. Ryan Griffith closed this discussion on 03 Aug, 2016 01:28 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac