Hi,

in my work I quite frequently have the requirement to create somewhat
complex HTML tables using Velocity. "complex" here refers to cells
spanning multiple rows and columns which are located somewhere inside
the table. That of course leads to interesting challenges in the
Velocity macro to print the proper HTML tags such as "<tr>" and
"<td>" in those places where they are required.

Until recently, I used hierarchies of #if directives together
with marker variables (#set) e. g. to decide when to print a
"<tr>" for a cell that spans multiple rows.

Since that became quite tedious and difficult to maintain I have
searched for an easier way to manage such tables with Velocity. Since
I couldn't find anything really I decided to write some helper classes
myself.

To give you an example of what I mean:

This code snippet

VelocityInteractor interactor = new VelocityInteractor(".");

Table table = new Table(10, 15);
table.setClipping(true);

Cell cell = new Cell("test", 4, 3);
table.setCell(cell, 2, 2);
cell = new Cell("test22", 2, 13);
table.setCell(cell, 8, 6);
cell = new Cell("test33", 10, 1);
table.setCell(cell, 0, 0);
table.setCell(cell, 0, 5);
cell = new Cell("test44", 4, 4);
table.setCell(cell, 0, 11);

BufferedWriter writer = new BufferedWriter(new FileWriter("test.html"));

interactor.add("table.vm");
interactor.put("table", table);
interactor.merge(writer);
writer.flush();
writer.close();

creates a table and adds some cells to it. The cells span multiple
rows and columns. Table and Cell are classes that I have created.

The Velocity macro table.vm used here is quite simple:

<html>
<body>

#set ( $rowMax = $table.rowNumber - 1 )
#set ( $colMax = $table.colNumber - 1 )

<table border="1" cellspacing="0" cellpadding="0">

#foreach ( $row in [0..$rowMax])

  <tr>

  #foreach ( $col in [0..$colMax])

    #if ( $table.isVisible($row, $col) )
      #if ( $table.isDefaultCell($row, $col) )
        <td> ($row,$col) </td>
      #else
        #set ( $cell = $table.getCell($row, $col) )
        <td rowspan="$cell.rowSpan"
            colspan="$cell.colSpan"
            align="center"> $cell.name
        </td>
      #end
    #end

  #end

#end

</table>

</body>
</html>

and the example HTML output file is attached. All the cells are
as expected with a very simple and clean Velicity macro structure.

There is a lot more that could be said about these helper
classes (for example on how cell data can be transported
into the Velocity macro), but the main question for me is:
is this something that is useful such that it's worth writing
some short article about it for the benefit of others - or has
this been solved a thousand times before (and much better)
but I used the wrong keywords in Google when checking for that?

I realize of course that this is something to complement
Velocity and has nothing to do with enhancing Velocity itself.
Nevertheless, any feedback on the usefulness of this for the
general public would be appreciated. I would then invest more
time in documentation and additional debugging and look for a
place to make this available. Maybe there is a general place for
Velocity add-ons?

Thanks a lot,
Matthias



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to