I'm trying to create a macro to alphabetically order a section of lines:
<#order_lines>
import B
import A
</#order_lines>
This should output
import A
import B
The way I try to implement:
<#macro order_lines>
${myUtil.orderLines(<#nested>)}
</#macro>
There's a problem with the macro; syntax error at '<#nested>'
Encountered "<", but was expecting one of: ...
You can assign it to a variable:
<#macro order_lines>
<#local x><#nested></#local>
${myUtil.orderLines(x)}
</#macro>
Another option is to imlement TemplateDirectiveModel and use it the same way you would use a macro.
Related
I am working on looping through one object with multiple attributes. In my scenario, I am looking for external content values.
email_address
article01
article02
article03
email#address.com
Y
Y
These values can change all the time, so we have to define them manually every instance where we use this, but I want to be able to list them in a sequence and then loop through and include them when object.attribute=Y.
The below block is purely conceptual, but referencing the attribute within the expression is where I get confused.
<#assign seq = ['article01','article02','article03']>
<#list seq as articles>
<#if base.${article}="Y">
<#include "*/${article}.htm"/>
</#if>
</#list>
In this instance, the resulting code would be:
<html><article01.html content></html>
<html><article03.html content></html>
Assuming base.articel01 would work, you can use base[article] (instead of base.${article}).
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.
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>
<#assign reasonValue="xxx.ftl">
and I call it like:
<#include "${reasonValue}">
and I get output like:
Rejected - Something
how can I now use split on this ouput because I would like to get just Something as output
I tried:
<#list "${reasonValue}"?split("-") as sValue>
${sValue}
</#list>
but problem is that instead of real value i get name of ftl file...
Assign output of include to some variable and then use split on this variable.
<#assign xx>
<#include reasonValue>
</#assign>
<#list xx?split("-") as sValue>
${sValue}
</#list>
If you need to show only part of the string after "-" then use substring and index_of.
${xx?substring(xx?index_of("-") + 2)}