Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freemarker - creating multiple child tags

Given an ftl file which is structured as below (just an example) i am able to replace / insert all elements in to level 1 which is fine.

My confusion arises when there maybe multiple level2's - for example it could repeat many times. As such my process for replacing will hit a pain point

<parent>
    <level1>
        <a>${a}</a>
        <b>${b}</b>
        <c>${c}</c>
        <d>${d}</d>
    </level1>
    <level2>
        <e><${e}</e>
        <f><${f}</f>
        <g><${g}</g>
        <h><${h}</h>
        <i><${i}</i>
        <j><${j}</j>
    </level2>
</parent>

    map.put("a", "valuefora");
    map.put("b", "valueforb");
    map.put("c", "valueforc");
    map.put("d", "valueford");

    Configuration cfg = new Configuration();
    cfg.setDirectoryForTemplateLoading(new File("C:\\templates"));
    Template temp = cfg.getTemplate("freemarker.ftl");
    Writer out = new OutputStreamWriter(System.out);
    temp.process(map, out);

At this point here - assuming i have a list of values to have multiple level2 nodes - how would i go about producing this using the style shown above?

Thanks

like image 383
Biscuit128 Avatar asked Jan 19 '16 16:01

Biscuit128


People also ask

How do you create a list on FreeMarker?

FreeMarker doesn't support modifying collections. But if you really want to do this in FreeMarker (as opposed to in Java), you can use sequence concatenation: <#assign myList = myList + [newItem]> . Here you create a new sequence that wraps the two other sequences.

How do you insert a line break in FreeMarker?

Show activity on this post. In my code I am using "\n" for line breaks. It was suggested that I need to avoid "\n" because this is different for different OS (UNIX, windows and MAC) and each operating system would interpret this character differently.

What are macros in FreeMarker?

Macro variable stores a template fragment (called macro definition body) that can be used as user-defined directive. The variable also stores the name of allowed parameters to the user-defined directive.

Does FreeMarker have content?

The has_content FunctionFreeMarker has built-in functions to detect for variables. The most common method of detecting for empty or null values uses the has_content function and the trim function. The has_content function is often used with another built-in FreeMarker function to detect for null values.


1 Answers

I am not sure if i really understand your question, but i think you would just use the freemarker list-command. You would put Maps or Lists in your context. The list-command is documented here

<#list your.object.or.map as listElem>
  <level2>
     <e>${listElem.e}</e>
     <f>${listElem.f}</f>
   </level2>
</#list>

Your context object, just needs an according map or object.

The quickstart guide(see The list directive) also contains an example for using the list directive.

like image 121
Hendrik Jander Avatar answered Oct 19 '22 00:10

Hendrik Jander