Search in sources :

Example 1 with ClassPathTemplateLoader

use of com.github.jknack.handlebars.io.ClassPathTemplateLoader 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)

Example 2 with ClassPathTemplateLoader

use of com.github.jknack.handlebars.io.ClassPathTemplateLoader in project ddf by codice.

the class RegistryReportBuilder method setup.

public void setup() {
    templateLoader = new ClassPathTemplateLoader();
    templateLoader.setPrefix("/templates");
    templateLoader.setSuffix(".hbt");
    handlebars = new Handlebars(templateLoader);
    handlebars.registerHelpers(new HandlebarsHelper());
    handlebars.registerHelpers(StringHelpers.class);
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) HandlebarsHelper(org.codice.ddf.registry.rest.endpoint.HandlebarsHelper)

Example 3 with ClassPathTemplateLoader

use of com.github.jknack.handlebars.io.ClassPathTemplateLoader in project wcomponents by BorderTech.

the class HandlebarsRendererImpl method getHandlebarsEngine.

/**
 * @param options the engine options
 * @return the handlebars engine
 */
protected Handlebars getHandlebarsEngine(final Map<String, Object> options) {
    // Setup handlebars
    TemplateLoader loader = new ClassPathTemplateLoader();
    // Clear the suffix so the file name does not default the file type to ".hbs"
    loader.setSuffix("");
    Handlebars handlebars = new Handlebars(loader);
    // Pretty Print
    Object value = options.get(PRETTY_PRINT);
    if (value != null) {
        handlebars.setPrettyPrint("true".equalsIgnoreCase(value.toString()));
    }
    // Escaping Strategy
    value = options.get(ESCAPING_STRATEGY);
    if (value instanceof EscapingStrategy) {
        handlebars.with((EscapingStrategy) value);
    }
    value = options.get(THEME_I18N);
    if (value == null || "true".equalsIgnoreCase(value.toString())) {
        String resourceBundleBasename = ConfigurationProperties.getI18nThemeResourceBundleBaseName();
        // Theme i18n helper uses "t" not "i18n".
        handlebars.registerHelper("t", I18nHelper.i18n);
        I18nHelper.i18n.setDefaultLocale(I18nUtilities.getEffectiveLocale());
        I18nHelper.i18n.setDefaultBundle(resourceBundleBasename);
    }
    // Use markdown
    // Disabled temporarily for issues # 565
    /*value = options.get(MARKDOWN);
		if (value != null && "true".equalsIgnoreCase(value.toString())) {
			handlebars.registerHelper("md", new MarkdownHelper());
		}*/
    // Caching
    value = options.get(USE_CACHE);
    boolean cache = (isCaching() && value == null) || (value != null && "true".equalsIgnoreCase(value.toString()));
    if (cache) {
        handlebars.with(CACHE);
    }
    return handlebars;
}
Also used : EscapingStrategy(com.github.jknack.handlebars.EscapingStrategy) 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)

Aggregations

Handlebars (com.github.jknack.handlebars.Handlebars)3 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)3 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)2 Context (com.github.jknack.handlebars.Context)1 EscapingStrategy (com.github.jknack.handlebars.EscapingStrategy)1 Template (com.github.jknack.handlebars.Template)1 IOException (java.io.IOException)1 HandlebarsHelper (org.codice.ddf.registry.rest.endpoint.HandlebarsHelper)1