9

I am not sure how I could achieve the following using Velocity Template Language.

Essentially, I'd like to check if a given value exists from the given list/array. In C#, it's equivalent to .Any() or .Contains() method.

#set($myValues = ["apple", "banana", "strawberry"])
#foreach($i in $items)
    $myValues.any($i) <= better way to achieve this??
    or something like this
    $myValues.contains($i)
#end

Of course, I could just use multiple if...else... conditions and loop through the list to check the existence of a certain value, but the code gets messy very quickly (if a lookup list is huge). Is there any better way to do this?

woodykiddy
  • 6,074
  • 16
  • 59
  • 100
  • `$myValues.contains($i)` should work just fine as velocity can interpret such method calls, as long as they are java – XtremeBaumer Aug 22 '19 at 07:38

1 Answers1

13
$myValues.contains($i)

or

$list.contains($myValues, $i)

(this one is deprecated since the former was introduced in Velocity 1.6)

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142