0

我想知道是否有可能类似于以下数组:

#set ( $foo1 = [ 
  $title, 'some title', 
  $text, 'some text'
]

#set ( $foo2 = [ 
  $title, 'different title', 
  $text, 'different text'
]

然后循环这样的代码

#foreach($i in [1..2])

<div id="$i">
  <h1>$foo$i.title<h1>
  <p>$foo$i.text</p>
</div>

#end

我似乎有两个问题我无法找到解决方案。我已经能够设置一个数组,但我只成功地使用了数字索引而不是变量名。

我的第二个问题可能与 Velocity 只解析一次有关。即使是一个简单的例子,如:

#set ( $foo1 = "bar" )
#set ( $foo2 = "bar2" )

#foreach($i in [1..2])
  $foo$i ---
#end

只会解析为 $foo1 --- $foo2

是否有可能做我希望实现的事情,以及我如何能够调整我的代码。如果我只需要在我的数组中使用一个数字索引就可以了,所以我想更紧迫的问题是如何通过每个循环动态更改变量。

4

1 回答 1

0

在找到几个具有 Velocity 知识的人之后,我能够破解这个问题。

#set ( $foo1 = { 
  "title": "some title", 
  "text": "some text"
]

#set ( $foo2 = [ 
  "title" "different title", 
  "text", "different text"
]

#set ( $foos = [$foo1, $foo2] )

#foreach($foo in $foos)

<div id="$velocityCount">
  <h1>$title<h1>
  <p>$text</p>
</div>

#end
于 2013-09-30T15:47:31.060 回答