velocityxdocreport

Velocity if condition insertion into xdocreport docx template


Reviewed XDocReport wiki for conditional text.

https://code.google.com/p/xdocreport/wiki/DocxReportingJavaMainCondition

Not clear on how to embed velocity template code into docx mergefield.

In docx, a mergefield of watershare has been created.

The context replace code is as follows:

IContext context = report.createContext();

// populate map
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("water_share", "#if( $water_share_transfer_flag )\n"
  + "\t<strong>Velocity!</strong>\n"
  + "#end");
map.put("water_share_transfer_flag", new Boolean("true"));
context.putMap(map);

the resulting docx replaces the water_share mergefield with the velocity condition, but doesnt replace variable water_share_transfer_flag and display result Velocity!!.


Solution

  • You can compare your docx as Velocity template and IContext to VelocityContext. So if you wish to use #if to manage condition you must not use it in your context like you have done, but use it inside mergefields (inside your docx) :

    #if( $water_share_transfer_flag )

    Velocity!

    #end