velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fred Toth <>
Subject Re: conditional #include/#parse?
Date Sat, 08 May 2004 02:54:53 GMT
Thanks Will.

Though this is very nice and will no doubt be helpful,
I was hoping to have the best of both worlds. In other words, I need
all four behaviors (include, parse, conditional include, conditional
parse), sometimes in the same template!

So, since Velocity is such a fine set of tools, I offer this to the group.

A mini "tool" for the tool box:

public class TemplateExistsTool {
     public boolean exists(String template) {
         return Velocity.templateExists(template);

A tool box entry:


And a couple of macro definitions:

#macro( cinclude $file )##
#macro( cparse $file )##

With the above you can use these macros to get the job done:


If the files are not there, the macro silently evaluates to the
empty string. If the files exist, you get the normal behavior.

Thanks for the help.


At 11:00 AM 5/7/2004, you wrote:
>There's a patch to Velocity under discussion that would allow this behavior,
>but it has not yet been committed.
>Among other things, the patch allows you to use a include event handler that
>can take action before the file is included.  A sample event handler is
>provided that will include an error page if the file isn't found.  If the
>error page is blank, this would do exactly what you are looking for.
>----- Original Message -----
>From: "Fred Toth" <>
>To: <>
>Sent: Friday, May 07, 2004 4:58 AM
>Subject: conditional #include/#parse?
> > Hi,
> >
> > I'm wondering if there is a way to make #include and #parse
> > "silent" in the case where the file to be included/parsed does
> > not exist. Currently they always throw an exception.
> >
> > We find that to be useful behavior in certain circumstances.
> >
> > I imagine I could write a macro that references a bean that
> > checks for the file, but I'm hoping there is an easier way.
> >
> > Any suggestions?
> >
> > Many thanks,
> >
> > Fred
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > For additional commands, e-mail:
> >
>To unsubscribe, e-mail:
>For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message