hiway
hiway

Reputation: 3976

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 ?

Upvotes: 13

Views: 16632

Answers (3)

WJCarpenter
WJCarpenter

Reputation: 35

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>

Upvotes: 1

dr_hanns
dr_hanns

Reputation: 111

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>&lt;${object?node_name}<#if object?node_type=="element" && object.@@?has_content><#list object.@@ as attr>
                        ${attr?node_name}="${attr?html}"</#list></#if>&gt;
                        <#if object?children?has_content><#list object?children as item>
                            <@dump_object object=item/></#list><#else>${object}</#if> &lt;/${object?node_name}&gt;</#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.

Upvotes: 5

ddekany
ddekany

Reputation: 31152

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.

Upvotes: 33

Related Questions