Search in sources :

Example 1 with TemplateLoader

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

Example 2 with TemplateLoader

use of com.github.jknack.handlebars.io.TemplateLoader 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 3 with TemplateLoader

use of com.github.jknack.handlebars.io.TemplateLoader in project hippo by NHS-digital-website.

the class SchemaHelper method initialiseHandlebars.

private Handlebars initialiseHandlebars(final MarkdownHelper markdownHelper, final String templatesClasspath) throws URISyntaxException {
    final TemplateLoader templateLoader = templatesClasspath == null ? new ClassPathTemplateLoader(TEMPLATES_CLASSPATH, TEMPLATE_EXTENSION) : new FileTemplateLoader(new File(templatesClasspath), TEMPLATE_EXTENSION);
    final ContextModelsStack.Factory contextStackFactory = new ContextModelsStack.Factory(new UniqueModelStackExtractor());
    return new Handlebars(templateLoader).parentScopeResolution(false).infiniteLoops(true).prettyPrint(true).registerHelper(MarkdownHelper.NAME, markdownHelper).registerHelper(IndentationHelper.NAME, new IndentationHelper(contextStackFactory)).registerHelper(IfNotNullHelper.NAME, IfNotNullHelper.INSTANCE).registerHelper(IfRequiredHelper.NAME, new IfRequiredHelper(contextStackFactory)).registerHelper(ConditionalHelpers.eq.name(), ConditionalHelpers.eq).registerHelper(ConditionalHelpers.or.name(), ConditionalHelpers.or).registerHelper(EnumHelper.NAME, EnumHelper.INSTANCE).registerHelper(TypeAnyHelper.NAME, TypeAnyHelper.INSTANCE).registerHelper(JacksonPrettyJsonHelper.NAME, JacksonPrettyJsonHelper.INSTANCE).registerHelper(TypeAnySanitisingHelper.NAME, TypeAnySanitisingHelper.INSTANCE).registerHelper(AssignHelper.NAME, AssignHelper.INSTANCE).registerHelper(VariableValueHelper.NAME, VariableValueHelper.INSTANCE).registerHelper(BorderHelper.NAME, new BorderHelper(contextStackFactory)).registerHelper(UuidHelper.NAME, UuidHelper.INSTANCE);
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader) TemplateLoader(com.github.jknack.handlebars.io.TemplateLoader) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader) File(java.io.File)

Aggregations

Handlebars (com.github.jknack.handlebars.Handlebars)3 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)3 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)3 Context (com.github.jknack.handlebars.Context)1 EscapingStrategy (com.github.jknack.handlebars.EscapingStrategy)1 Template (com.github.jknack.handlebars.Template)1 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)1 File (java.io.File)1 IOException (java.io.IOException)1