Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to DUMP object in freemarker ( .ftl )

Tags:

freemarker

is there a way how to dump whole object and write it somewhere?

Like: var_dump() in php console.log in JS

I found something like list, so I try something like this below:

<#list calculation as c>
${c}
</#list>

But template fall with error. I appriciate any advise!

like image 774
Andurit Avatar asked Mar 13 '23 02:03

Andurit


1 Answers

It depends on the type of object you are iterating through. You can check the type of data your variable is and then output it appropriate (Reference: http://freemarker.incubator.apache.org/docs/ref_builtins_expert.html#ref_builtin_isType)

Here are some examples:

<#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>

Take a look at https://github.com/ratherblue/freemarker-debugger/blob/master/debugger.ftl for more examples on dumping data

like image 59
ratherblue Avatar answered Mar 19 '23 01:03

ratherblue