如何在freeMarker中动态访问指定的变量

时间:2018-05-17 14:07:07

标签: html apache html-email freemarker email-templates

<#assign U="Unknown" B="both" M="male" F="female">

<div>
<span>     ${transaction.addressTransactions.addresses.newInfo.GenderTypeCode.code} </span>
</div>
  • transaction.addressTransactions.addresses.newInfo.GenderTypeCode.code =这将输出U / B / M / F.我想使用此输出并访问相应的已分配变量。
  • 例如,如果transaction.addressTransactions.addresses.newInfo.GenderTypeCode.code返回U,我想访问指定的变量并获取U的值“Unknown”。我怎么能在Freemarker中做到这一点?

1 个答案:

答案 0 :(得分:1)

你可以像.vars[myDynamicVariableName]那样做,但是,你可能不需要那样做。您可以使用<#assign codeToLabel = { "U": "Unknown", "B": "both", "M": "male", "F": "female" }>然后使用codeToLabel[myDynamicCode]