Discussion:
[FreeMarker-user] How we read the list<map;string>
Yadid Qin
2014-05-28 07:57:42 UTC
Permalink
Hello Freemarker Team,

There is one issue it confused me.

Eg: java code:

SimpleHash root = new SimpleHash();
List<Object> list = new ArrayList<Object>();
list.add("red");
list.add("green");
list.add("blue");
SimpleHash mapColor = new SimpleHash();
mapColor.put("c1", "red");
mapColor.put("c2", "green");
mapColor.put("c3", "blue");
list.add(mapColor);
root.put("theList", list);

FTL file:
<#list theList as item>

<#if item_index =3> --> if I do not know the index=3, then how I read the map in the list.
<#assign keys = item?keys>
<#list keys as key>
${key} = ${item[key]}<br>
</#list>
<#else>
${item_index + 1}. ${item}<br>
</#if>

</#list>

Output:
+- theList
| |
+- 1. red
| |
+- 2. green
| |
+- 3. blue
| |
||
+- c1 = red
||
+- c2 = green
||
+- c3 = blue


Thanks & Best Regards,
Yadid
b***@me.com
2014-05-29 06:56:06 UTC
Permalink
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@SimSun";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1026" /> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1" /> </o:shapelayout></xml><![endif]--> </head> <body lang="EN-US" link="blue" vlink="purple">Hi Yadid,<div><br></div><div>You can use is_hash_ex to find if the item is a map</div><div><br>http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_isType</div><div><br><hr><div><strong>From:</strong> "Yadid Qin" &lt;***@onsemi.com&gt;<br><strong>To:</strong> "freemarker-***@lists.sourceforge.net" &lt;freemarker-***@lists.sourceforge.net&gt;<br><strong>CC:</strong> "freemarker-***@lists.sourceforge.net" &lt;freemarker-***@lists.sourceforge.net&gt;<br><strong>Sent:</strong> 28 May 2014 20:28<br><strong>Subject:</strong> [FreeMarker-user] How we read the list<map;string><br></map;string></div><br> <div class="WordSection1"> <p class="MsoNormal">Hello Freemarker Team,<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">There is one issue it confused me.<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">Eg: java code:<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">SimpleHash root = new SimpleHash();<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">List&lt;Object&gt; list = </span><b><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#7F0055">new</span></b><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"> ArrayList&lt;Object&gt;();</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; list.add(</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"red"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; </span><u><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:blue">list</span></u><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">.add(</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"green"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; list.add(</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"blue"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; SimpleHash mapColor = </span><b><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#7F0055">new</span></b><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"> SimpleHash();</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; mapColor.put(</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"c1"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">, </span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"red"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; mapColor.put(</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"c2"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">, </span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"green"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; mapColor.put(</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"c3"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">, </span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF">"blue"</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">);</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp; list.add(mapColor);<o:p></o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">root.put("theList", list);<o:p></o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"><o:p>&nbsp;</o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">FTL file:<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">&lt;#list theList as item&gt;<o:p></o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"><o:p>&nbsp;</o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">&lt;#if item_index =3&gt; &nbsp;&nbsp;&nbsp;</span><span style="font-size:10.0pt;font-family:Wingdings">?</span><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"> </span><b><span style="font-size:12.0pt;font-family:&quot;Courier New&quot;;background:yellow;mso-highlight:yellow">if I do not know the index=3, then how I read the map in the list.</span></b><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;"><o:p></o:p></span></p> <p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">&lt;#assign keys = item?keys&gt;&nbsp;&nbsp; <o:p></o:p></span></p> <p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">&lt;#list keys as key&gt;<o:p></o:p></span></p> <p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">${key} = ${item[key]}&lt;br&gt;<o:p></o:p></span></p> <p class="MsoNormal" style="margin-left:.5in;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">&lt;/#list&gt;<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">&lt;#else&gt; <o:p></o:p></span></p> <p class="MsoNormal" style="text-indent:.5in;text-autospace:none"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">${item_index + 1}.&nbsp; ${item}&lt;br&gt;&nbsp; <o:p></o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;">&lt;/#if&gt;<o:p></o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"><o:p>&nbsp;</o:p></span></p> <p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">&lt;/#list&gt;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black">Output:<o:p></o:p></span></p>
<p class="MsoNormal">+- theList<br>
| |<br>
+- 1. red<br>
| |<br>
+- 2. green<br>
| |<br>
+- 3. blue<br>
| |<br>
||<br>
+- c1 = red<br>
||<br>
+- c2 = green<br>
||<br>
+- c3 = blue<span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Courier New&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal"><b><i><span style="color:#4F81BD">Thanks &amp; Best Regards,<o:p></o:p></span></i></b></p>
<p class="MsoNormal"><b><i><span style="color:#4F81BD">Yadid<o:p></o:p></span></i></b></p>
<p class="MsoNormal"><span style="font-size:12.0pt;color:#4F81BD"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>


</div></body></html>

Loading...