简体   繁体   English

如何使用freemarker遍历变量名

[英]How to loop through variable names using freemarker

I have a few variables that i passed from Java to FTL file. 我有一些从Java传递到FTL文件的变量。 eg variable1, variable2, variable 3 例如变量1,变量2,变量3

How can i retrieve the value for these variables using a loop? 如何使用循环检索这些变量的值? Below is the sample code. 下面是示例代码。 It is not working correctly though. 它不能正常工作。

<#assign x=3>
<#list 1..x as i>
  ${abc+1}
</#list>  

Thank you. 谢谢。

Dynamic variable names can be used through special hash variable .var (and .data_model ) combined with the [] operator: 动态变量名可以通过特殊的哈希变量.var (和.data_model )与[]运算符结合使用:

<#assign x=3>
<#list 1..x as i>
  ${.vars['abc'+ i?c]}
</#list>  

For me, it worked like this: 对我来说,它像这样工作:

<#assign x=3>
<#list 1..x as i>
     variable_name:${.vars['variable'+i]}
</#list>

Output: 输出:

variable_name: (value of variable1) variable_name: (value of variable2) variable_name: (value of variable3) variable_name :(变量1的值)variable_name :(变量2的值)variable_name :(变量3的值)

If you just want to print variable1, variable2, variable3 如果只想打印variable1,variable2,variable3

<#assign x=3>
<#list 1..x as i>
     ${'variable'+i}
</#list>

Output: variable1 variable2 variable3 输出:变量1变量2变量3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM