How to DUMP object in freemarker ( .ftl ) - 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!

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

Related

How can I create dynamic imports in Freemarker?

I want create dynamics imports in freemarket based in a previous list I have created...
<#if filesList??>
<#list filesList as specificFile>
<#import ${specificFile} + ".ftlh" as ${specificFile}>
</#list>
<#else>
NO FILES
</#if>
Always giving me an error... How can I use it?
Thank you

filter freemarker output by hash

<#list reports as report>
<#list report.transactionList as expense>
${expense.transactionID}^<#t>
${table[expenses.transcationID}
<#if expense.modifiedCreated?has_content>
${expense.modifiedCreated}^<#t>
<#else>
${expense.created}^<#t>
</#if>
In the above code I have a hash table called table and I want to use expense.transactionID as the key to then load the table's value like in the above code.
when I run it, the second item instead of a value is just a blank spot.
figured it out. {table[expenses.transcationID} needs to cast the category I am using as the key into a string. so the answer is: {table[expenses.transcationID?string}

Freemarker - How to put variables in hashmap?

I searched the web to find how to add some entries into an existing hashmap.
Consider this code:
<#assign foo={'bar':'go'}>
I want to add a new entry and have something like this:
foo={'bar':'go','new':'entry}
How can I do that?
Using concatenation:
<#assign foo=foo+{'new':'entry'}>
print the hashmap:
<#list foo?keys as k>
${k}: ${foo[k]} <br>
</#list>
The result is exactly what you want:
bar: go
new: entry
D.

Freemarker assign value to variable

I'm having trouble while trying to assign value to a variable using Freemarker.
<#if size??>
<#assign num=size?number>
<#if (num>0)>
<#list 0..num-1 as i>
<#if .vars['abc'+i?c] = "test">
<#assign .vars['abc'+i?c] = .vars['abc'+i?c]?replace("test","Test")>
</#if>
</#list>
</#if>
This is the error message: Encountered ".", but was expecting one of:
STRING_LITERAL
RAW_STRING
ID
Can anyone help me with this?
Thank you.
You can only write top-level variables in a FreeMarker template. Also you can't assign to a variable with dynamically constructed name, except with an ?interpret hack: <#"<#assign abc${i?c} = abc${i?c}?reaplce('test', "Test")>"?interpret />. Obviously that's horrid... BTW, what's the use case here? Why do you need to assign to dynamically constructed variable names?

does freemarker support show all variable in data-model?

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.
An even more detailed way would be this macro:
<#macro dump_object object debug=false>
<#compress>
<#if object??>
<#attempt>
<#if object?is_node>
<#if object?node_type == "text">${object?html}
<#else><${object?node_name}<#if object?node_type=="element" && object.##?has_content><#list object.## as attr>
${attr?node_name}="${attr?html}"</#list></#if>>
<#if object?children?has_content><#list object?children as item>
<#dump_object object=item/></#list><#else>${object}</#if> </${object?node_name}></#if>
<#elseif object?is_method>
#method
<#elseif object?is_sequence>
[<#list object as item><#dump_object object=item/><#if !item?is_last>, </#if></#list>]
<#elseif object?is_hash_ex>
{<#list object as key, item>${key?html}=<#dump_object object=item/><#if !item?is_last>, </#if></#list>}
<#else>
"${object?string?html}"
</#if>
<#recover>
<#if !debug><!-- </#if>LOG: Could not parse object <#if debug><pre>${.error}</pre><#else>--></#if>
</#attempt>
<#else>
null
</#if>
</#compress>
</#macro>
<#dump_object object=.data_model/>
This gives you a full dump of your data model.
Here is #lemhannes macro definition modified to emit JSON. Lightly tested on a fairly simple datamodel
<#macro dump_object object debug=false>
<#compress>
<#if object??>
<#attempt>
<#if object?is_node>
<#if object?node_type == "text">${object?json_string}
<#else>${object?node_name}<#if object?node_type=="element" && object.##?has_content><#list object.## as attr>
"${attr?node_name}":"${attr?json_string}"</#list></#if>
<#if object?children?has_content><#list object?children as item>
<#dump_object object=item/></#list><#else>${object}</#if>"${object?node_name}"</#if>
<#elseif object?is_method>
"#method"
<#elseif object?is_sequence>
[<#list object as item><#dump_object object=item/><#if !item?is_last>, </#if></#list>]
<#elseif object?is_hash_ex>
{<#list object as key, item>"${key?json_string}":<#dump_object object=item/><#if !item?is_last>, </#if></#list>}
<#else>
"${object?string?json_string}"
</#if>
<#recover>
<#if !debug>"<!-- </#if>LOG: Could not parse object <#if debug><pre>${.error}</pre><#else>-->"</#if>
</#attempt>
<#else>
null
</#if>
</#compress>
</#macro>

Resources