last day (15 days later) » 

9:23 PM
0
Q: Evaluate String from database in taglib in Grails

Lojza IbgI have a taglib method and I fetch an object from database with string expressions to evaluate. From the docs, it should be possible to do sth like this: out << "<div id=\"${attrs.book.id}\">" But when I try to do the same for the object fetched from database, the expression between ${} does n...

 
I got this: <div id="666">
Yes, I also wrote that it could be that the db object contains string which is then not evaluated. The problem is when I convert it to GString, it is also not evaluated.
 
Do you mean that the objectFromDb.getContent() method returns a string that should get evaluated by the GStringTemplateEngine, but you don't know what binding variables will be needed for that evaluation ?
 
@sensei yes, I want to evaluate a String that is returned by objectFromDb.getContent(). If it's a normal GString variable created in a taglib method, you can do sth like this out << "<a href='${g.createLink(sth: sth)}'>link</a>" and it will evaluate correctly, right? But if it's saved into db and then I get it later, I need to save it as String and then it's returned as String. However it cannot be evaluated anymore since it's just a string.
@TarasKohut if it's a GString, then it's evaluated correctly. But if I save this GString into database, I need to save it as String since I cannot save GString. Then when I retrieve this saved String back from database, I don't have GString anymore. And it will print <div id=\"${attrs.book.id}\"> without expression being correctly evaluated.
 
Wouldn't it work is you used the template engine's make() passing it the current binding ? i.e. engine.createTemplate(objectFromDb.content).make(this.bindin‌​g.variables)
 
@sensei what do you mean by this this.bindin‌​g.variables? And how can I use other taglibs, e.g. when I want to have out << "<a href='${g.createLink(sth: sth)}'>link</a>" in objectFromDb.content?
 
9:23 PM
this.binding is an instance of docs.groovy-lang.org/latest/html/api/groovy/lang/Binding.htm‌​l and should contain your unknown variable (otherwise we need to know where you get it from). Since g is probably one of them, you should be able to use other taglibs closures. But the best way to know for sure is to try. Let me know if it works.
 
@sensei unfortunately it's not working. It throws this error: groovy.lang.MissingPropertyException: No such property: binding for class: my.package.MyTagLib. Seems like this.binding is not available in the taglib.
 
Yes, my bad, Binding only happens in scripts, I think. Where would you get your variable from actually ?
Is it only defined in the page that's calling the tag ?
 
for example, when you evaluate a GString in a taglib, you can use g:link, grailsApplication etc.
I would like to create a dynamic GSP where the content is saved in the database and will be rendered using a taglib
so instead of putting all the content into a static GSP file, the content will be loaded from the database and then evaluated in a taglib. Therefore I would like to have an option use all the variables that I can use in GSP.
 
9:41 PM
In the groovy page, you should have access to this.binding (since the page is evaluated inside a TemplateEngine (I think). From there, you could pass it to your own specially crafted GSP tag that will load content.
Let's say you have build a taglib with this:
loadTemplate = { binding -> engine.createTemplate(fetchObjectFromDb().content).make(binding) }
You should be able to do: <myTagLib:loadTemplate binding="${this.binding}" /> in you GSP.
Actually, scrap the taglib definition. It's wrong.
 
I will give it a try
 
Should be: def loadTemplate = { attrs, body ->
out << engine.createTemplate(fetchObjectFromDb().content).make(attrs.binding)
}
Of course, you need to instantiate engine first.
 
yes
let me try
 
It's been wuite a long time since I've played with Grails...
 
so attrs.binding exists, but it does not have e.g. grailsApplication, which is available in the GSP as well as in the taglibs
sorry actually I'm wrong. attrs.binding does not exist
it's null
 
9:55 PM
Well, sorry, no other idea at the moment.
 
ok, thanks for your help anyway
 
10:11 PM
I did some looking into TemplateEngine. Try again what I said but replace ${this.binding} with ${delegate}.
You should be able to list all variables passed to your original GSP with delegate.variables.
 

  last day (15 days later) »