velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fred Toth <ft...@synernet.com>
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:

package your.package.here;
import org.apache.velocity.app.Velocity;
public class TemplateExistsTool {
     public boolean exists(String template) {
         return Velocity.templateExists(template);
     }
}

A tool box entry:

     <tool>
         <key>ftest</key>
         <scope>application</scope>
         <class>your.package.here.TemplateExistsTool</class>
     </tool>

And a couple of macro definitions:

#macro( cinclude $file )##
#if($ftest.exists($file))#include($file)#end##
#end
#macro( cparse $file )##
#if($ftest.exists($file))#parse($file)#end##
#end

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

#cinclude("file_that_may_not_be_there")
#cparse("another_file_that_may_not_be_there")

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.

Fred

At 11:00 AM 5/7/2004, you wrote:
>Hi,
>
>There's a patch to Velocity under discussion that would allow this behavior,
>but it has not yet been committed.
>http://issues.apache.org/bugzilla/show_bug.cgi?id=28388
>
>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.
>
>Best,
>WILL
>
>----- Original Message -----
>From: "Fred Toth" <ftoth@synernet.com>
>To: <velocity-user@jakarta.apache.org>
>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: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: velocity-user-help@jakarta.apache.org



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


Mime
View raw message