Skip to content

Commit 30bfa45

Browse files
spsspullara
authored andcommitted
add support for {{.}}
this is useful for data structures that are not associative, like an array of primitives. for example: { list:[1,2,3] } {{#list}} {{.}} {{/list}}
1 parent 340ed40 commit 30bfa45

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/main/java/com/sampullara/mustache/Mustache.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@
3737
* Time: 10:12:47 AM
3838
*/
3939
public abstract class Mustache {
40+
4041
protected static Logger logger = Logger.getLogger(Mustache.class.getName());
4142
private static final boolean debug = Boolean.getBoolean("mustache.debug");
4243
public static final boolean trace = Boolean.getBoolean("mustache.trace");
44+
45+
private static final String IMPLICIT_CURRENT_ELEMENT_TOKEN = ".";
46+
4347
private File root;
4448
private String path;
4549

@@ -485,7 +489,13 @@ protected Iterable<Scope> inverted(final Scope s, final String name) {
485489

486490
protected Object getValue(Scope s, String name) {
487491
try {
492+
488493
Object o = s.get(name);
494+
495+
if (o == null && IMPLICIT_CURRENT_ELEMENT_TOKEN.equals(name)) {
496+
o = s.values().iterator().next();
497+
}
498+
489499
if (o == null && debug) {
490500
StringBuilder sb = new StringBuilder();
491501
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {

src/test/java/com/sampullara/mustache/CompilerTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,36 @@ public void testJson() throws IOException, MustacheException {
306306

307307
}
308308

309+
@SuppressWarnings("serial")
310+
public void testCurrentElementInArray() throws IOException, MustacheException {
311+
312+
MustacheCompiler c = init();
313+
Mustache m = c.parseFile("simple_array.html");
314+
StringWriter sw = new StringWriter();
315+
FutureWriter writer = new FutureWriter(sw);
316+
m.execute(writer, new Scope(new HashMap<String, Object>() {
317+
{
318+
put("list", Arrays.asList(1,2,3));
319+
}
320+
}));
321+
writer.flush();
322+
assertEquals(getContents(root, "simple_array.txt"), sw.toString());
323+
324+
/*
325+
* verify null elements in a list are properly handled when using {{.}}
326+
*/
327+
sw = new StringWriter();
328+
writer = new FutureWriter(sw);
329+
m.execute(writer, new Scope(new HashMap<String, Object>() {
330+
{
331+
put("list", Arrays.asList(null,null));
332+
}
333+
}));
334+
writer.flush();
335+
assertEquals("\n\n", sw.toString());
336+
337+
}
338+
309339
public void testReadme() throws MustacheException, IOException {
310340
MustacheCompiler c = init();
311341
Mustache m = c.parseFile("items.html");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{#list}}
2+
{{.}}
3+
{{/list}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
2
3+
3

0 commit comments

Comments
 (0)