Freemarker迭代hashmap键 [英] Freemarker iterating over hashmap keys

查看:151
本文介绍了Freemarker迭代hashmap键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Freemarker有两个集合数据类型,列表和哈希映射
有没有办法迭代hashmap键,就像我们对列表一样?

Freemarker has two collection datatypes, lists and hashmaps Is there a way to iterate over hashmap keys just as we do with lists?

所以如果我有一个数据的var让我们说:

So if I have a var with data lets say:

user : {
  name : "user"
  email : "looser@everything.com"
  homepage : "http://nosuchpage.org"
}

我想用它们的值打印所有用户的属性。
这是无效的,但目标很明确:

I would like to print all the user's properties with their value. This is invalid, but the goal is clear:

<#list user.props() as prop>
  ${prop} = ${user.get(prop)}
</#list>


推荐答案

编辑: Don'使用此解决方案与FreeMarker 2.3.25及更高版本,特别是 .get(prop)。请参阅其他答案。

Don't use this solution with FreeMarker 2.3.25 and up, especially not .get(prop). See other answers.

您使用内置的功能,例如这应该工作:

You use the built-in keys function, e.g. this should work:

<#list user?keys as prop>
    ${prop} = ${user.get(prop)}
</#list>  

这篇关于Freemarker迭代hashmap键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