Testing for nonexistent properties in groovy templates

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing for nonexistent properties in groovy templates

Galen Dunkleberger

I’m new to groovy templates so I’m hoping there is an obvious answer to my question. Say I have a template like this…

 

<%if(notAlwaysAvailable!=null){%>

<div>

            ${notAlwaysAvailable}

</div>

<%}%>

 

I would expect that if the notAlwaysAvailable property didn’t existing in the bindings for the template that the statement would evaluate to false. What seems to be happening is I’m getting  a “groovy.lang.MissingPropertyException: No such property” error. I thought maybe there would be some way to register an error handler on the template so that I could pick and choose how to handle such errors and determine whether the template should still be rendered but it doesn’t appear there are any such hooks in. So does this mean that if I write a groovy template I have to ensure that every property  in it is at specified in the bindings of the template even if the property is null or is there some way to deal with nonexistent properties?

                                                           

                                                                                    Thanks,

                                                                                     Galen

Reply | Threaded
Open this post in threaded view
|

Re: Testing for nonexistent properties in groovy templates

graemer
IMO this is the wrong behaviour and SimpleTemplateEngine should be changed.

I discovered this when developing GSP for Grails and actually changed
the templates MetaClass to return null instead of throwing the
exception.

Graeme

On 7/25/06, Galen Dunkleberger <[hidden email]> wrote:

>
>
>
>
> I'm new to groovy templates so I'm hoping there is an obvious answer to my
> question. Say I have a template like this…
>
>
>
> …
>
> <%if(notAlwaysAvailable!=null){%>
>
> <div>
>
>             ${notAlwaysAvailable}
>
> </div>
>
> <%}%>
>
> …
>
>
>
> I would expect that if the notAlwaysAvailable property didn't existing in
> the bindings for the template that the statement would evaluate to false.
> What seems to be happening is I'm getting  a
> "groovy.lang.MissingPropertyException: No such property"
> error. I thought maybe there would be some way to register an error handler
> on the template so that I could pick and choose how to handle such errors
> and determine whether the template should still be rendered but it doesn't
> appear there are any such hooks in. So does this mean that if I write a
> groovy template I have to ensure that every property  in it is at specified
> in the bindings of the template even if the property is null or is there
> some way to deal with nonexistent properties?
>
>
>
>
>                         Thanks,
>
>
>                          Galen

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Testing for nonexistent properties in groovy templates

Guillaume Laforge
Administrator
In reply to this post by Galen Dunkleberger
No particular support out of the box to controle the behaviour there.
But you can either create your own Binding, or own Template engine.
Another solution is to use the more verbose syntax:
if (binding.variables.contains("notAlwaysAvailable") {...}

On 7/25/06, Galen Dunkleberger <[hidden email]> wrote:

>
>
>
>
> I'm new to groovy templates so I'm hoping there is an obvious answer to my
> question. Say I have a template like this…
>
>
>
> …
>
> <%if(notAlwaysAvailable!=null){%>
>
> <div>
>
>             ${notAlwaysAvailable}
>
> </div>
>
> <%}%>
>
> …
>
>
>
> I would expect that if the notAlwaysAvailable property didn't existing in
> the bindings for the template that the statement would evaluate to false.
> What seems to be happening is I'm getting  a
> "groovy.lang.MissingPropertyException: No such property"
> error. I thought maybe there would be some way to register an error handler
> on the template so that I could pick and choose how to handle such errors
> and determine whether the template should still be rendered but it doesn't
> appear there are any such hooks in. So does this mean that if I write a
> groovy template I have to ensure that every property  in it is at specified
> in the bindings of the template even if the property is null or is there
> some way to deal with nonexistent properties?
>
>
>
>
>                         Thanks,
>
>
>                          Galen


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

RE: Testing for nonexistent properties in groovy templates

Galen Dunkleberger
In reply to this post by graemer
I should have emailed you guys earlier I spent much of the day trying to
figure out how to get that to work. I just created my own template engine
and custom binding which returns null if the property is not present. Works
great now.

I have to agree with Graeme that it would be nice if this behaviour was
changed or at least configurable. Most other templating engines don't throw
exceptions when a property is not bound.

Graeme I thought about also change the MetaClass but wasn't sure the best
way to go about doing this. Did you just change the source directly and work
with a custom groovy build or did you extend the MetaClass and hook it into
your own template engine?
                               
                                                Thanks for the help,
                                                        Galen
               
-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of
Graeme Rocher
Sent: Tuesday, July 25, 2006 3:52 PM
To: [hidden email]
Subject: Re: [groovy-user] Testing for nonexistent properties in groovy
templates

IMO this is the wrong behaviour and SimpleTemplateEngine should be changed.

I discovered this when developing GSP for Grails and actually changed
the templates MetaClass to return null instead of throwing the
exception.

Graeme

On 7/25/06, Galen Dunkleberger <[hidden email]> wrote:

>
>
>
>
> I'm new to groovy templates so I'm hoping there is an obvious answer to my
> question. Say I have a template like this.
>
>
>
> .
>
> <%if(notAlwaysAvailable!=null){%>
>
> <div>
>
>             ${notAlwaysAvailable}
>
> </div>
>
> <%}%>
>
> .
>
>
>
> I would expect that if the notAlwaysAvailable property didn't existing in
> the bindings for the template that the statement would evaluate to false.
> What seems to be happening is I'm getting  a
> "groovy.lang.MissingPropertyException: No such property"
> error. I thought maybe there would be some way to register an error
handler
> on the template so that I could pick and choose how to handle such errors
> and determine whether the template should still be rendered but it doesn't
> appear there are any such hooks in. So does this mean that if I write a
> groovy template I have to ensure that every property  in it is at
specified

> in the bindings of the template even if the property is null or is there
> some way to deal with nonexistent properties?
>
>
>
>
>                         Thanks,
>
>
>                          Galen

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Testing for nonexistent properties in groovy templates

Jochen Theodorou
Galen Dunkleberger schrieb:
[...]
> I have to agree with Graeme that it would be nice if this behaviour was
> changed or at least configurable. Most other templating engines don't throw
> exceptions when a property is not bound.

well of this is because templates are converted to normal scripts, but
while in scripts you may want to be informed of using an invalid
variable you may not want it in case of a template. but changing this
for GSP only is a good idea I think.

bye blackdrag

--
Jochen Theodorou
Groovy Tech Lead

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email