When creating a Sequence in Freemarker, is it poss...
# advancedpdf
j
When creating a Sequence in Freemarker, is it possible to field name and values so I can create a record similar to that of the record.item as item functionality so I can do something like: #list my_seq as x ${x.my_field1} ${x.my_field2} /#list
d
I think I understand. To confirm, you're wanting to populate a field on the record with an array of names and values (i.e. JSON) and then use the
<#list>
directive to list it in your template
so your field contains something like:
Copy code
[{my_field1:"1A", my_field2:"1B"},
{my_field1:"2A", my_field2:"2B"}]
if I've got that correct, you can use the string built-in ?eval_json for example:
Copy code
<#assign my_seq = custfield_jsonfield?eval_json>
<#list my_seq as x>
    ${x.my_field1}
    ${x.my_field2}
</#list>
if you're already doing custom printing with the
N/render
module, you can/should use TemplateRenderer.addCustomDataSource() directly on your object instead of putting it into a field with
JSON.stringify()
but if you're just printing with one of the native print buttons the ?eval_json approach will work fine
j
Thank you!
d
NP, GL let us know how you get on
j
@David B, I have never used TemplateRenderer.addCustomDataSource(), wound you mind sending me some sample code??
d
there's sample code for the method right on the help page for it, otherwise a bunch of forum/blog examples via google
j
Yes I found it...thank you!!
šŸ‘ 1
@David B, following up on your comment "let us know how you get on". I must understand something incorrectly. The code below prints nothing and adding ?eval_json complains that my list is not a string. Do you have any advice?
Copy code
<#assign my_list = [] />
  <#if recond?has_content>
  	<#list record.item as item>
    	<#assign my_list = my_list + [{my_item:"${item.item}", my_description:"${item.description}"}] />
  	</#list>
  </#if>
    
  <#list my_list as lines>${lines.my_item}</#list>
d
It's a bit anti-freemarker to build new sequences of hashmaps (arrays of objects) inside of the template. Could you instead: • Do your printing inside the original loop? • Just loop
record.item
again and print there? It might be that you've just used a trivial example, but I'm not seeing the use-case for what you're attempting
j
I figured this may be out of scope but I often run into requests that would lend themselves to building and manipulating the data in a separate data structure. My example was a theoretical exercise. Thank you for the follow up.
šŸ‘ 1