logo       

RE: parsing variable name in a velocity template: msg#00124

jakarta.velocity.user

Subject: RE: parsing variable name in a velocity template

> I have a very
> specific need to know when there are variables in the template that I
> (the programmer) don't or didn't know about...
>
> We use Velocity to generate our (opt-in, lemme alone!) email
> newsletter.
> In order to facilitate idiot-proofing I have to know if the template
> designer (definitely NOT a programmer :) either mis-spelled something,
> left something out, or added something that I don't provide.
>
> This knowledge allows us to unit-test (sorta) our email
> templates (which
> we use for every email our site sends; registration confirmation,
> newsletters, etc.) and make sure that an email template hasn't been
> broken by me or the template designer.

It seems like a very simple solution that would be adequate for your needs
would be to write a method which:

1. Creates a context with dummy values for the variables you know
2. merges that context with the template to get the resulting output
3. use a regular expression engine (like java.util.regex in java 1.4) to
parse through the template output for ""\\s\\$\\S+"
4. each occurrence of the pattern is an unmatched variable. Add them to a
Set
5. return the Set

While I cannot share my code I can tell you that this can be done in 10
straightforward lines.

-Moshe


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise