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?
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.
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.
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)
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.
not found! Use PHP in_array () function to check if a value exists in an array. Did you find this tutorial useful?
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.
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 ...
$myValues.contains($i)
or
$list.contains($myValues, $i)
(this one is deprecated since the former was introduced in Velocity 1.6)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With