Freemarker-Macros: How to use a macro twice? - freemarker

I am currently working on some contracts and the my freemarker macros are working fine. But now I have to use the same macro twice and I'm getting the report: Expected a hash, but this evaluated to a number
It seems the same macro cant do the same thing twice, the macro I am currently using is:
[#macro TeilbetragProzent]
[#assign gesamt = ((verkaufsauftrag.beteiligungGesamt)!"") /]
[#assign nominale = ((nominale.betrag)!"")/]
[#if (gesamt?string)!=""]
[#assign prozent = ((nominale/gesamt)*100)/]
${(prozent)?string["0.##"]}
[#else]100
[/#if]
[/#macro]
I tried to change it a little:
[#macro TeilbetragProzent2]
[#assign gesamt2 = ((verkaufsauftrag.beteiligungGesamt)!"") /]
[#assign nominale2 = ((nominale2.betrag2)!"")/]
[#if (gesamt2?string)!=""]
[#assign prozent2 = ((nominale2/gesamt2)*100)/]
${(prozent2)?string["0.##"]}
[#else]100
[/#if]
[/#macro]
Still not working... Am i missing something?

Rather than an issue in the macro, I believe your issue lies in the second assign directive: [#assign nominale = ((nominale.betrag)!"")/]. You are trying to assign the value of the node betrag in the hash variable nominale (likely number, based on the error message) to a variable of the same name, nominale. If you change the variable name to something, I think you will have success.
[#macro TeilbetragProzent]
[#assign gesamt = ((verkaufsauftrag.beteiligungGesamt)!"") /]
[#assign nom = ((nominale.betrag)!"")/]
[#if (gesamt?string)!=""]
[#assign prozent = ((nom/gesamt)*100)/]
${(prozent)?string["0.##"]}
[#else]100
[/#if]
[/#macro]

Related

how to use a variable from another .ftl in freemarker

I'm trying to use a defined variable in a second template to have the same output in the current one.
template 1:
[#if definition.name=="configMINIMAL_STACK_SIZE"]
[#assign valueMinimalStackSize = definition.value]
[/#if]
Second template:
#define configMINIMAL_STACK_SIZE ((uint16_t)${valueMinimalStackSize})
how could I have the same output of "valueMinimalStackSize " in the second template please ?
Thanks for the help
You could have a template that sets these variables, let's call it "commons.ftl", and then use <#include "commons.ftl"> at the beginning of other templates.

How to properly group records when executing a <#list> for a Saved Search

New to the forum but have been working with NetSuite for about 3 years and using this site as a resource. I was hoping someone could help me understand the answer to the following question, How to properly group records when executing a <#list>, so that I could adapt it to be used when pulling a saved search for items?
Thanks for the quick response! I've been trying to use the code you provided as a template and hoped you could take a quick look. For the macro, I modified as follows.
<#macro listGroups results groupField>
<#if results?size == 0><#return></#if>
<#local sortedResults = results?sort_by(groupField)>
<#local groupStart = 0>
<#list sortedResults as result>
<#if !result?is_first && result[groupField] != lastResult[groupField]>
<#local groupEnd = result?index>
<#nested lastResult[groupField], sortedResults[groupStart ..< groupEnd]>
<#local groupStart = groupEnd>
</#if>
<#local lastResult = result>
</#list>
<#local groupEnd = sortedResults?size>
<#nested lastResult[groupField], sortedResults[groupStart ..< groupEnd]>
</#macro>
When I used the macro later, I modified as follows.
<#listGroups record.result "result.class"; groupName, groupResults>
<p>${groupName}</p>
<table>
<#list groupResults as groupResult>
<tr>
<td>${groupResult.itemid}</td>
<td>${groupResult.salesdescription}</td>
<td>${groupResult.othervendor}</td>
<td>${groupResult.vendorcost}</td>
</tr>
</#list>
</table>
</#listGroups>
This is the error I'm getting, but I feel like I'm very close to what I'd like to accomplish.
*The following has evaluated to null or missing:
==> record [in template "template" at line 58, column 14]
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
FTL stack trace ("~" means nesting-related):
Failed at: #macro listGroups results groupField [in template "template" in macro "listGroups" at line 3, column 1]
Reached through: #listGroups record.result, "result.cl... [in template "template" at line 58, column 1]
----*

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?

FREEMARKER: avoid escaping HTML chars

Having a problem with freemarker output...
[#assign optionsHTML = ""]
[#list data as item]
[#assign optionsHTML = optionsHTML + '<option value="' + item.value +'>'+ item.label + '</option>' /]
[/#list]
so, if I do
<select>
${iptionsHTML}
</select>
the output from otions get html entities instead of actual html.... so
<option value=&quot .....
even if I do
[#assign optionsHTML = ""]
[#list data as item]
[#noescape]
[#assign optionsHTML = optionsHTML + '<option value="' + item.value +'>'+ item.label + '</option>' /]
[/#noescape]
[/#list]
tried even
<select>
${iptionsHTML?html}
</select>
but's even worse :(
Putting #noescape around #assign has no effect. Automatic escaping only applies to ${...}-s that are embedded directly into the static text (the HTML). So there's no escaping to disable inside that #assign.
?html is used to escape a string "manually". Like in your example you could write optionsHTML = optionsHTML + '<option value="${item.value?html}>${item.label?html}</option>', because you know that the value will be output non-auto-escaped later, and the ${...}-s inside the string literal aren't escaped automatically.
However, the best would be if you can organize your code so that things that generate HTML don't construct the HTML inside variables and then print the variable, but print the HTML directly into the output. That's what FTL is designed for.
So after trying stuff, I don't know what I've done wrong before, but clean, this way is working
[#assign optionsHTML = ""]
[#list data as item]
[#assign optionsHTML = optionsHTML + '<option value="' + item.value +'>'+ item.label + '</option>' /]
[/#list]
<select>
[#noescape]
${optionsHTML}
[/#noescape]
</select>
Like ddekany said, write something like this:
<select>
[#list data as item]
<option value="${item.value}">${item.label}</option>
[/#list]
</select>
I faced same problem in string with special chars.
In this example I have checknumber = "6547&6548"
which caused problem before using this #escape
the best and simple way to handle this as following code
<#escape x as x?html>${deposit.checkNumber}</#escape>

JSTL stringFormat...set to a VAR?

Hello I have something like this:
<c:when test='${results.fieldNames[rowStatus.index] == "NSN"}'>
<fmt:formatNumber value="${cell}" pattern="${commas_with_no_decimal_pattern}" var="tArr"/>
</c:when>
Where Im setting a formatted # to var="tArr". I want to use a string formatter and set it to var="tArr". But I only know how to set it up like this:
<ctl:stringFormat format="####-##-###-####">${dispVal}</ctl:stringFormat>
How can I set this formatted string to a var?
Had to set var as a property in the ctl:stringFormat tld

Resources