On 8/6/07, Familie Laux <[EMAIL PROTECTED]> wrote:
> 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.
Excellent!
> 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.
Yeah, it looks really clean considering the task being done.
> 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 haven't actually seen anyone do this sort of thing. At least, i
don't recall anyone contributing it. This would be a good thing to
have up on our Wiki somewhere (perhaps under CommunityArticles and
ContributedCode.
http://wiki.apache.org/velocity/
I also wonder if there's some way to turn your Table/Cell classes into
a TableTool of sorts that could go into the VelocityTools project.
http://velocity.apache.org/tools/devel/
Even if it weren't a typical tool (i.e. one that can be automatically
created and injected into the context) and always required special
handling, i still think this largely fits the aims of the
VelocityTools project.
I'm not as sure where the VelocityInteractor would fit, but i would be
curious to see/know more about what it does. In recent months, i've
been mulling over the possibility of a new project (or new package in
the core project) to provide super-easy-to-use wrappers around the
Velocity runtime that are pre-configured (or much more easily and
specifically configured) and enhanced for particular Velocity tasks
(e.g. like a VelocityEmailer). It's still just a thought at this
point, but a glance at how you're using it makes it look like the
VelocityInteractor could be a such a thing or at least a seed for such
things.
> 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]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]