mardi 1 décembre 2015

grails reflection - Calling a dynamic tag

I know this is odd, but I'm trying to provide a pass through interface in a taglib that allows the caller to pass in any other tag to be displayed in a container with additional processing.

To this effect, I'm trying to dynamically call an arbitrary tag in an arbitrary namespace. This may be clearer by example.

GSP:

<myLib:myTag someProp="blah" anotherProp="blah2" size="80" namespace="g" tag="textField">

In my taglib, I'm trying to display the tag they pass.

Taglib:

def myTag = {
  String id = //some processing, not specified by caller
  attrs.put("id", id)
  def namespace = attrs.remove("namespace")
  def tag = attrs.remove("tag")
  out << ?????
}

The problem comes after the out... I'm having trouble calling the tag. I've tried the following with the following errors

namespace.tag(attrs) //No signature of method: java.lang.String.tag()
namespace."${tag}"(attrs) //No signature of method: java.lang.String.textField()
"${namespace}"."${tag}"(attrs) //No signature of method: java.lang.String.textField()

This seems to work, but the method needs to support tags in other namespaces

g."${tag}"(attrs)

So the question is How can I use reflection to use a dynamically defined taglib?

I don't have them pass the fully formed tag in the body() because I need to interact with it in the taglib.





Aucun commentaire:

Enregistrer un commentaire