HTMLTemplate extends ContainerFragment { public String process(TemplateContext context) { if (context.root == null) throw new Error("root must not be null"); if (!modelClass.isInstance(context.root)) throw new Error(format("model class does not match, expectedClass={}, actualClass={}", modelClass.getCanonicalName(), context.root.getClass().getCanonicalName())); StringBuilder builder = new StringBuilder(2048); process(builder, context); return builder.toString(); } HTMLTemplate(Class<?> modelClass); String process(TemplateContext context); @Override void process(StringBuilder builder, TemplateContext context);  }
@Test void process() { HTMLTemplateBuilder builder = new HTMLTemplateBuilder(new ClasspathTemplateSource("template-test/template.html"), TestModel.class); builder.message = key -> Optional.of(key + "_value"); HTMLTemplate template = builder.build(); TestModel model = new TestModel(); model.stringField = "string<"; model.numberField = 100; model.items.addAll(List.of("a", "b", "c")); model.children.add(child("child1", 1.0, true)); model.children.add(child("child2", 2.0, false)); model.htmlField = "<pre>html</pre>"; String result = template.process(new TemplateContext(model, new CDNManager())); assertEquals(ClasspathResources.text("template-test/template-result.html").replaceAll("\r\n", "\n"), result); }