【发布时间】:2016-06-12 16:01:00
【问题描述】:
有没有办法转储整个对象并将其写入某处?
喜欢:
var_dump() 在 php 中
JS中的console.log
我发现了类似list 的东西,所以我尝试了以下类似的东西:
<#list calculation as c>
${c}
</#list>
但是模板出错了。我欢迎任何建议!
【问题讨论】:
标签: freemarker
有没有办法转储整个对象并将其写入某处?
喜欢:
var_dump() 在 php 中
JS中的console.log
我发现了类似list 的东西,所以我尝试了以下类似的东西:
<#list calculation as c>
${c}
</#list>
但是模板出错了。我欢迎任何建议!
【问题讨论】:
标签: freemarker
这取决于您正在迭代的对象的类型。您可以检查您的变量的数据类型,然后适当地输出(参考:http://freemarker.incubator.apache.org/docs/ref_builtins_expert.html#ref_builtin_isType)
这里有一些例子:
<#if calculation?is_sequence>
<#list calculation as c>
${c}
</#list>
<#elseif calculation?is_hash_ex>
<#list calculation?keys as key>
${key} - ${calculation[key]}
</#list>
<#elseif calculation?is_string>
${calculation}
</#if>
查看https://github.com/ratherblue/freemarker-debugger/blob/master/debugger.ftl 了解更多关于转储数据的示例
【讨论】:
${key} - ${calculation[key]} 行实际上可能会产生错误,如果计算 [key] 是复杂的,我用它来写出密钥并使用所有这些 if 检查再次转储值