com.github.mustachejava.reflect.ReflectionObjectHandler Java Examples
The following examples show how to use
com.github.mustachejava.reflect.ReflectionObjectHandler.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TemplateUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
/** * 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(); }
Example #2
Source File: MustacheJavaViewResolverTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testRendering() { My.templatesPath("view-rendering"); MustacheJavaViewResolver viewResolver = Integrate.mustacheJavaViewResolver(); viewResolver.setCustomizer(compiler -> { compiler.setObjectHandler(new ReflectionObjectHandler() { @Override public String stringify(Object object) { return "[" + object + "]"; } }); return compiler; }); My.viewResolver(viewResolver); On.get("/").view("mtmpl").mvc((req, resp) -> { resp.model("y", "bar"); return U.map("x", "foo"); }); getReq("/"); }