SimpleTemplateEngine: How to preserve $strings in template

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

SimpleTemplateEngine: How to preserve $strings in template

mj454
Hi

I'm using SimpleTemplateEngine and want to convert:

    "I'd like this <%REQ_BIND%> but leave $thisone alone"
into
    "I'd like this transformed but leave $thisone alone"

But when I execute it, I get:

groovy.lang.MissingPropertyException: No such property: thisone for class: Script1
        at Script1.run(Script1.groovy:4)

I've posted this at: http://groovyconsole.appspot.com/script/1091001

I've tried to get around this by passing in "$thisone" as a value to the binding, but it recursively parse that too and I still get the same error (http://groovyconsole.appspot.com/script/1092001)


1) Why is it trying to convert "$thisone"?  The docs say it supports <% %> & <%= %> syntax only
2) How can I get it to ignore it?

Thanks in advance,

Matt


Reply | Threaded
Open this post in threaded view
|

Re: SimpleTemplateEngine: How to preserve $strings in template

Guillaume Laforge-4
You can escape the dollar sign with a backslash: \$thisone


On Wed, Sep 25, 2013 at 9:39 AM, mj454 <[hidden email]> wrote:
Hi

I'm using SimpleTemplateEngine and want to convert:

    "I'd like this <%REQ_BIND%> but leave $thisone alone"
into
    "I'd like this transformed but leave $thisone alone"

But when I execute it, I get:

groovy.lang.MissingPropertyException: No such property: thisone for class:
Script1
        at Script1.run(Script1.groovy:4)

I've posted this at: http://groovyconsole.appspot.com/script/1091001

I've tried to get around this by passing in "$thisone" as a value to the
binding, but it recursively parse that too and I still get the same error
(http://groovyconsole.appspot.com/script/1092001)


1) Why is it trying to convert "$thisone"?  The docs say it supports <% %> &
<%= %> syntax only
2) How can I get it to ignore it?

Thanks in advance,

Matt






--
View this message in context: http://groovy.329449.n5.nabble.com/SimpleTemplateEngine-How-to-preserve-strings-in-template-tp5717067.html
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email





--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Reply | Threaded
Open this post in threaded view
|

Re: SimpleTemplateEngine: How to preserve $strings in template

mj454
Thanks Guillaume.

That hasn't worked though.  I'm still getting the same error (http://groovyconsole.appspot.com/script/1093001)

Any other ideas?

Matt
Reply | Threaded
Open this post in threaded view
|

Re: SimpleTemplateEngine: How to preserve $strings in template

Jochen Theodorou
Am 25.09.2013 09:52, schrieb mj454:
> Thanks Guillaume.
>
> That hasn't worked though.  I'm still getting the same error
> (http://groovyconsole.appspot.com/script/1093001)

createTemplate creates the template, the following make, produces the
final result. There then everything will be bound, including your
thisone. If you want it without "thisone" being bound, you have to stop
at createTemplate

bye blackdrag

--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: SimpleTemplateEngine: How to preserve $strings in template

paulk_asert
In reply to this post by mj454

There are probably numerous ways. Here's one (perhaps not the simplest):
println engine.createTemplate($/I'd like this <%=REQ_BIND%> but to leave \$$thisone alone/$).make(bindings).toString()

On 25/09/2013 5:52 PM, mj454 wrote:

> Thanks Guillaume.
>
> That hasn't worked though.  I'm still getting the same error
> (http://groovyconsole.appspot.com/script/1093001)
>
> Any other ideas?
>
> Matt
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/SimpleTemplateEngine-How-to-preserve-strings-in-template-tp5717067p5717069.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> 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: SimpleTemplateEngine: How to preserve $strings in template

mj454
Thanks for all the input and prompt responses guys.

Paulk - very neat.  Will let you know once I've understood exactly what you've done there :-)

Also, as this is basically just string substitution (and I'm not worried about performance here), this function works a treat too:

def parseTemplate(template, bindings) {
 
   String parsedTemplate = template
   bindings.each() {
      key, value ->
      parsedTemplate = parsedTemplate.replace("<%" + key + "%>", value)
   }
   return parsedTemplate
}
Reply | Threaded
Open this post in threaded view
|

Re: SimpleTemplateEngine: How to preserve $strings in template

Raffaele Sgarro
Less contrived

"I'd like this <%REQ_BIND%> but to leave ${'thisone'} alone"


2013/9/25 mj454 <[hidden email]>
Thanks for all the input and prompt responses guys.

Paulk - very neat.  Will let you know once I've understood exactly what
you've done there :-)

Also, as this is basically just string substitution (and I'm not worried
about performance here), this function works a treat too:

def parseTemplate(template, bindings) {

   String parsedTemplate = template
   bindings.each() {
      key, value ->
      parsedTemplate = parsedTemplate.replace("<%" + key + "%>", value)
   }
   return parsedTemplate
}



--
View this message in context: http://groovy.329449.n5.nabble.com/SimpleTemplateEngine-How-to-preserve-strings-in-template-tp5717067p5717072.html
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: SimpleTemplateEngine: How to preserve $strings in template

Raffaele Sgarro
Sorry, there was a typo. Should be <%=REQ_BIND%>
Working on ideone: http://ideone.com/Dh12hc


2013/9/25 Raffaele Sgarro <[hidden email]>
Less contrived

"I'd like this <%REQ_BIND%> but to leave ${'thisone'} alone"


2013/9/25 mj454 <[hidden email]>
Thanks for all the input and prompt responses guys.

Paulk - very neat.  Will let you know once I've understood exactly what
you've done there :-)

Also, as this is basically just string substitution (and I'm not worried
about performance here), this function works a treat too:

def parseTemplate(template, bindings) {

   String parsedTemplate = template
   bindings.each() {
      key, value ->
      parsedTemplate = parsedTemplate.replace("<%" + key + "%>", value)
   }
   return parsedTemplate
}



--
View this message in context: http://groovy.329449.n5.nabble.com/SimpleTemplateEngine-How-to-preserve-strings-in-template-tp5717067p5717072.html
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email