Velocity #foreach does not work as expected with a string passed as $ value variable

I have not used Velocity recently, but something that I definitely used to work now doesn’t work - the only difference I can think of since I last used it, and now we updated it with v1. 6.4 to v1.7.

In our Java code, we add values ​​to the script context, which is a comma-delimited string along the lines;

context.put("value", "'a','b','c'")

(this has been simplified as an example).

In my speed code, if I say things like this:

$value -- it prints 'a','b','c'
$value.split(",")[0] -- it prints 'a'
$value.split(",").size() -- it prints 3

However, if I try to say

#foreach ( $val in $value.split(",") )
  -- $val
#end

He doesn't print anything. In fact, if I say

#set ( $val = $value.split(",") ) 
#if ( ! $val ) 
  -- print null message here
#end

It goes into the if block and says $ val is null.

At some point, I thought that I would be smart and do something like the following

#set ( $count = $value.split(",").size() )
#foreach ( $item in [0..$count] )
   -- reference $value.split(",")[$item]
#end

RHS $.

, :

#set ( $value = "'a','b','c'" )
#foreach ( $val in $value.split(",") ) 
  -- Write out $val
#end

, . - , .

- , ? ?

,

p.s.

+3
1

foreach , :

context.put("value", Arrays.asList("a", "b", "c"));
0

All Articles