如何在 freemarker ( .ftl ) 中转储对象

And*_*rit 4 freemarker

有没有办法如何转储整个对象并将其写在某个地方?

像: var_dump()在 php console.log中的 JS

我找到了类似的东西list,所以我在下面尝试了类似的东西:

<#list calculation as c>
${c}
</#list>
Run Code Online (Sandbox Code Playgroud)

但是模板会出错。我appriciate任何建议!

rat*_*lue 6

这取决于您正在迭代的对象类型。您可以检查变量的数据类型,然后适当地输出它(参考: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>
Run Code Online (Sandbox Code Playgroud)

有关转储数据的更多示例,请查看https://github.com/ratherblue/freemarker-debugger/blob/master/debugger.ftl