use of com.github.mustachejava.Binding in project dcos-commons by mesosphere.
the class TemplateUtils method renderMustache.
/**
* Renders a given Mustache template using the provided value map, returning any template parameters which weren't
* present in the map.
*
* @param templateName descriptive name of template to show in logs
* @param templateContent String representation of template
* @param values Map of values to be inserted into the template
* @param missingValues List where missing value entries will be added for any template params in
* {@code templateContent} which are not found in {@code values}
* @return Rendered Mustache template String
*/
public static String renderMustache(String templateName, String templateContent, Map<String, String> values, final List<MissingValue> missingValues) {
StringWriter writer = new StringWriter();
DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory();
mustacheFactory.setObjectHandler(new ReflectionObjectHandler() {
@Override
public Binding createBinding(String name, final TemplateContext tc, Code code) {
return new MissingValueBinding(this, name, tc, code, missingValues);
}
});
Map<String, Object> objEnv = new HashMap<>();
for (Map.Entry<String, String> entry : values.entrySet()) {
if (StringUtils.equalsIgnoreCase(entry.getValue(), "false") || StringUtils.equalsIgnoreCase(entry.getValue(), "true")) {
objEnv.put(entry.getKey(), Boolean.valueOf(entry.getValue()));
} else {
objEnv.put(entry.getKey(), entry.getValue());
}
}
mustacheFactory.compile(new StringReader(templateContent), templateName).execute(writer, objEnv);
return writer.toString();
}
Aggregations