velocity: do something except in last loop iteration
You can use a test if you are in last iteration:: #foreach( $item in $list ) $item.text #if( $foreach.hasNext ), #end #end
You can use a test if you are in last iteration:: #foreach( $item in $list ) $item.text #if( $foreach.hasNext ), #end #end
I designed ST to build jGuru after getting sick of the “code in template” model of JSP. Velocity and friends (i.e., every other engine i think) give you more power than you need. I used basically four features to build jGuru.com (as described in paper). More features are unnecessary and lead you to entangle your … Read more
There is no instanceof, but you can get class name as a string and then go from there: ${myObj.class.name} would return “com.test.MyObj” ${myObj.class.simpleName} would return “MyObj”
You can invoke standard java methods on these objects. If s_attribute.name is type String you can directly use $s_attribute.name.toUpperCase() or for your specific case use $s_attribute.name.substring(0,1).toUpperCase() and $s_attribute.name.substring(1).toLowerCase()
$!a does the trick. You can use this form directly without an if check.
There’s only #foreach. You’ll have to put something iterable on your context. E.g. make bar available that’s an array or Collection of some sort: #foreach ($foo in $bar) $foo #end Or if you want to iterate over a number range: #foreach ($number in [1..34]) $number #end
@Vartec: I don’t think that the “strict separation of view from business logic” is a velocity feature that is not present in jsp. You can do business logic in jsp (more or less) but it’s not recommended at all. But I agree in your point regarding the syntax. Performance JSP is compiled to Java, so … Read more
#if($hideMyControl) // your code #end If $hideMyControl is defined, your code will execute
If you just want Velocity to display the value if there, or display nothing if absent, a quiet reference by itself will do the trick: $!incentive.disclaimer If you’re wanting to explicitly test for empty, StringUtils from Apache Commons Lang can help. First add it to your Context (reference here): context.put(“StringUtils”, StringUtils.class); Though if you’re on … Read more