Skip to content

Commit 88301de

Browse files
committed
Implemented GRAILS-8518 (Map iteration with g:each tag as it was in Grails 1.x.x)
1 parent 19446c3 commit 88301de

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/pages/GroovyPageRenderingTests.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class GroovyPageRenderingTests extends AbstractGrailsTagTests {
4343
def result = applyTemplate(template, [toplist: [[sublist:['a','b']],[sublist:['c','d']]]])
4444
assertEquals 'abcd', result
4545
}
46+
47+
void testForeachIteratingMap() {
48+
def template='<g:each var="k,v" in="[a:1,b:2,c:3]">${k}=${v},</g:each>'
49+
def result = applyTemplate(template, [:])
50+
assertEquals 'a=1,b=2,c=3,', result
51+
}
4652

4753
void testForeachRenaming() {
4854
def template='<g:each in="${list}"><g:each in="${list}">.</g:each></g:each>'

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/taglib/GroovySyntaxTag.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ protected void doEachMethod(String in) {
139139
var = "_it"+ Math.abs(System.identityHashCode(this));
140140
foreachRenamedIt = var;
141141
}
142-
142+
143+
String[] entryVars=null;
144+
if(hasVar && var.indexOf(',') > -1) {
145+
entryVars = var.split("\\s*,\\s*");
146+
var = "_entry" + Math.abs(System.identityHashCode(this));
147+
}
148+
143149
out.print("for( " + var);
144150
out.print(" in "); // dot de-reference
145151
out.print(parser != null ? parser.getExpressionText(in, false) : extractAttributeValue(in)); // object
@@ -148,6 +154,9 @@ protected void doEachMethod(String in) {
148154
out.println();
149155
if (!hasVar) {
150156
out.println("changeItVariable(" + foreachRenamedIt +")" );
157+
} else if (entryVars != null) {
158+
out.println("def " + entryVars[0].trim() + "=" + var + ".getKey()");
159+
out.println("def " + entryVars[1].trim() + "=" + var + ".getValue()");
151160
}
152161
}
153162

0 commit comments

Comments
 (0)