tags:

views:

668

answers:

3

Is possible to overwrite the behaviour of the methods CreateLink and CreateLinkTo ?

How?

Thanks in advance,

Luis

+2  A: 

You could use meta programming to replace the closure on ApplicationTaglib.

ApplicationTagLib.metaClass.getCreateLink = {->
  return {attrs->
         // your code here
  }
}

I've never tried it but it might work :)

leebutts
+1  A: 

All you need to do is create a taglib of your own and define the tags yourself ie

class MyTabLib {
  def createLink = {attrs, body ->
   .... etc ....
  }

  def createLinkTo = {attrs, body ->
   .... etc ....
  }

}

Grails will use your taglib first.

Hope this Helps!

Scott Warren
thanks for you answer. Could you specify how to call the original createLink inside this tag lig. I only need to modify the generated link only adding a prefix. (+1)
Luixv
Your should be able to do something likedef createLink = {attrs, body -> def apptag = new ApplicationTagLib() out << prefix; out << apptag.createLink(attrs,body);}
Scott Warren
+1  A: 

This is a little late, but the solutions above didn't work for me. I was able to successfully do this, though:

public class MyTagLib extends ApplicationTagLib {

  def oldResource

  public MyTagLib() {
    // save the old 'resource' value
    oldResource = resource;
    resource = staticResource;
  }

  def staticResource = { attrs ->
    // dork with whatever you want here ...
    // ...
    out << oldResource(attrs);
 }
}

you're basically extending the original tag lib. Since the 'resource' tag is a property of the object (and not a method) I don't think you can actually override it. Instead, just save the original value and call it after you've make your changes to the tag request.

Eric Lambrecht
oh.. I was overriding the 'resource' tag, but this should work just as well for 'createLink' or 'createLinkTo' (the deprecated 'resource' tag)
Eric Lambrecht