Skip to content

Commit eddbc4f

Browse files
committed
Improve template method.
1 parent 621ac1c commit eddbc4f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public class $<T> {
3939
put("interpolate", "<%=([\\s\\S]+?)%>");
4040
put("escape", "<%-([\\s\\S]+?)%>");
4141
} };
42-
private static final int GROUP_INDEX_2 = 2;
43-
private static final int GROUP_INDEX_3 = 3;
4442
private static final int ARRAY_SIZE_2 = 2;
4543
private static final int MIN_PASSWORD_LENGTH_8 = 8;
4644
private static final long CAPACITY_SIZE_5 = 5L;
@@ -123,6 +121,10 @@ public String apply(Map<K, V> value) {
123121
"\\s*\\Q" + ((Map.Entry) element).getKey()
124122
+ "\\E\\s*")).matcher(result).replaceAll(escape(String.valueOf(((Map.Entry) element)
125123
.getValue())));
124+
result = java.util.regex.Pattern.compile(evaluate.replace(ALL_SYMBOLS,
125+
"\\s*\\Q" + ((Map.Entry) element).getKey()
126+
+ "\\E\\s*")).matcher(result).replaceAll(String.valueOf(((Map.Entry) element)
127+
.getValue()));
126128
}
127129
return result;
128130
}

src/test/java/com/github/underscore/UtilityTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ public void templateValue2() {
250250
put("name", "moe"); put("value", "<script>"); } }));
251251
}
252252

253+
@Test
254+
public void templateValue3() {
255+
Template<Map<String, Object>> template = $.template("hello: <% name %>, <b><%- value %></b>");
256+
assertEquals("hello: moe, <b>&lt;script&gt;</b>",
257+
template.apply(new LinkedHashMap<String, Object>() { {
258+
put("name", "moe"); put("value", "<script>"); } }));
259+
}
260+
253261
/*
254262
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
255263
_.result(object, 'cheese');

0 commit comments

Comments
 (0)