title: How to prepare a table with dynamic data in velocity template?
body: <p>I have a requirement in which I need to generate dynamic table in velocity template.</p>
  
  <p>I below java code I am preparing a hashmap. The inputs will come from external application.</p>
  
  <pre><code>public String generateFileFromStringTemplate(HashMap&lt;String, Object&gt; documentContentHashmap, String templateString,TWList list){
           System.out.println("hashmap : "+list.getArraySize());
           TWObject obj=null;
           Map&lt;String, Map&lt;String,String&gt;&gt; mapobj=new HashMap&lt;&gt;();
          try{
            TemplateUtility tutility = new TemplateUtility();
            obj = TWObjectFactory.createObject();
            for (int i = 0; i &lt; list.getArraySize(); i++) {
              obj=(TWObject) list.getUnmodifiableArray().get(i);
              Object[]s=obj.getPropertyNames().toArray();
              Map&lt;String, String&gt; map1=new HashMap&lt;&gt;();
              for (int j = 0; j &lt; obj.getPropertyNames().toArray().length; j++) {
                  map1.put(s[j].toString(),(String) obj.getPropertyValue(s[j].toString()));
              }
              mapobj.put("Map"+i, map1);
            }
            documentContentHashmap.put("Map", mapobj);
            StringWriter strWriter = tutility.generateTemplateFromString(documentContentHashmap, templateString);
  
            String documentBytes = tutility.generateDocsEncodedToBase64Content(strWriter);
            if (documentBytes != null) {
              return documentBytes;
            }
            return null;
          }
          catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
          }
        }
  }
  </code></pre>
  
  <p>Below is the code where i am passing the documentContentHashmap into velocity template engine.</p>
  
  <pre><code>StringWriter generateTemplateFromString(HashMap&lt;String, Object&gt; documentContentHashmap, String templateString)
        {
          try
          {
            RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy_HHmmss");
            String fileName = "StrTemplate_" + sdf.format(date) + ".html";
            StringReader reader = new StringReader(templateString);
            SimpleNode node = runtimeServices.parse(reader, fileName);
            Template template = new Template();
            template.setRuntimeServices(runtimeServices);
            template.setData(node);
            template.initDocument();
            VelocityContext context = new VelocityContext();
            Set&lt;String&gt; sourceKeySet = documentContentHashmap.keySet();
            System.out.println("sourceKeySet : "+sourceKeySet);
            for (String key : sourceKeySet) {
                System.out.println("documentContentHashmap.get(key) : "+documentContentHashmap.get(key));
              context.put(key, documentContentHashmap.get(key));
            }
            StringWriter writer = new StringWriter();
            template.merge(context, writer);
            return writer;
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
          return null;
        }
  </code></pre>
  
  <p>Below is the sample data which is generated from above code.</p>
  
  <pre><code>{Map0={interestRate=3, tenor=2 sal, proposedLimit=555, loanType=PL, approvedLimit=556, loanAmount=554, loanRepaymentDate=AAJ}, Map1={interestRate=1, tenor=3 sal, proposedLimit=445, loanType=HL, approvedLimit=446, loanAmount=444, loanRepaymentDate=KAL}}
  </code></pre>
  
  <p>Below is my html where I am iterating the map and trying to display above data into 2 rows, but i am not able to achieve so all the data gets displayed in one row only.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;style type="text/css"&gt;
  
  td,th{font-family:Arial, "Helvetica Neue", Helvetica, sans-serif;font-size:12px ;}
  .PaymentVoucher{
      font-size:15px;
      text-align:center;
      letter-spacing: 3px;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div&gt;
  &lt;table style="width:100%; border: 1px solid #dddddd; font-family: Times New Roman, Times, sans-serif;"&gt;
  &lt;tr&gt;
      &lt;td  colspan="2"&gt;
          &lt;label&gt;&lt;b&gt;&lt;/b&gt;&lt;/label&gt;
      &lt;/td&gt;
      &lt;td style="width:20%;"&gt;
      &lt;span&gt;Customer Name : $pvMap.get('customer_name')&lt;/span&gt;&lt;br/&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td style="width:20%;"&gt;
      &lt;br /&gt;
          &lt;label&gt;Page &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:&lt;/label&gt;&lt;span&gt;1 of 1&lt;/span&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td colspan="4" class="PaymentVoucher"&gt;&lt;b&gt;Loan&amp;nbsp; Details&amp;nbsp;&lt;/b&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  
      &lt;td&gt;
          &lt;label style="width:450px;"&gt;Loan Application Date : &lt;/label&gt;&lt;span&gt;$pvMap.get('application_date')&lt;/span&gt;
      &lt;/td&gt;
      &lt;td colspan = "3"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;br /&gt;
  &lt;table  style="width:100%; border: 1px solid #dddddd; font-family: Times New Roman, Times, sans-serif;"&gt; 
    &lt;tr&gt;
          &lt;th&gt;Loan Type&lt;/th&gt;&lt;th&gt;Loan Amount&lt;/th&gt;&lt;th&gt;Interest Rate&lt;/th&gt;&lt;th&gt;Loan Repayment Date&lt;/th&gt;&lt;th&gt;Tenor&lt;/th&gt;&lt;th&gt;Proposed Limit&lt;/th&gt;&lt;th&gt;Approved Limit&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      #set($str1="loanType")
  
      #set($str2="loanAmount")
  
      #set($str3="interestRate")
  
      #set($str4="loanRepaymentDate")
  
      #set($str5="tenor")
  
      #set($str6="proposedLimit")
  
      #set($str7="approvedLimit")
  
      #foreach ($mapEntry in $Map.entrySet())
          #foreach ($map1 in $mapEntry.getValue().entrySet())
              #if($map1.key==$str1)
                  &lt;td&gt;$map1.getValue()&lt;/td&gt;
              #end
          #end
      #end
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  &lt;span&gt;Regards,&lt;/span&gt;&lt;BR/&gt;
  &lt;span&gt;Test&lt;/span&gt;&lt;BR/&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Can some one help on this? Thanks in advance.</p>