Permalink
Browse files

fix for GRAILS-4786

  • Loading branch information...
1 parent 10f6819 commit ddeebbcb0870accc33402ed98ad82ac7769817fb @graemerocher graemerocher committed Jul 21, 2009
@@ -17,6 +17,8 @@
import groovy.lang.Binding;
import groovy.lang.Closure;
import groovy.lang.GString;
+import groovy.lang.GroovyObject;
+import org.codehaus.groovy.grails.commons.TagLibArtefactHandler;
import org.codehaus.groovy.grails.web.pages.GroovyPage;
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
@@ -56,6 +58,9 @@ public GroovyPageTagBody(Object owner, GrailsWebRequest webRequest,boolean write
binding = null;
if(owner instanceof GroovyPage)
binding = ((GroovyPage) owner).getBinding();
+ else if(owner != null && owner.getClass().getName().endsWith(TagLibArtefactHandler.TYPE)) {
+ binding = (Binding) ((GroovyObject)owner).getProperty(GroovyPage.PAGE_SCOPE);
+ }
this.writeStringResult=writeStringResult;
}
@@ -88,20 +93,27 @@ private Object captureClosureOutput(Object args) {
// GRAILS-2675: Copy the current binding so that we can restore
// it to its original state.
- Map currentBinding = binding.getVariables();
- Map originalBinding = new HashMap(currentBinding);
+ Map currentBinding = null;
+ Map originalBinding = null;
+
+ if(binding!=null) {
+ currentBinding = binding.getVariables();
+ originalBinding = new HashMap(currentBinding);
+ // Add the extra variables passed into the body to the
+ // current binding.
+ currentBinding.putAll((Map) args);
+ }
- // Add the extra variables passed into the body to the
- // current binding.
- currentBinding.putAll((Map) args);
try {
bodyResult = bodyClosure.call(args);
}
finally {
- // GRAILS-2675: Restore the original binding.
- currentBinding.clear();
- currentBinding.putAll(originalBinding);
+ if(binding!=null) {
+ // GRAILS-2675: Restore the original binding.
+ currentBinding.clear();
+ currentBinding.putAll(originalBinding);
+ }
}
}
else {
@@ -11,6 +11,17 @@ public class InvokeTagLibWithBodyAsMethodTests extends AbstractGrailsTagTests {
public void onSetUp() {
gcl.parseClass('''
class TestTagLib {
+ def testInvokeWithBodyClosure = { attrs, body ->
+ out << eachItem(items:[1,2,3]) { bodyAttrs ->
+ out << "body=${bodyAttrs.var}"
+ }
+ }
+ def eachItem = { attrs, body ->
+ def items = attrs.items
+ items.each { i ->
+ out << body(var:i)
+ }
+ }
def testWithClosureAndGStringReturn = { attrs, body ->
def foo = "bar"
out << "one" << test(foo:"bar") { "$foo" } << "four"
@@ -44,6 +55,13 @@ class TestTagLib {
''')
}
+ void testInvokeWithBodyClosure() {
+ def template = '<g:testInvokeWithBodyClosure />'
+
+
+ assertOutputEquals 'body=1body=2body=3', template
+
+ }
void testWithClosureAndGStringReturn() {
def template = '<g:testWithClosureAndGStringReturn />'

0 comments on commit ddeebbc

Please sign in to comment.