The input to my freemarker template is records: List<List<String>> records = new ArrayList();
each list inside records is a row from a database;
I need to create a csv;
How I can write values of a row in a separate line to create a csv.
Here is my freemarker template:
for now it write any value in a different line.
<#list records>
<#items as record>
<#list record>
<#items as value>
${value},
</#items>
</#list>
</#items>
</#list>
``````````
In simple cases #items is not needed, so I give the example without that (but same approach works with #items as well).
<#list record as value>${value}<#sep>,</#list>
That is, #list (and #items) just repeatedly prints the content nested between its start tag and end tag (except, it ignores the line-break directly after the start tag). So if you don't have a line-break at the end of the nested content, then it doesn't print a line-break.
As of #sep, I added that because you don't want comma after the last value of the record.
Related
I'm using a data-directive or list to pull in several values, of which, I don't know how many there will be, and I want to try and list through them, and create several variables in the list if you will.
<#list 1..10 as x>
<#-- the next line doesn't work, but what i'm trying to fix -->
<#assign .vars['VAR'+x?string] = rand(100) />
</#list>
But I can list them back out that way.
<#list 1..10 as x>
${.vars['VAR'+x?string]}
</#list>
The documentation for assign, says:
name: name of the variable. It is not expression. However, it can be
written as a string literal, which is useful if the variable name
contains reserved characters, for example <#assign "foo-bar" = 1>.
Note that this string literal does not expand interpolations (as
"${foo}").
Is there no way around this? Am I trying to do the impossible? Is there some way I can insert a derived name into the .vars... Hash is it?
A little more research that was close, but didn't get me there:
This prevoius question gives how to READ the derived variable, but I need to WRITE/CREATE the derived variable.
FreeMarker get variable value by concatenating another variable value
This prevoius question shows that I can use a string to assign a variable and re-iterates what we saw in the first link.
Variable name in Freemarker Template Language
As FreeMarker can't assign to elements of collections (but you can ?map(it -> ...) a collection to another), the only way is via ?interpret:
<#list 1..10 as x>
<#-- the next line doesn't work, but what i'm trying to fix -->
<#'<#assign VAR${x?c} = rand(100)>'?interpret />
</#list>
I wonder why do you need to assign to a dynamically named variables though, since reading dynamically named variables is also a pain, even if a lesser one.
Ultimately, I believe the correct way to phrase my solution was a sequence of hashes:
<#list 1..z_Coupon_Pet.NUM_COUPONS as x>
<#assign INSTORE_COUPON=call_coupon_from_table1() />
<#assign ONLINE_COUPON=call_coupon_from_table2() />
<#assign coupon_string_row= '{
"COUPON_NUM" : ${x},
"INSTORE" : "${INSTORE_COUPON?js_string}",
"ONLINE" : "${ONLINE_COUPON?js_string}"
}' />
<#if x==1>
<#assign coupon_hash_string = coupon_string_row />
<#else>
<#assign coupon_hash_string = coupon_hash_string + ',' + coupon_string_row />
</#if>
</#list>
</#if>
<#if coupon_hash_string?has_content>
<#assign coupon_hash=parsejson('[' + coupon_hash_string + ']') />
</#if>
We specifically avoid <#assign my_hash = my_hash + element /> because of this note in the documentation:
Note that hash concatenation is not to be used for many repeated concatenations, like for adding items to a hash inside a loop. While adding together hashes is fast and is constant time (independent of the size of the hashes added), the resulting hash is a bit slower to read than the hashes added together. Thus after tens... of additions the result can be impractically slow to read.
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}).
<#list reports as report>
<#list report.transactionList as expense>
${expense.transactionID}^<#t>
${table[expenses.transcationID}
<#if expense.modifiedCreated?has_content>
${expense.modifiedCreated}^<#t>
<#else>
${expense.created}^<#t>
</#if>
In the above code I have a hash table called table and I want to use expense.transactionID as the key to then load the table's value like in the above code.
when I run it, the second item instead of a value is just a blank spot.
figured it out. {table[expenses.transcationID} needs to cast the category I am using as the key into a string. so the answer is: {table[expenses.transcationID?string}
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 want to generate some java code using freemarker, that is to generate parameters for a method.Say I have a method named doIt, which needs some parameter name and their class names, I will give the template a param named paramList.I define a macro directive , iterate the parameters list,but consequently each parameter occupies a row. My template code is as below:
<#macro paramList plist>
<#if plist??>
<#list plist as p>
${p.javaType?substring(2)} ${p.name} <#if p_has_next>, </#if>
</#list>
</#if>
</#macro>
doIt(<#paramList plist=params/>)
The running result is :
doIt( int end ,
String endDate ,
String evtCode ,
int evtNo ,
String giftCode ,
int start ,
String startDate
)
How to make all the parameters output appear in the same row. I know I can write the list directive logic in the same row to avoid line breaking ,but if there are other logic too, it will get too long to read and understand after a while.
The format I want is :
doIt(int end , String endDate, String evtCode , int evtNo , String giftCode , int start , String startDate)
Put a <#t> after the innermost </#if>. (See http://freemarker.org/docs/ref_directive_t.html#ref.directive.t)
Another way for you to do it there would be to change your code like this:
<#macro paramList plist>
<#if plist??>
<#list plist as p>${p.javaType?substring(2)} ${p.name} <#if p_has_next>, </#if></#list>
</#if>
</#macro>
doIt(<#paramList plist=params/>)
That is, make the list all inline. It's harder to read, but it will keep your white space characters that you need (i.e. the spaces).