freemarker支持在数据模型中显示所有变量吗? [英] does freemarker support show all variable in data-model?

查看:265
本文介绍了freemarker支持在数据模型中显示所有变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在freemarker数据模型中查看所有变量,就像struts2 debug标签显示值堆栈一样.

freemarker有办法做到这一点吗?

解决方案

没有通用的解决方案,但是您可以尝试

<#list .data_model?keys as key>
  ${key}
</#list>

如果数据模型只是普通的Map或JavaBean,这将起作用,但对于更复杂的数据模型,如果它支持?keys且确实返回了所有内容,则取决于数据模型的实现.

您还具有在模板中设置的变量,可以像上面列出的那样,仅使用.globals.namespace(表示当前模板名称空间)和.locals代替.data_model. /p>

您可能还具有Configuration级共享变量,并且无法单纯从FTL列出这些变量(您可以为其编写自定义的TemplateMethodModel,尽管它读取Configuration.getSharedVariableNames()并从模板中调用它)

当然,理想情况下,FreeMarker应该具有<#show_variables>指令或某种指令,可以尽最大努力显示所有这些信息……但可惜的是还没有这种东西.

I want to see all variables in freemarker data-model, just like struts2 debug tag to show value stack.

Is there a way for freemarker to do this ?

解决方案

There's no universal solution possible for that, but you can try

<#list .data_model?keys as key>
  ${key}
</#list>

This works if the data-model is just a usual Map or JavaBean, but for more sophisticated data-models it's up to the data-model implementation if it supports ?keys and if it indeed returns everything.

You also have the variables that you set in the templates, which can be listed like above, only instead of .data_model use .globals, .namespace (which means the current template namespace) and .locals.

You may also have Configuration-level shared variables, and there's no way to list those purely from FTL (you could write a custom TemplateMethodModel for it that reads Configuration.getSharedVariableNames() though, and call it from the template).

Of course, ideally, FreeMarker should have a <#show_variables> directive or something, that does a best effort to show all this... but sadly there is no such thing yet.

这篇关于freemarker支持在数据模型中显示所有变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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