“for” loop in velocity template
Try to do it like this: #set($start = 0) #set($end = 4) #set($range = [$start..$end]) #foreach($i in $range) doSomething #end The code has not been tested, but it should work like this.
Try to do it like this: #set($start = 0) #set($end = 4) #set($range = [$start..$end]) #foreach($i in $range) doSomething #end The code has not been tested, but it should work like this.
I’ve never used Velocity, but its VTL reference guide says that calling a method is done using $customer.getAddress() or ${purchase.getTotal()}. So I would use ${myArrayList.size()}.
You can do #set($value = “#if($flag)red#{else}blue#end”)
By default you can use the methods of the Java String object: #set( $a = “Hello” ) #set( $b = $a.replace(“l”, “+”) ) ${b} will produce He++o and you can also use velocity variables as arguments to your method calls, e.g.: #set( $a = “Hello” ) #set( $b = “+” ) #set( $c = $a.replace(“l”, … Read more
You can mark variables as “silent” like this: $!variable If $variable is null, nothing will be rendered. If it is not null, its value will render as it normally would.
Aha! Been there. #set($intString = “9”) #set($Integer = 0) $Integer.parseInt($intString) Doing this uses the java underlying velocity. The $Integer variable is nothing more that a java Integer object which you can then use to access .parseInt Edit: The above code is for demonstration. Of course there are ways to optimize it.
If you want to use resources from classpath, you should use resource loader for classpath: ve.setProperty(RuntimeConstants.RESOURCE_LOADER, “classpath”); ve.setProperty(“classpath.resource.loader.class”, ClasspathResourceLoader.class.getName()); ve.init();
Your mistake is referring to key and value as methods (with trailing “()” parenthesis) instead of as properties. Try this: #set ($map = $myobject.getMap() ) #foreach ($mapEntry in $map.entrySet()) <name>$mapEntry.key</name> <value>$mapEntry.value</value> #end In other words, use either a property, like mapEntry.key, or the method, like mapEntry.getKey().
To check if a variable is not null simply use #if ($variable) #if ($variable) … do stuff here if the variable is not null #end If you need to do stuff if the variable is null simply negate the test #if (!$variable) … do stuff here if the variable is null #end
this: #[[ ## ]]# will yield: ## anything within #[[ … ]]# is unparsed.