Templates are easy to use :
Create a file that is your template, this file contains some html and some special tags
<!-- BEGIN example1 -->
<div class="box">
This is an {EXAMPLE}
</div>
<!-- END example1 -->
In the start method of your module create the template object
public void start()
{
//....
Document doc = repository.loadTemplate("/template.html");
repository.addTemplate("example1", (Element) doc.selectSingleNode("/node/loopname='example1']/node"));
Template example1 = repository.createTemplate("example1");
//....
}
Template and module interact with a DelegateContext object
//....
DelegateContext ctx = new DelegateContext();
ctx.put("EXAMPLE", "example");
//....
Template renders with a writer and the context :
example1.render(ctx, page.getBodyWriter());
!You can nest <!-- BEGIN ... --> <!-- END ... --> tags
<!-- BEGIN extern -->
{EXTERN_VALUE}
<!-- BEGIN switch_nested-->
{switch_nested.NESTED_VALUE}
<!-- END switch_nested -->
<!-- END extern -->
the contex must be initialised this way :
// ....
DelegateContext extern = new DelegateContext();
extern.put("EXTERN_VALUE", "extern value");
DelegateContext nested = extern.next("switch_nested");
nested.put("NESTED_VALUE", "nested value");
// ....
It is very usefull when you want to hide or display the nested content because the template system do not render "switch_nested" if you do not call next() on the extern context.
You can implement loops too:
<!-- BEGIN extern -->
{EXTERN_VALUE}
<!-- BEGIN switch_loop -->
{switch_loop.LOOP_VALUE}
<!-- END switch_loop -->
<!-- END extern -->
DelegateContext extern = new DelegateContext();
extern.put("EXTERN_VALUE", "extern value");
for (int i = 0; i < 100; i++)
{
extern.next("switch_loop").put("LOOP_VALUE", new String(i));
}
Comments