Search in sources :

Example 11 with Context

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));
}
Also used : Context(com.github.jknack.handlebars.Context) Test(org.junit.Test)

Example 12 with Context

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;
}
Also used : Context(com.github.jknack.handlebars.Context) Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) TemplateLoader(com.github.jknack.handlebars.io.TemplateLoader) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template)

Aggregations

Context (com.github.jknack.handlebars.Context)12 Template (com.github.jknack.handlebars.Template)10 IOException (java.io.IOException)8 Handlebars (com.github.jknack.handlebars.Handlebars)6 PrintWriter (java.io.PrintWriter)3 UIContext (com.github.bordertech.wcomponents.UIContext)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)2 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)2 FileNotFoundException (java.io.FileNotFoundException)2 Test (org.junit.Test)2 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Type (java.lang.reflect.Type)1