Is possible to overwrite the behaviour of the methods CreateLink and CreateLinkTo?
Asked
Active
Viewed 1,550 times
2
-
What behavior are you looking to override? – Mike Sickler Oct 28 '09 at 18:05
-
I want to add a prefix to the generated links. I can solve part of my problem writing a the method HTMLCodec which will return href's with the prefix. Unfortunately this is not the case for tags using the attribute SRC. As far as I've seen this SRC's are generated using CreateLink or CreateLinkTo. Is possible to have this behaviour? – Luixv Oct 28 '09 at 18:29
3 Answers
2
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
- 4,882
- 1
- 23
- 25
1
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
- 1,581
- 1
- 17
- 30
-
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 Oct 29 '09 at 06:05
-
Your should be able to do something like def createLink = {attrs, body -> def apptag = new ApplicationTagLib() out << prefix; out << apptag.createLink(attrs,body); } – Scott Warren Oct 29 '09 at 22:41
1
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
- 151
- 1
- 7
-
oh.. I was overriding the 'resource' tag, but this should work just as well for 'createLink' or 'createLinkTo' (the deprecated 'resource' tag) – Eric Lambrecht Mar 03 '10 at 22:03