Is there a way to call a function within a Freemarker macro?
Example of what I am trying to do:
<#function foo>
<return "hello">
</#function>
<#macro fooMacro>
${foo()}
</#macro>
<#fooMacro> <#-- This doesn't work for me
"The "foo()" has evaluated to null or missing" -->
Similarly, is there a way to pass the output of a function into a param of a macro? Like:
<#macro fooMacro p1>
${p1}
</#macro>
<#fooMacro p1=foo()/> <!-- Also doesn't seem to work for me -->
Answer: both are possible, just change
<return "hello"> ==> <#return "hello">
Related
I want to output tags around a <#nested> directive in a macro, but only if it would actually output something. The actual use case is more complicated, this is just the broken down version.
How do I check for existence of <#nested> content?
<#macro opt tagname>
<#if (#nested)??> <-- what do I need to put here
<${tagname}>
<#nested>
</${tagname}>
</#if>
</#macro>
Example 1
Template: <#opt hello />
Output: (empty)
Example 2
Template: <#opt hello>goodbye</#opt>
Output: <hello>goodbye</hello>
You have to capture the nested content, and then print it if necessary. Like this (this assumes auto-escaping on):
<#macro opt tagname>
<#local nestedContent><#nested></#local>
<#if nestedContent?has_content>
<${tagname}>${nestedContent}</${tagname}>
</#if>
</#macro>
Without auto-escaping the #if changes to just <#if nestedContent != ''>.
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?
How can I call nested macros as such?
<#replaceA-sToB-s>
<#replaceB-sToC-s Text/>
</#replaceA-sToB-s>
replaceB-sToC-s simply replaces any "B" with "C"
replaceA-sToB-s simply replaces any "A" with "B"
If you want to pass the result of replaceB-sToC-s macro to the replaceA-sToB-s then you have to use something like this:
<#assign str><#replaceB-sToC-s "abc" /></#assign>
<#replaceA-sToB-s str />
Like this:
<#macro replaceAsToBs>
<#local captured><#nested></#local>
${captured?replace('a', 'b')}<#t>
</#macro>
<#macro replaceBsToCs text>
${text?replace('b', 'c')}<#t>
</#macro>
(The #t-s are only there to remove the extra whitespace around the ${...} parts.) And then you call it like:
<#replaceAsToBs>
<#replaceBsToCs "abcd"/>
</#replaceAsToBs>
When I receive some data from the back end, for example the data string 'AbcDef', does freemarker have some build-in function to parse the string to 'ABC_DEF'?? How can I do with the freemarker template language?
Like this:
<#function camelToDashed(s)>
<#return s
<#-- "fooBar" to "foo_bar": -->
?replace('([a-z])([A-Z])', '$1_$2', 'r')
<#-- "FOOBar" to "FOO_Bar": -->
?replace('([A-Z])([A-Z][a-z])', '$1_$2', 'r')
<#-- All of those to "FOO_BAR": -->
?upper_case
>
</#function>
${camelToDashed('AbcDef')}
Of course it can done without #function too, it's just easier to reuse it this way.
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>