Skip to content

Instantly share code, notes, and snippets.

@bmvakili
Forked from jamesfalkner/liferay_dump.ftl
Last active February 14, 2018 09:00
Show Gist options
  • Save bmvakili/b5d871b4cbc4ebbbe0a0 to your computer and use it in GitHub Desktop.
Save bmvakili/b5d871b4cbc4ebbbe0a0 to your computer and use it in GitHub Desktop.
Dump Liferay 7 GA4 variables in Freemarker - adapted from James Falkner's original
<#attempt>
<#assign black_list = ["class", "request", "getreader", "getinputstream", "writer"] />
<#macro dump key data>
<#if data?is_enumerable>
<p><b>${key}</b>
<@printList data,[] />
<#elseif data?is_hash_ex>
<p><b>${key}</b>
<@printHashEx data,[] />
<#else>
<p><@printItem data?if_exists,[], key, false /></#if>
</#macro>
<#macro printList list has_next_array>
<#local counter=0 />
<#list list as item>
<#list has_next_array+[true] as has_next><#if !has_next> <#else> | </#if></#list>
<#list has_next_array as has_next><#if !has_next> <#else> | </#if></#list><#t>
<#t><@printItem item?if_exists,has_next_array+[item_has_next], counter, false />
<#local counter = counter + 1/>
</#list>
</#macro>
<#macro printHashEx hash has_next_array>
= <code>${(hash.data!(hash.toString()))!"No String Representation"}</code>
<ul>Callable methods:
<ul>
<#list hash?keys as key>
<#list has_next_array+[true] as has_next><#if !has_next> <#else> | </#if></#list>
<#list has_next_array as has_next><#if !has_next> <#else> | </#if></#list><#t>
${key}
</#list>
</ul>
</ul>
</#macro>
<#macro printHashExFull hash has_next_array>
<#list hash?keys as key>
<#list has_next_array+[true] as has_next><#if !has_next> <#else> | </#if></#list>
<#list has_next_array as has_next><#if !has_next> <#else> | </#if></#list><#t>
<#t><#attempt><@printItem hash[key]?if_exists,has_next_array+[key_has_next], key, true /><#recover><pre>${.error}</pre></#recover>
</#list>
</#macro>
<#macro printItem item has_next_array key full>
<#if item?is_method>
+- <b>${key}</b> = ?? (method)
<#elseif item?is_enumerable>
+- <b>${key}</b>
<@printList item, has_next_array /><#t>
<#elseif item?is_hash_ex && omit(key?string)><#-- omit bean-wrapped java.lang.Class objects -->
+- <b>${key}</b> (omitted)
<#elseif item?is_hash_ex>
+- <b>${key}</b>
<#if full>
<@printHashExFull item, has_next_array /><#t>
<#else>
<@printHashEx item, has_next_array /><#t>
</#if>
<#elseif item?is_number>
+- <b>${key}</b> = <code>${item}</code>
<#elseif item?is_string>
+- <b>${key}</b> = <code>"${item}"</code>
<#elseif item?is_boolean>
+- <b>${key}</b> = <code>${item?string}</code>
<#elseif item?is_date>
+- <b>${key}</b> = <code>${item?string("yyyy-MM-dd HH:mm:ss zzzz")}</code>
<#elseif item?is_transform>
+- <b>${key}</b> = ?? (transform)
<#elseif item?is_macro>
+- <b>${key}</b> = ?? (macro)
<#elseif item?is_hash>
+- <b>${key}</b> = ?? (hash)
<#elseif item?is_node>
+- <b>${key}</b> = ?? (node)
</#if>
</#macro>
<#function omit key>
<#local what = key?lower_case>
<#list black_list as item>
<#if what?index_of(item) gte 0>
<#return true>
</#if>
</#list>
<#return false>
</#function>
<h1>All Liferay Variables</h1>
<em>See the bottom for the <code>request</code> variable expansion</em>
<hr>
<#list .data_model?keys as var>
<#if var?index_of("writer") lt 0>
<@dump var,.data_model[var] />
</#if>
</#list>
<hr>
<h1>The <code>request</code> object</h1>
<pre>
<@printHashExFull request,[] />
</pre>
<#recover>
<pre>
${.error}
</pre>
</#recover>
@caneta
Copy link

caneta commented Feb 13, 2018

Great Job!
I've tryied to insert these script inside a freemarker portlet, but it breaks at line 108 reporting the following error:

hen calling macro "printHashExFull", required parameter "hash" (parameter #1) was specified, but had null/missing value.

----
Tip: If the parameter value expression on the caller side is known to be legally null/missing, you may want to specify a default value for it with the "!" operator, like paramValue!defaultValue.
----

If I comment out that line, the script works...so: how to make request variable available in that context?

@caneta
Copy link

caneta commented Feb 14, 2018

A way I found to get it work is substituting line 108 with

<@printHashExFull themeDisplay.getRequest(), [] />

even if the printed object is really huge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment