You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Ewan Makepeace <ew...@yahoo.com> on 2001/05/11 03:41:40 UTC

# Character

A while back there was a question as to whether the # character could be
changed for another string for specific templates. I believe the reply was
that it was hard coded and no need was seen to make it changeable as that
would make different templates incompatible, and harder to understand (both
valid arguments).
However I have an application that would benefit a lot from being able to
select my own # symbol (what do you call it - the directive symbol?). I am
generating significant amounts of code from my own application generator,
mainly in the form of self persisting 'smart' objects, and typesafe
collections of those objects. I also automatically generate JSP pages to
display and edit each object and to display a table of objects from a
collection.
I would like to switch to Velocity from JSP for my display code, and at the
same time use Velocity as the template engine for the application generator.
This works fine for my Java templates, but generating Velocity templates
using Velocity just doesn't work well. Apart from the work of escaping all
those # characters that are part of the output template not the input, the
whole thing becomes an unreadable mess...
So (Geir?) - any workarounds, or might it be possible to set an alternative
character?

rgds
Ewan


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Re: Slightly OT: J2EE authentication Framework?

Posted by pat comcen <ps...@comcen.com.au>.
Thanks Leon,
I've come to the conclusion that this is the way to go, after some polite
encouragement from Fedor.
I've downloaded the TDK and am rearing to go!

Patrick.


----- Original Message -----
From: Leon Messerschmidt <le...@opticode.co.za>
To: <ve...@jakarta.apache.org>
Sent: Friday, May 11, 2001 4:32 PM
Subject: Re: Slightly OT: J2EE authentication Framework?


> Hi Patrick,
>
> I would really suggest that you go to Turbine.  Your security model is
bound
> to become more complex as time goes by and Turbine will handle this for
you.
> It will also help you when your database gets a bit more tricky.
>
> The nice thing about Turbine is that if you're not using a feature it
isn't
> even loaded and you don't need to think about it.  Turbine will therefore
> not be overkill - you don't need to learn anything that you don't use :-)
>
> Get the TDK.  You'll have a running app within 10 minutes.  I should stop
> now I'm starting to sound like a marketing guy :-)
>
> ~ Leon
>
> > Does anyone know of a framework that I can 'plug' into
> > my java web application that will let me easily authenticate users.
> > User informaion should be stored in a JDBC compatible dataase.
> >
> > I know Turbine does something like this, but Turbine is
> > a little overkill for my project.
> > Things I would hope for:
> >     * Table with user info.
> >     * Encapsulation/Abstraction of user data.
> >     * Methods to validate user password.
> >
> > I know it's not too hard to do, but I want to avoid re-inventing
> > if it already exists somewhere.
> >
> > BTW I don't much about java beans - my head is already hurting
> > from learning Velocity, Servlets, MySQL, etc..!
> >
> > Thanks,
> >
> > Patrick Saunders.
>
>


Re: Slightly OT: J2EE authentication Framework?

Posted by Leon Messerschmidt <le...@opticode.co.za>.
Hi Patrick,

I would really suggest that you go to Turbine.  Your security model is bound
to become more complex as time goes by and Turbine will handle this for you.
It will also help you when your database gets a bit more tricky.

The nice thing about Turbine is that if you're not using a feature it isn't
even loaded and you don't need to think about it.  Turbine will therefore
not be overkill - you don't need to learn anything that you don't use :-)

Get the TDK.  You'll have a running app within 10 minutes.  I should stop
now I'm starting to sound like a marketing guy :-)

~ Leon

> Does anyone know of a framework that I can 'plug' into
> my java web application that will let me easily authenticate users.
> User informaion should be stored in a JDBC compatible dataase.
>
> I know Turbine does something like this, but Turbine is
> a little overkill for my project.
> Things I would hope for:
>     * Table with user info.
>     * Encapsulation/Abstraction of user data.
>     * Methods to validate user password.
>
> I know it's not too hard to do, but I want to avoid re-inventing
> if it already exists somewhere.
>
> BTW I don't much about java beans - my head is already hurting
> from learning Velocity, Servlets, MySQL, etc..!
>
> Thanks,
>
> Patrick Saunders.


Re: Slightly OT: J2EE authentication Framework?

Posted by Tim Joyce <ti...@hoop.co.uk>.
Hi Patrick ,

> Does anyone know of a framework that I can 'plug' into
> my java web application that will let me easily authenticate users.
> User informaion should be stored in a JDBC compatible dataase.

you could do worse than look at melati (http://melati.org), which will
provide you with a user table and both Session based and HTTP Basic based
authorization interfaces 'out of the box'.  You will need to run with both
Melati and Velocity from CVS until the next released snapshots of both (or
use the nightly snapshot).

Melati may also be a bit of overkill, but it does provide a serious
alternative to Turbine.  I do suspect that you will find that getting to
grips with eithier of these frameworks will seriously increase your
productivity.  The chances are that User Authentification is not the only
feature that your application will need.  eg what about an Administration
interface to your database, persistant objects, messageboards, shopping
cart?

i hope this helps

cheers

timj



Slightly OT: J2EE authentication Framework?

Posted by ps...@comcen.com.au.
Does anyone know of a framework that I can 'plug' into
my java web application that will let me easily authenticate users.
User informaion should be stored in a JDBC compatible dataase.

I know Turbine does something like this, but Turbine is
a little overkill for my project.
Things I would hope for:
    * Table with user info.
    * Encapsulation/Abstraction of user data.
    * Methods to validate user password.

I know it's not too hard to do, but I want to avoid re-inventing
if it already exists somewhere. 

BTW I don't much about java beans - my head is already hurting
from learning Velocity, Servlets, MySQL, etc..!

Thanks,

Patrick Saunders.

RE: # Character

Posted by Ewan Makepeace <ew...@yahoo.com>.
Geir, Jason,

a)	You two are so responsive that it blows my socks off...
b)	Is it just me or is this a friendlier list than Turbine?

Anyway I don't yet have a Velocity to Velocity example for you. I attach a
typical generated JSP page (even uglier than normal JSP since it was
auto-generated!) for comparison. As you see there are quite a few IF{}ELSE{}
blocks. I also attach the corresponding template file, but as you can see
all the tricky stuff is in my Java code, not the template. One of the
attractions of Velocity is to move more of the template stuff back into the
template, especially with VelociMacros.

Now in my original email I wasn't clear enough because the problem is not
just with # but $ too! How do I distinguish between  context access for my
code generator and context access for the generated template?

rgds
Ewan

-----Original Message-----
From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
Behalf Of Geir Magnusson Jr.
Sent: Friday, May 11, 2001 10:06 AM
To: velocity-user@jakarta.apache.org
Subject: Re: # Character


Ewan Makepeace wrote:
>
> A while back there was a question as to whether the # character could be
> changed for another string for specific templates. I believe the reply was
> that it was hard coded and no need was seen to make it changeable as that
> would make different templates incompatible, and harder to understand
(both
> valid arguments).
> However I have an application that would benefit a lot from being able to
> select my own # symbol (what do you call it - the directive symbol?). I am
> generating significant amounts of code from my own application generator,
> mainly in the form of self persisting 'smart' objects, and typesafe
> collections of those objects. I also automatically generate JSP pages to
> display and edit each object and to display a table of objects from a
> collection.
> I would like to switch to Velocity from JSP for my display code, and at
the
> same time use Velocity as the template engine for the application
generator.
> This works fine for my Java templates, but generating Velocity templates
> using Velocity just doesn't work well. Apart from the work of escaping all
> those # characters that are part of the output template not the input, the
> whole thing becomes an unreadable mess...
> So (Geir?) - any workarounds, or might it be possible to set an
alternative
> character?
>

You only need to escape the # if your templates are full of

#if
#set
#else
#elseif
#include
#parse
#end
#macro
##
#* *#

that aren't VTL.  Otherwise, Velocity's parser ignores them.

What does one look like?

geir

--
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: # Character

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Ewan Makepeace wrote:
> 
> A while back there was a question as to whether the # character could be
> changed for another string for specific templates. I believe the reply was
> that it was hard coded and no need was seen to make it changeable as that
> would make different templates incompatible, and harder to understand (both
> valid arguments).
> However I have an application that would benefit a lot from being able to
> select my own # symbol (what do you call it - the directive symbol?). I am
> generating significant amounts of code from my own application generator,
> mainly in the form of self persisting 'smart' objects, and typesafe
> collections of those objects. I also automatically generate JSP pages to
> display and edit each object and to display a table of objects from a
> collection.
> I would like to switch to Velocity from JSP for my display code, and at the
> same time use Velocity as the template engine for the application generator.
> This works fine for my Java templates, but generating Velocity templates
> using Velocity just doesn't work well. Apart from the work of escaping all
> those # characters that are part of the output template not the input, the
> whole thing becomes an unreadable mess...
> So (Geir?) - any workarounds, or might it be possible to set an alternative
> character?
> 

You only need to escape the # if your templates are full of

#if
#set
#else
#elseif
#include
#parse
#end
#macro
##
#* *#

that aren't VTL.  Otherwise, Velocity's parser ignores them.

What does one look like?

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: conditional operator

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Duddleston wrote:
> 
> This of course is not supported, so I do the following.
> 
> #if( $velocityCount%2==0 )
>     #set( $bgcolor = "#66CCFF")
> #else
>     #set( $bgcolor = "#FFFFFF")
> #end
> 
> Is there a trick to keep this small. I tried to setup a macro, but the end
> results in messed up formating and the causes a bug in IE.

[gmj@192 misc]$ more color.vm 

#macro( bgc $count )#if( $count%2==0 )#66CCFF#else#EEEEEE#end#end

<td bgcolor="#bgc(1)"> blaa blaa blaa blaa blaa blaa </td>
<td bgcolor="#bgc(2)"> blaa blaa blaa blaa blaa blaa </td>

[gmj@192 misc]$ ./test.sh color.vm ; more output

<td bgcolor="#EEEEEE"> blaa blaa blaa blaa blaa blaa </td>
<td bgcolor="#66CCFF"> blaa blaa blaa blaa blaa blaa </td>


-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: Simple questions

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Jorge De Flon wrote:
> 
> Hello,
> 
> I Apologize if my questions are too simple, but i have not found the answers
> in the users guide (maybe i overlooked them)
> 1) Do you need apache to run velocity? can it run in other webserver (IIS?
> netscape?)

You don't need the Apache webserver, but will need a reasonably good
servlet container.

> 2) Do you need a servlet container?

If you wish to use Velocity for web work, yes, that would be the best
way.

Take a look at Tomcat  http://jakarta.apache.org/tomcat/

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: Simple questions

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/11/01 4:06 PM, "Jorge De Flon" <ja...@yahoo.com> wrote:

> thank you for your quick and helpful reply.
> 
> I am begining to use it, and I am trying to use it as a java-PHP, that is,
> a simple scripting language for apache AND IIS.
> later (when i learn it well enough ), I want to use servlets and even EJB in
> the business side.
> is that possible?
> do i need tomcat? or only a webserver?
> 
> thank you again.

http://www.oreilly.com/catalog/jservlet2/

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


RE: Simple questions

Posted by Jorge De Flon <ja...@yahoo.com>.
thank you for your quick and helpful reply.

I am begining to use it, and I am trying to use it as a java-PHP, that is,
a simple scripting language for apache AND IIS.
later (when i learn it well enough ), I want to use servlets and even EJB in
the business side.
is that possible?
do i need tomcat? or only a webserver?


thank you again.


----- Original Message -----
From: Jon Stevens <jo...@latchkey.com>
To: velocity-user <ve...@jakarta.apache.org>
Sent: Friday, May 11, 2001 5:47 PM
Subject: Re: Simple questions


> on 5/11/01 3:37 PM, "Jorge De Flon" <ja...@yahoo.com> wrote:
>
> > Hello,
> >
> > I Apologize if my questions are too simple,
>
> Well, it would be helpful if they were more specific rather than simple
> since simple is relative.
>
> > but i have not found the answers
> > in the users guide (maybe i overlooked them)
> > 1) Do you need apache to run velocity? can it run in other webserver
(IIS?
> > netscape?)
>
> That is two different questions.
>
> #1a. No.
> #1b. Yes.
>
> > 2) Do you need a servlet container?
>
> The answer is that it depends on what you are trying to do.
>
> -jon
>
> --
> If you come from a Perl or PHP background, JSP is a way to take
> your pain to new levels. --Anonymous
> <http://jakarta.apache.org/velocity/ymtd/ymtd.html>


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Re: Simple questions

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/11/01 3:37 PM, "Jorge De Flon" <ja...@yahoo.com> wrote:

> Hello,
> 
> I Apologize if my questions are too simple,

Well, it would be helpful if they were more specific rather than simple
since simple is relative.

> but i have not found the answers
> in the users guide (maybe i overlooked them)
> 1) Do you need apache to run velocity? can it run in other webserver (IIS?
> netscape?)

That is two different questions.

#1a. No.
#1b. Yes.

> 2) Do you need a servlet container?

The answer is that it depends on what you are trying to do.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


Simple questions

Posted by Jorge De Flon <ja...@yahoo.com>.
Hello,

I Apologize if my questions are too simple, but i have not found the answers
in the users guide (maybe i overlooked them)
1) Do you need apache to run velocity? can it run in other webserver (IIS?
netscape?)
2) Do you need a servlet container?

Any help or commentary is wellcome,
Thanks in advance.






_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Re: I'm having fun... now on to Caching

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/11/01 9:23 PM, "David Duddleston" <da...@i2a.com> wrote:

> Here is a scenario. I have a site where the content does not change alot,
> but it takes a huge load. Each page on the site consists of several included
> templates. Some of the included templates in that page will change alot and
> some don't change at all. Some of them are headers that are unique to the
> visitor and are used on almost every page on the site, but can be cached per
> session as once it is created it will remain unchanged for the whole
> session.

Ok.

> #parse(header.vm) ##cache per session... customized for user.
> #parse(current_time.vm) ##no cache... changes everytime.
> #parse(menu_home.vm) ##long term cache... rarely changes.
> #parse(home.vm) ##medium term cache... changes once a day.
> #parse(footer) ##long term cache... rarely changes.

Right, but this can't really be cached. There is no completely static
content here. The parsing of the template is cached, but caching the data in
the templates doesn't make sense because the templates are not static data.

If you have static non-parsed data, then use #include.

> So in short. There is page level caching which can be handled by the
> application framework and component level caching which may need to be
> intergrated into Velocity to work.

I don't think that is possible because there is no way to know what is
static and what isn't. Webmacro has a feature to "mark" what is static and
what isn't, but then you are completely breaking the MVC pattern and I would
be against adding something like that to Velocity as a standard part of it
because I don't think there is a lot of overall gain in it. Velocity builds
a proper AST so it understands which parts of the page are static TEXT and
which parts are other types of nodes and deals with them properly.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


RE: I'm having fun... now on to Caching

Posted by David Duddleston <da...@i2a.com>.
> I am interested to know what problem you are trying to solve where this
> is important.

Here is a scenario. I have a site where the content does not change alot,
but it takes a huge load. Each page on the site consists of several included
templates. Some of the included templates in that page will change alot and
some don't change at all. Some of them are headers that are unique to the
visitor and are used on almost every page on the site, but can be cached per
session as once it is created it will remain unchanged for the whole
session.

> The big problem, and what I think the entire problem will boil down to,
> is how the objects in the context change.

Right, this can be a hastle, but at least as the application developer, I
usualy know when and how data is changed and can invalidate that data given
that I have an interface that supports this. I can easily work around
Velocity to implement this part.

> It's an important thing, but I am not convinced it's a Velocity issue -
> it's more in the domain of a framework, I think.

The trick for this senario is to issolate and control individual templates.
If I have a template that includes 5 templates. I could get the results of
the whole page being rendered, but have no control of the included
templates.

## Velocity Script for Home page /index.vm

#parse(header.vm) ##cache per session... customized for user.
#parse(current_time.vm) ##no cache... changes everytime.
#parse(menu_home.vm) ##long term cache... rarely changes.
#parse(home.vm) ##medium term cache... changes once a day.
#parse(footer) ##long term cache... rarely changes.

In this case, the page needs to be rendered everytime, but only a few parts
of it need actual rendering.

This is just one type of caching. To implement this type of caching, each
template may need to have some caching properties and maybe a listener. I
think to have this sort of caching, it may need to be built into Velocity or
Velocity will need to be extened by the user which may be easy or difficult.

The above is just one way of caching based on a particular need. There are
other needs like the ability to cache renderings against the same template
but with differnt data in the context. An example would be how a message
forum would us a single Template to generate lots of HTML pages that
represent different messages. Each message uses a different dataset. Once
the output for message 1 is created, it can be cached and used the next time
it is requested. This kind of caching can be handled by the framework, if it
only entails page level caching. But if it includes the above example of
component or sub template caching, then you have some issue to work out.

So in short. There is page level caching which can be handled by the
application framework and component level caching which may need to be
intergrated into Velocity to work.

-david



Re: I'm having fun... now on to Caching

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Duddleston wrote:
> 
> In short... renderings. The caching could be built on top of Velocity and
> managed by the application using Velocity and not Velocity itself, but it
> might be possible to build some of this into Velocity.

I am interested to know what problem you are trying to solve where this
is important.

We have discussed this on and off for a while.  The problem always boils
down to not whether the context changed - that's easy - but if the data
that would be returned by the data objects in the context has changed.

If you know that, I think that it's a straightforward problem, for the
most part - given some parameterized request, you can simply store a
bytestream of the rendered template.

The catch, I think, will be the parameterization of the request, and
knowing if your data has changed.  I think that both of those prevent a
general solution.

> 
> I don't know the internals of Velocity, so I could be way off on what is
> feasible. I'm just thinking out load right now and wondered if anyone else
> has thought about ways to implement a rendered template/context cache. I
> would think there could be a way to determine if a context and the data it
> contains is still valid and that the Template has not changed since the last
> render against that context. 

It's pretty easy to know if the template has changed - as a matter of
fact, the File and Jar loaders handle this already.  As for the context,
you can easily implement your own context that will tell you last time
put() was called.

The big problem, and what I think the entire problem will boil down to,
is how the objects in the context change.

> If all is still valid and up-to-date, then
> Velocity would use a cached version of the rendering instead of rerendering
> it. The concept seems simple, but I realize there are some sticky issues to
> be worked out and it could be difficult. One sticky issue is with
> VelocityServlet or using Velocity in a Servlet enviroment. Normaly a context
> is created and the request and response objects are added along with any
> additional data the application provides. To determine if a cached version
> of the rendering could be used, a few checks would need to be made... has
> the Template or any child Templates/Macros been changed... is the Template
> using any data in the request or response objects, since these change on
> every request... is there a change to the data that would be placed into the
> context to be rendered.
> 
> If this is beyond the scope of Velocity, a stupid idea, not feasible, totaly
> retarded, then I will drop the subject ;-)
> 

It's an important thing, but I am not convinced it's a Velocity issue -
it's more in the domain of a framework, I think.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

RE: I'm having fun... now on to Caching

Posted by David Duddleston <da...@i2a.com>.
In short... renderings. The caching could be built on top of Velocity and
managed by the application using Velocity and not Velocity itself, but it
might be possible to build some of this into Velocity.

I don't know the internals of Velocity, so I could be way off on what is
feasible. I'm just thinking out load right now and wondered if anyone else
has thought about ways to implement a rendered template/context cache. I
would think there could be a way to determine if a context and the data it
contains is still valid and that the Template has not changed since the last
render against that context. If all is still valid and up-to-date, then
Velocity would use a cached version of the rendering instead of rerendering
it. The concept seems simple, but I realize there are some sticky issues to
be worked out and it could be difficult. One sticky issue is with
VelocityServlet or using Velocity in a Servlet enviroment. Normaly a context
is created and the request and response objects are added along with any
additional data the application provides. To determine if a cached version
of the rendering could be used, a few checks would need to be made... has
the Template or any child Templates/Macros been changed... is the Template
using any data in the request or response objects, since these change on
every request... is there a change to the data that would be placed into the
context to be rendered.

If this is beyond the scope of Velocity, a stupid idea, not feasible, totaly
retarded, then I will drop the subject ;-)

-david

> -----Original Message-----
> From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Friday, May 11, 2001 4:27 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: I'm having fun... now on to Caching
>
>
> David Duddleston wrote:
> >
> > First off. Geir, thank you so much for the quick and
> informative responses.
> >
> > I'm really getting the hang of Velocity now (thanks to some help) and my
> > next step would be to try and take advantage of caching some
> way... I'm such
> > a huge caching fan! I know caching is not currently implemented
> in Velocity
> > (well there is Template caching), but I remember reading that
> it is planned
> > and that Geir had some ideas about some possible approaches. I
> looked around
> > (the latest CVS version) and could not find any information other than a
> > brief mention in the todo.
>
> Caching what?
>
> --
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> "still climbing up to the shoulders..."


Re: I'm having fun... now on to Caching

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Duddleston wrote:
> 
> First off. Geir, thank you so much for the quick and informative responses.
> 
> I'm really getting the hang of Velocity now (thanks to some help) and my
> next step would be to try and take advantage of caching some way... I'm such
> a huge caching fan! I know caching is not currently implemented in Velocity
> (well there is Template caching), but I remember reading that it is planned
> and that Geir had some ideas about some possible approaches. I looked around
> (the latest CVS version) and could not find any information other than a
> brief mention in the todo.

Caching what?

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

I'm having fun... now on to Caching

Posted by David Duddleston <da...@i2a.com>.
First off. Geir, thank you so much for the quick and informative responses.

I'm really getting the hang of Velocity now (thanks to some help) and my
next step would be to try and take advantage of caching some way... I'm such
a huge caching fan! I know caching is not currently implemented in Velocity
(well there is Template caching), but I remember reading that it is planned
and that Geir had some ideas about some possible approaches. I looked around
(the latest CVS version) and could not find any information other than a
brief mention in the todo.

Thanks.

-david


Re: Syntax Help - how do I do this - days[i%days.length]

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Duddleston wrote:
> 
> I love Velocity and have figured out how to do most of the things I need to
> do, but I'm stuck on a few things... here is one of them
> 
> I can't figure out a way to do this in Velocity
> 
> Here is the JSP code.
> 
> <%
>     String[] days = {"Mon", "Tue", "Wed", "Thur", "Fri"};
> 
>     for(int i = 0; i < 28; i++)
>     {
>        out.println(days[i%days.length];
>     }
> %>

This is one of those examples where JSP outshines Velocity because of
the complicated code you must generate in Velocity's limited,
restrictive template language.

#set($days = ["Mon", "Tue", "Wed", "Thur", "Fri"] )

#foreach( $day in [0..27] )
   #set($which = $day % $days.size())
   $days.get($which)
#end

;->

The 'trick' to know is that the range operator and array operator
produce nice ArrayList objects, so they can do useful things for you.

>     or I do this...
> 
>     for(int i = 0; i < 28; i++)
>     {
>        out.println(rloop(i, days));
>     }
> 
>     public static String rloop(int count, String[] list)
>     {
>         return list[count%list.length];
>     }
> 
> If I take the macro approach, this is what I have.
> 
> Of course my rloop _wannabe_ Velocity macro is not legal and won't parse.
> I'm so lost in vmmacro land.
> 
> #macro ( rloop $count $list)
>      #set( $result = $list[$count%$list.length] )
>      $result
> #end
> 
> #set( $days = ["Mon", "Tue", "Wed", "Thur", "Fri"] )
> 
> #foreach ( $i in [0..15] )
>     #rloop($i $days)
> #end

You were on exactly the right track.  The only problems were to note
that unlike C, C++, Java, etc you can't drop a modulus anywhere - we
restrict them to proper expressions : #if(<expr>) and #set( <expr> ) and
that we don't support array access like [] because of various and sundry
safety concerns.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: Unhelpful messages on syntax errors

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Magnús Þór Torfason wrote:
> 
> I've been using Velocity in a Turbine webapp, but decided that I would have
> to check out the standalone setup, to get a hang of how Velocity is used
> without Turbine.
> 
> So, I setup the forumdemo (very neat!).
> 
> What strikes me is that if I modify the templates, so that there is a syntax
> error, I get the following error:
> 
> VelocityServlet : Error processing the template
> java.lang.Exception: Cannot find the template!
> 
> This happens in a completely standard drop-in installation of forumdemo, and
> when the syntax error is fixed, all is well again.
> 
> It is just that for first-timers, not knowing if you have a configuration
> error, or simply a bug in the template, can be pretty frustrating.
> 
> Any thoughts?

It helps us separate the wheat from the chaff...  I wanted to make it
"Not a typewriter", but no one would let me.

Seriously, I think it's pretty simple - we forgot to update forum demo
when we added groovy application exceptions. "in the beginning" we
didn't have good informative exceptions to propogate to the app layer
when something happened in getting or merging the template.

Now there is a set of application exceptions, the one you would want is
ParseErrorException.  I will take a look and update, or if  you have a
patch, so much the better :)

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: Unhelpful messages on syntax errors

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/11/01 6:33 AM, "Magnús Þór Torfason" <ma...@handtolvur.is> wrote:

> In my defence, I took a look at providing a patch but I'm really a total
> novice in the workings of Velocity, so I chickened out.

Never ever be afraid to provide a patch.

At the same time, never ever get so caught up in things that you can't take
someone telling you that your patch needs work and suggesting ways to fix
it.

:-)

We are all learning here. That is why we get so much work done.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


RE: Unhelpful messages on syntax errors

Posted by Magnús Þór Torfason <ma...@handtolvur.is>.
Geir, you spoil us rotten :)

In my defence, I took a look at providing a patch but I'm really a total
novice in the workings of Velocity, so I chickened out.

Thanks for amazingly quick responses to this and other velocity issues!

Cheers,

Magnus

> -----Original Message-----
> From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: 11. maí 2001 13:33
> To: velocity-user@jakarta.apache.org
> Subject: Re: Unhelpful messages on syntax errors
>
>
> Magnús Þór Torfason wrote:
> >
> > I've been using Velocity in a Turbine webapp, but decided that
> I would have
> > to check out the standalone setup, to get a hang of how Velocity is used
> > without Turbine.
> >
> > So, I setup the forumdemo (very neat!).
> >
> > What strikes me is that if I modify the templates, so that
> there is a syntax
> > error, I get the following error:
> >
> > VelocityServlet : Error processing the template
> > java.lang.Exception: Cannot find the template!
> >
> > This happens in a completely standard drop-in installation of
> forumdemo, and
> > when the syntax error is fixed, all is well again.
> >
> > It is just that for first-timers, not knowing if you have a
> configuration
> > error, or simply a bug in the template, can be pretty frustrating.
> >
> > Any thoughts?
>
> Fixed. I added the exceptions, and changed things so the error message
> (parsing, resource not found...) go to both console as well as
> browser...
>
> in current CVS
>
> geir
>
> --
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> "still climbing up to the shoulders..."


Re: Unhelpful messages on syntax errors

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Magnús Þór Torfason wrote:
> 
> I've been using Velocity in a Turbine webapp, but decided that I would have
> to check out the standalone setup, to get a hang of how Velocity is used
> without Turbine.
> 
> So, I setup the forumdemo (very neat!).
> 
> What strikes me is that if I modify the templates, so that there is a syntax
> error, I get the following error:
> 
> VelocityServlet : Error processing the template
> java.lang.Exception: Cannot find the template!
> 
> This happens in a completely standard drop-in installation of forumdemo, and
> when the syntax error is fixed, all is well again.
> 
> It is just that for first-timers, not knowing if you have a configuration
> error, or simply a bug in the template, can be pretty frustrating.
> 
> Any thoughts?

Fixed. I added the exceptions, and changed things so the error message
(parsing, resource not found...) go to both console as well as
browser...

in current CVS

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Unhelpful messages on syntax errors

Posted by Magnús Þór Torfason <ma...@handtolvur.is>.
I've been using Velocity in a Turbine webapp, but decided that I would have
to check out the standalone setup, to get a hang of how Velocity is used
without Turbine.

So, I setup the forumdemo (very neat!).

What strikes me is that if I modify the templates, so that there is a syntax
error, I get the following error:

VelocityServlet : Error processing the template
java.lang.Exception: Cannot find the template!

This happens in a completely standard drop-in installation of forumdemo, and
when the syntax error is fixed, all is well again.

It is just that for first-timers, not knowing if you have a configuration
error, or simply a bug in the template, can be pretty frustrating.

Any thoughts?

Magnus


Re: Syntax Help - how do I do this - days[i%days.length]

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/11/01 12:58 AM, "David Duddleston" <da...@i2a.com> wrote:

> 
> I love Velocity and have figured out how to do most of the things I need to
> do, but I'm stuck on a few things... here is one of them
> 
> 
> I can't figure out a way to do this in Velocity
> 
> Here is the JSP code.
> 
> <%
>   String[] days = {"Mon", "Tue", "Wed", "Thur", "Fri"};
> 
>   for(int i = 0; i < 28; i++)
>   {
>      out.println(days[i%days.length];
>   }
> %>
> 
>   or I do this...
> 
> 
>   for(int i = 0; i < 28; i++)
>   {
>      out.println(rloop(i, days));
>   }
> 
>   public static String rloop(int count, String[] list)
>   {
>       return list[count%list.length];
>   }

Embedding Java code in your JSP page? Tisk Tisk Tisk ...well at least you
found Velocity... :-)

> If I take the macro approach, this is what I have.
> 
> Of course my rloop _wannabe_ Velocity macro is not legal and won't parse.
> I'm so lost in vmmacro land.
> 
> #macro ( rloop $count $list)
>    #set( $result = $list[$count%$list.length] )
>    $result
> #end
> 
> #set( $days = ["Mon", "Tue", "Wed", "Thur", "Fri"] )
> 
> #foreach ( $i in [0..15] )
>   #rloop($i $days)
> #end

$list is an ArrayList object...so treat it as that...

$list.get($count % $list.length)

Now, I don't think you can embed math logic into the () so you may need to
do this:

#set ( $foo = $count % $list.length )
$list.get($foo)

If that doesn't work, then you need to define an object and put it into the
context to do that for you...

#set ( $foo = $math.mod ( $count, $list.length ) )
$list.get($foo)

I hope that helps.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


Syntax Help - how do I do this - days[i%days.length]

Posted by David Duddleston <da...@i2a.com>.
I love Velocity and have figured out how to do most of the things I need to
do, but I'm stuck on a few things... here is one of them


I can't figure out a way to do this in Velocity

Here is the JSP code.

