use of com.github.jknack.handlebars.Context in project knotx by Cognifide.
the class JsonObjectValueResolverTest method JsonObjectResolver_whenApplyingProgrammaticallyCreatedObject_expectVariablesResolved.
@Test
public void JsonObjectResolver_whenApplyingProgrammaticallyCreatedObject_expectVariablesResolved() throws Exception {
Context context = Context.newBuilder(programmaticModel()).push(JsonObjectValueResolver.INSTANCE).build();
String compiled = template.apply(context).trim();
assertThat(compiled, equalTo(expected));
}
use of com.github.jknack.handlebars.Context in project ddf by codice.
the class LandingPage method compileTemplateWithProperties.
// package-private for unit testing
String compileTemplateWithProperties() {
title = getTitle();
version = branding.map(registry -> registry.getAttributeFromBranding(BrandingPlugin::getProductName)).orElse("");
logoToUse = StringUtils.isNotEmpty(logo) ? logo : getProductImage();
// FieldValueResolver so this class' fields can be accessed in the template.
// MapValueResolver so we can access {{@index}} in the #each helper in the template.
final Context context = Context.newBuilder(this).resolver(FieldValueResolver.INSTANCE, MapValueResolver.INSTANCE).build();
// The template is "index.handlebars".
final TemplateLoader templateLoader = new ClassPathTemplateLoader("/", ".handlebars");
final Handlebars handlebars = new Handlebars(templateLoader);
// extractDate(), extractAnnouncement(), expanded(), and in() are helper functions used in the
// template.
handlebars.registerHelpers(this);
String landingPageHtml;
try {
final Template template = handlebars.compile(LANDING_PAGE_FILE);
landingPageHtml = template.apply(context);
} catch (IOException e) {
LOGGER.info("Unable to compile Landing Page template.", e);
landingPageHtml = "<p>We are experiencing some issues. Please contact an administrator.</p>";
}
return landingPageHtml;
}
Aggregations