jastrachan | 7 May 12:04 2004

Re: GString and null

On 7 May 2004, at 09:31, Igor I. Nuzhnov wrote:
> Hello, all
> For two years we use Velocity in our projects.
> But there are so many bugs in Velocity!!!
> So, we decide to write library with Velocity interface that translates 
> velocity templates into groovy source and than compiles that source 
> and run it.

Coolbeans! Hey do you fancy wrapping up your velocity-style template 
engine as a pluggable groovy template engine?


we've a default implementation which uses ASP / JSP style notation <% 
%> etc. Having a #if () velocity style would rock!


> Now almost velocity syntax work, but we have some problems:
> In velocity there is an operator $!{expression}
> If expression evaluated as null than no text inserted into result 
> stream
> But in groovy result of ${expression} is "null" if expression is null.
> How can I achieve this velocity functionality?

When you parse the template you could translate


to be


and then have a method

nonNullExpressionsAsText(value) {
     return (value != null) ? value.toString() : ""

Incidentally, several people have brought up the issue when using 
expressions inside strings inside Groovy itself, that handling nulls 
could be better (e.g. often people want to output nothing rather than 
'null' for null expressions. So it might well be worth adding $!{expr} 
notation to groovy itself as it seems a neat idea.