You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Familie Laux <am...@web.de> on 2007/08/07 00:41:57 UTC

Table creation helper package for Velocity?

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




Re: Table creation helper package for Velocity?

Posted by Nathan Bubna <nb...@gmail.com>.
On 8/7/07, Christopher Schultz <ch...@christopherschultz.net> wrote:
> Nathan,
>
> Nathan Bubna wrote:
> > 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/
>
> While I think this is an interesting tool, I don't think it's widely
> applicable enough to go into the VelocityTools project. Most of the
> tools already provided are useul in a very wide range of applications,
> but this one seems very narrow.

Organizing data into tables is pretty common.  things like DisplayTag
are quite popular in the JSP world (actually displaytag is 80% of why
i like to see Velocity and JSP play together better).  While it would
probably take a herculean effort to turn this into a match for that
anytime soon, having something to simplify the process of outputting
tables of data would be a small step in that direction.  i'm not
saying that i'm sure this would fit in VelocityTools, but i'm think it
might with a little work.  i'd at least like to see more of it.

> I like your idea of putting it onto the Contributed Code section of the
> Wiki, though.
>
> Just my two cents

always appreciated!!

> -chris
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Table creation helper package for Velocity?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Nathan,

Nathan Bubna wrote:
> 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/

While I think this is an interesting tool, I don't think it's widely
applicable enough to go into the VelocityTools project. Most of the
tools already provided are useul in a very wide range of applications,
but this one seems very narrow.

I like your idea of putting it onto the Contributed Code section of the
Wiki, though.

Just my two cents.
-chris


Re: Table creation helper package for Velocity?

Posted by Familie Laux <am...@web.de>.
Hi,

thanks a lot for the quick responses!

 > 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.

Yes, the CommunityArticles and ContributedCode sections
look promising to me as well. I'll try to get something done soon.

> 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 have attached the source code for the VelocityInteractor. It's
not rocket science, just a wrapper that I found useful for handling
many different VelocityContexts and Templates. Maybe you also find
it useful. Feel free to use or modify.

Thanks,
Matthias

P.S.: I noticed that the mailing list archive seems to discard
attachments, at least I can't see the attachment from my previous
posting in the web interface. Let me know if I should resend it
directly.


Re: Table creation helper package for Velocity?

Posted by Nathan Bubna <nb...@gmail.com>.
On 8/6/07, Familie Laux <am...@web.de> 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: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org