Grails 3 static compilation of taglib

332 views Asked by At

I'm trying to apply @GrailsCompileStatic to taglib and geting the following error:

Error:(19, 16) Groovyc: [Static type checking] - Cannot find matching method com.tempvs.image.MyTagLib#render(java.util.LinkedHashMap ). Please check if the declared type is right and if the method exists.

Code example:

@GrailsCompileStatic
class MyTagLib {
    ...
    String myTag = { Map attrs ->
        ...
        out << render(template: '/templates/myTemplate', model: [...])
    }
}

What am I doing wrong and how can I solve the problem?

2

There are 2 answers

1
Daniel On BEST ANSWER

You are using some dynamic features, because taglibs in general do use them. Even just calling "render" is in a sense dynamic.

If you really want to, you can work around this by injecting a PageRenderer and using that to render your page, and then outputting the resulting HTML. I'm not sure it would be worth it, but I certainly don't know your performance requirements!

Example:

import grails.compiler.GrailsCompileStatic
import grails.gsp.PageRenderer

@GrailsCompileStatic
class StaticTestTagLib {

    static namespace = "staticTest"

    PageRenderer groovyPageRenderer

    String myTag = { Map attrs ->
        out << groovyPageRenderer.render(template: '/templates/myTemplate', model: [...])

    }
}

This works, but you will have some work ahead of you if your taglib is doing anything remotely complex, because EVERY call to render, or any other tag is going to have to be replaced by a statically compiled call. It's possible, but possibly not hugely beneficial.

1
elixir On

I am not sure what is the point in creating a new tag that just renders a template.

Grails already has a tag library that renders a template.

   <g:render template="displaybook" model="['book':book,'author':author]" />

https://docs.grails.org/latest/ref/Tags/render.html