neuland/jade4j

Compile in line jade templates

Closed this issue · 1 comments

I'm searching a way to compile directly String containing Jade code inline, without a file template.

For example with Handlebars template :

Map<String, Object> context; // Given context
String templateString = "Hello {{this}}!";
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline(templateString);
String html = template.apply(context);
System.out.println(html);

Or with Freemarker :

Map<String, Object> context; // Given context
String templateString = "Hello ${this}!";
Template template = new Template(name, new StringReader(templateString), freemarkerConfiguration);
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
System.out.println(html);

Is there an equivalent with Jade4J template engine ?

Thanks.

You could use a ReaderTemplateLoader like this:

Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", "world");
JadeConfiguration config = new JadeConfiguration();
Reader reader = new StringReader("Hello #{foo}!");
TemplateLoader templateLoader = new ReaderTemplateLoader(reader, "template");
config.setTemplateLoader(templateLoader);
JadeTemplate template = config.getTemplate("template");
String html = config.renderTemplate(template, model);
System.out.println(html);

Note that #150 needs to be resolved in order for the above sample to work.