WarnerJan Veldhuis wrote:
Gents,
I was wondering, what would be the best place for generating HTML? It
can either be in Velocity templates or by overriding the
Control#render(HtmlStringBuffer) and Control#getTag() methods.
What do you normally do?
I don't think there is a hard and fast rule here. Both approaches
have pros and cons.
Sticking to Java (#render) allows you use a debugger to step through
code. Java is also a more powerful language to expose extension points
(template pattern). For example you could have a method createLabel()
invoked from your render method which subclasses can override to
create a different label.
One problem with Java though is that multiline Strings and
interpolation isn't supported while templates engines do support this.
So specifying the HTML in a template normally is easier to read and
understand.
For example, I have a UserList extending a Panel, which shows a list of
users from my system. The HTML is just a simple DIV containing a
title-DIV and a UL/LI of users. So will it be a Velocity template with a
#foreach, or should I go and create custom controls and generate HTML on
the fly?
Sounds like a template would be easiest to begin with. You can always
refactor it later if you need more flexibility.
kind regards
bob