Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if value exists in an array using Velocity Template Language

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?

like image 364
woodykiddy Avatar asked Aug 22 '19 07:08

woodykiddy


People also ask

How do I test a Velocity template language?

Approach 1: The obvious approach: Run and check. Run Velocity against the template to be checked. Look at the output and see if it is what you desired. This must be the most primitive testing approach, but most people nowadays are too lazy and want this done by the computer.

What is Velocity template language?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.

How do you use Velocity in HTML?

In the code you posted: there 2 ways to run velocity: running the main (but this is not what you do when you build your project) running ant form but since you don't fill any velocity context: velocity won't be able to replace the variable in your template (i.e. the generated html won't contains dynamic data)

How to access elements of a list in velocity?

However the safest way to access elements of a list is using foreach loops. Show activity on this post. Creating an array is easy: Getting an element from an array depends from your Velocity version.

How to check if a value exists in an array?

not found! Use PHP in_array () function to check if a value exists in an array. Did you find this tutorial useful?

How to test for NULL values in velocity context?

Use another value (e.g. -1) representing null values. Since velocity context works as a map, get (name) will return null when a parameter is missed as well as when it is set as null. Thanks for the reply, however I did find that you can test for a NULL with a Velocity #if statement and it works.

How to check for a value in an array of objects?

Two array methods to check for a value in an array of objects 1 Array.some ()#N#The some () method takes a callback function, which gets executed once for every element in the array... 2 Array.find () More ...


1 Answers

$myValues.contains($i)

or

$list.contains($myValues, $i)

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

like image 182
Andrew Tobilko Avatar answered Oct 11 '22 08:10

Andrew Tobilko