A recent conversation with the JBoss AeroGear team inspired me to add FreeMarker support to Metawidget. This should be useful for those wanting to declare static layouts using FreeMarker templates rather than Java code.
To use the new FreeMarkerLayout, simply configure it:
BaseObjectInspectorConfig config = new BaseObjectInspectorConfig()
.setPropertyStyle( new StaticPropertyStyle() );
metawidget.setInspector( new CompositeInspector( new CompositeInspectorConfig()
.setInspectors(
new PropertyTypeInspector( config ),
new MetawidgetAnnotationInspector( config ) ) ) );
metawidget.setLayout( new FreemarkerLayout( new FreemarkerLayoutConfig()
.setDirectoryForTemplateLoading( "./src/main/resources" )
.setTemplate( "template.ftl" ) ) );
Or you can use metawidget.xml if your prefer...
...
<layout>
<freemarkerLayout xmlns="java:org.metawidget.statically.freemarker.layout"
config="FreemarkerLayoutConfig">
<directoryForTemplateLoading>
<string>./src/main/resources</string>
</directoryForTemplateLoading>
<template>
<string>template.ftl</string>
</template>
</freemarkerLayout>
</layout>
</staticHtmlMetawidget>
...define your FreeMarker template...
<tbody>
<#list widgets as widget><tr>
<th>${widget.label}:</th>
<td>${widget.xml}</td>
<#if (widget.attributes.required!"false") == "true">
<td>*</td>
<#else>
<td></td>
</#if>
</tr></#list>
</tbody>
</table>
...and point Metawidget at your business class...
StringWriter writer = new StringWriter();
metawidget.write( writer );
System.out.println( writer );
Metawidget will use its existing Inspectors, InspectionResultProcessors, WidgetBuilders and WidgetProcessors to prepare the widgets - but will lay them out using your FreeMarker template.
I've put together a complete example you can download here. Feedback welcome!