<%
    String[] days = {"Mon", "Tue", "Wed", "Thur", "Fri"};

    for(int i = 0; i < 28; i++)
    {
       out.println(days[i%days.length];
    }
%>

    or I do this...


    for(int i = 0; i < 28; i++)
    {
       out.println(rloop(i, days));
    }

    public static String rloop(int count, String[] list)
    {
        return list[count%list.length];
    }


If I take the macro approach, this is what I have.

Of course my rloop _wannabe_ Velocity macro is not legal and won't parse.
I'm so lost in vmmacro land.

#macro ( rloop $count $list)
     #set( $result = $list[$count%$list.length] )
     $result
#end

#set( $days = ["Mon", "Tue", "Wed", "Thur", "Fri"] )

#foreach ( $i in [0..15] )
    #rloop($i $days)
#end



-david



RE: conditional operator

Posted by David Duddleston <da...@i2a.com>.
Solved the formating issue. See my other message.

-david

> -----Original Message-----
> From: David Duddleston [mailto:david@i2a.com]
> Sent: Thursday, May 10, 2001 11:11 PM
> To: velocity-user@jakarta.apache.org
> Subject: conditional operator
>
>
>
> First off. Thanks to the authors of Velocity, I'm very pleased with it. I
> have tried several front end alternative to JSP like XMLC, XSLT,
> ECS... and
> have never been satisfied, but Velocity is the first one that
> seems to find
> the perfect balance of flexiblity and simplicity for my project needs.
>
>
> Now to my question.
>
> To setup tables with rotating color rows (blue/grey) I normaly use a
> conditional
> operator like the following.
>
> String bgcolor = (i%2==0)?"#66CCFF":"#EEEEEE";
>
> By habit I want to do the following
>
> #set( $bgcolor = ($velocityCount%2==0)?"#66CCFF":"#EEEEEE")
>
> This of course is not supported, so I do the following.
>
> #if( $velocityCount%2==0 )
>     #set( $bgcolor = "#66CCFF")
> #else
>     #set( $bgcolor = "#FFFFFF")
> #end
>
> Is there a trick to keep this small. I tried to setup a macro, but the end
> results in messed up formating and the causes a bug in IE.
>
> #macro( bgc $count )
> #if( $count%2==0 )
> #66CCFF#else
> #EEEEEE#end
> #end
>
> This
>
>  <td bgcolor="#bgc($velocityCount)"> blaa blaa blaa blaa blaa blaa </td>
>
> ends up as
>
>  <td bgcolor="    #66CCFF"> blaa blaa blaa blaa blaa blaa </td>
>
> having the space causes a bug in IE 5. Note that I tried to
> format the macro
> several different ways and always ended up with the extra space.
>
>
>
>
>
>
>
> Here is an example in context.
>
> <html>
> <head><title>Example</title></head>
> <body bgcolor="#ffffff">
>
> #set( $days = ["Mon", "Tue", "Wed", "Thur", "Fri"] )
>
> #macro( bgc $count )
> #if( $count%2==0 )
> #66CCFF#else
> #EEEEEE#end
> #end
>
> <table cellspacing="0" cellpadding="5">
>     <tr>
>         <td bgcolor="#eebbbb" align="center"><b>Days</b></td>
>         <td bgcolor="#eebbbb" align="center"><b>Description</b></td>
>     </tr>
>     #foreach ($day in $days)
>
>         ##set( $bgcolor = ($velocityCount%2==0)?"#66CCFF":"#EEEEEE")
>
>         #if( $velocityCount%2==0 )
>             #set( $bgcolor = "#66CCFF")
>         #else
>             #set( $bgcolor = "#EEEEEE")
>         #end
>
>     <tr>
>         <td bgcolor="$bgcolor">$day</td>
>         <td bgcolor="#bgc($velocityCount)"> blaa blaa blaa blaa blaa blaa
> </td>
>     </tr>
>     #end
> </table>
> </html>
>
>
> -david


Re: conditional operator - Macro Format Mystery Solved

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Duddleston wrote:
> 
> Ahhh... in my first attempt to use a macro, I did not realize the macro was
> sort of cached. Figured out that I need to reload the Servlet to use a new
> macro with the same name. Anyway when I changed this macro
> 
> #macro( bgc $count )
> #if( $count%2==0 )
> #66CCFF#else
> #EEEEEE#end
> #end
> 
> to this
> 
> #macro( bgc $count )
> #if( $count%2==0 )#66CCFF#else#EEEEEE#end
> #end
> 
> and reloaded the Servlet... it took the new format. Now I'll have to see if
> there is a property regarding macro being cached.
> 

Yes, the default setting is that a global macro is protected from being
overwritten.

TO change this, set the property

velocimacro.permissions.allow.inline.to.replace.global  to true

OR

velocimacro.permissions.allow.inline.local.scope  ->  true

should prevent your macros from becoming 'global' in the first place.

To set these, and you are using the default mechanism in
VelocityServlet, just add them to your properties file, or do it
directly, a la

Velocity.setProperty("velocimacro.permissions.allow.inline.to.replace.global",
Boolean.TRUE );

in the right place.  Where 'the right place' is depends on your
initialization strategy.  Lately, out of sheer lazyness, I have been
overriding loadConfiguration(), and doing the entire setup of the
runtime engine in there, calling Velocity.init() to make sure it's
done.  The servlet init() will subsequently try to set properties and
call init, but since I have already init()-ed, it's done.

I guess we should add a little initVelocity() method call that you can
override.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

RE: conditional operator - Macro Format Mystery Solved

Posted by David Duddleston <da...@i2a.com>.
Ahhh... in my first attempt to use a macro, I did not realize the macro was
sort of cached. Figured out that I need to reload the Servlet to use a new
macro with the same name. Anyway when I changed this macro

#macro( bgc $count )
#if( $count%2==0 )
#66CCFF#else
#EEEEEE#end
#end

to this

#macro( bgc $count )
#if( $count%2==0 )#66CCFF#else#EEEEEE#end
#end

and reloaded the Servlet... it took the new format. Now I'll have to see if
there is a property regarding macro being cached.

-david

> -----Original Message-----
> From: David Duddleston [mailto:david@i2a.com]
> Sent: Thursday, May 10, 2001 11:11 PM
> To: velocity-user@jakarta.apache.org
> Subject: conditional operator
>
>
>
> First off. Thanks to the authors of Velocity, I'm very pleased with it. I
> have tried several front end alternative to JSP like XMLC, XSLT,
> ECS... and
> have never been satisfied, but Velocity is the first one that
> seems to find
> the perfect balance of flexiblity and simplicity for my project needs.
>
>
> Now to my question.
>
> To setup tables with rotating color rows (blue/grey) I normaly use a
> conditional
> operator like the following.
>
> String bgcolor = (i%2==0)?"#66CCFF":"#EEEEEE";
>
> By habit I want to do the following
>
> #set( $bgcolor = ($velocityCount%2==0)?"#66CCFF":"#EEEEEE")
>
> This of course is not supported, so I do the following.
>
> #if( $velocityCount%2==0 )
>     #set( $bgcolor = "#66CCFF")
> #else
>     #set( $bgcolor = "#FFFFFF")
> #end
>
> Is there a trick to keep this small. I tried to setup a macro, but the end
> results in messed up formating and the causes a bug in IE.
>
> #macro( bgc $count )
> #if( $count%2==0 )
> #66CCFF#else
> #EEEEEE#end
> #end
>
> This
>
>  <td bgcolor="#bgc($velocityCount)"> blaa blaa blaa blaa blaa blaa </td>
>
> ends up as
>
>  <td bgcolor="    #66CCFF"> blaa blaa blaa blaa blaa blaa </td>
>
> having the space causes a bug in IE 5. Note that I tried to
> format the macro
> several different ways and always ended up with the extra space.
>
>
>
>
>
>
>
> Here is an example in context.
>
> <html>
> <head><title>Example</title></head>
> <body bgcolor="#ffffff">
>
> #set( $days = ["Mon", "Tue", "Wed", "Thur", "Fri"] )
>
> #macro( bgc $count )
> #if( $count%2==0 )
> #66CCFF#else
> #EEEEEE#end
> #end
>
> <table cellspacing="0" cellpadding="5">
>     <tr>
>         <td bgcolor="#eebbbb" align="center"><b>Days</b></td>
>         <td bgcolor="#eebbbb" align="center"><b>Description</b></td>
>     </tr>
>     #foreach ($day in $days)
>
>         ##set( $bgcolor = ($velocityCount%2==0)?"#66CCFF":"#EEEEEE")
>
>         #if( $velocityCount%2==0 )
>             #set( $bgcolor = "#66CCFF")
>         #else
>             #set( $bgcolor = "#EEEEEE")
>         #end
>
>     <tr>
>         <td bgcolor="$bgcolor">$day</td>
>         <td bgcolor="#bgc($velocityCount)"> blaa blaa blaa blaa blaa blaa
> </td>
>     </tr>
>     #end
> </table>
> </html>
>
>
> -david


conditional operator

Posted by David Duddleston <da...@i2a.com>.
First off. Thanks to the authors of Velocity, I'm very pleased with it. I
have tried several front end alternative to JSP like XMLC, XSLT, ECS... and
have never been satisfied, but Velocity is the first one that seems to find
the perfect balance of flexiblity and simplicity for my project needs.


Now to my question.

To setup tables with rotating color rows (blue/grey) I normaly use a
conditional
operator like the following.

String bgcolor = (i%2==0)?"#66CCFF":"#EEEEEE";

By habit I want to do the following

#set( $bgcolor = ($velocityCount%2==0)?"#66CCFF":"#EEEEEE")

This of course is not supported, so I do the following.

#if( $velocityCount%2==0 )
    #set( $bgcolor = "#66CCFF")
#else
    #set( $bgcolor = "#FFFFFF")
#end

Is there a trick to keep this small. I tried to setup a macro, but the end
results in messed up formating and the causes a bug in IE.

#macro( bgc $count )
#if( $count%2==0 )
#66CCFF#else
#EEEEEE#end
#end

This

 <td bgcolor="#bgc($velocityCount)"> blaa blaa blaa blaa blaa blaa </td>

ends up as

 <td bgcolor="    #66CCFF"> blaa blaa blaa blaa blaa blaa </td>

having the space causes a bug in IE 5. Note that I tried to format the macro
several different ways and always ended up with the extra space.







Here is an example in context.

<html>
<head><title>Example</title></head>
<body bgcolor="#ffffff">

#set( $days = ["Mon", "Tue", "Wed", "Thur", "Fri"] )

#macro( bgc $count )
#if( $count%2==0 )
#66CCFF#else
#EEEEEE#end
#end

<table cellspacing="0" cellpadding="5">
    <tr>
        <td bgcolor="#eebbbb" align="center"><b>Days</b></td>
        <td bgcolor="#eebbbb" align="center"><b>Description</b></td>
    </tr>
    #foreach ($day in $days)

        ##set( $bgcolor = ($velocityCount%2==0)?"#66CCFF":"#EEEEEE")

        #if( $velocityCount%2==0 )
            #set( $bgcolor = "#66CCFF")
        #else
            #set( $bgcolor = "#EEEEEE")
        #end

    <tr>
        <td bgcolor="$bgcolor">$day</td>
        <td bgcolor="#bgc($velocityCount)"> blaa blaa blaa blaa blaa blaa
</td>
    </tr>
    #end
</table>
</html>


-david