Root level objects not nulling out in partial context switch
There appears to be a bug in the java version of handlebars proved by correct output in handlebars.js:
import com.github.jknack.handlebars.EscapingStrategy;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.io.FileTemplateLoader;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.Map;
public class HandleBarsJava {
public static void main(final String[] args) throws IOException {
FileTemplateLoader loader = new FileTemplateLoader(".", ".mustache");
Handlebars handleBars = new Handlebars(loader).with(EscapingStrategy.XML);
Template template = handleBars.compile("test");
Map<String, Object> data = ImmutableMap.<String, Object>builder()
.put("a", "aData")
.put("map", ImmutableMap.<String, String>builder()
.put("b", "bData")
.put("c", "cData")
.build())
.build();
System.out.println(template.apply(data));
}
}
test.mustache:
{{> partial null b=map.b}}
{{> partial null c=map.c}}
partial.mustache:
{{#with a}}
a: {{.}}
{{/with}}
{{#with b}}
b: {{.}}
{{/with}}
{{#with c}}
c: {{.}}
{{/with}}
Renders the following out put:
a: aData
b: bData
a: aData
c: cData
Because there are nulled contexts and no references to a, I would expect it to not show up in the output as seen by handlebars.js:
https://handlebarsjs.com/playground.html#format=1¤tExample=%7B%22template%22%3A%22%7B%7B%3E%20partial%20null%20b%3Dmap.b%7D%7D%5Cn%7B%7B%3E%20partial%20null%20c%3Dmap.c%7D%7D%22%2C%22partials%22%3A%5B%7B%22name%22%3A%22partial%22%2C%22content%22%3A%22%7B%7B%23with%20a%7D%7D%5Cn%20%20a%3A%20%7B%7B.%7D%7D%5Cn%7B%7B%2Fwith%7D%7D%5Cn%5Cn%7B%7B%23with%20b%7D%7D%5Cn%20%20b%3A%20%7B%7B.%7D%7D%5Cn%7B%7B%2Fwith%7D%7D%5Cn%5Cn%7B%7B%23with%20c%7D%7D%5Cn%20%20c%3A%20%7B%7B.%7D%7D%5Cn%7B%7B%2Fwith%7D%7D%22%7D%5D%2C%22input%22%3A%22%7B%5Cn%20%20map%3A%20%7B%5Cn%20%20%20%20'b'%3A%20%5C%22bData%5C%22%2C%5Cn%20%20%20%20'c'%3A%20%5C%22cData%5C%22%5Cn%20%20%7D%2C%5Cn%20%20'a'%3A%20%5C%22aData%5C%22%2C%5Cn%7D%5Cn%22%2C%22output%22%3A%22%5Cn%20%20b%3A%20bData%5Cn%5Cn%5Cn%5Cn%20%20c%3A%20cData%5Cn%22%2C%22preparationScript%22%3A%22%2F%2F%20Handlebars.registerHelper('loud'%2C%20function(string)%20%7B%5Cn%2F%2F%20%20%20%20return%20string.toUpperCase()%5Cn%2F%2F%20%7D)%3B%5Cn%22%2C%22handlebarsVersion%22%3A%224.7.6%22%7D
Which has the expected output:
b: bData
c: cData
I am also running into this issue. It seems like the java version of handlebars does not support null context? Are there any workarounds for this?
I just want to be able to include a partial and null the context so that variables I don't want don't leak into the partial accidentally. This works fine in the JavaScript version. Am I missing anything?