com.github.jknack.handlebars.io.TemplateLoader Java Examples
The following examples show how to use
com.github.jknack.handlebars.io.TemplateLoader.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HandlebarsFactory.java From commercetools-sunrise-java with Apache License 2.0 | 6 votes |
static Handlebars create(final List<TemplateLoader> templateLoaders, final I18nResolver i18NResolver, final I18nIdentifierFactory i18nIdentifierFactory) { if (templateLoaders.isEmpty()) { throw new SunriseConfigurationException("Could not initialize Handlebars due to missing template loaders configuration", CONFIG_TEMPLATE_LOADERS); } logger.info("Provide Handlebars: template loaders [{}]]", templateLoaders.stream().map(TemplateLoader::getPrefix).collect(joining(", "))); final TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[templateLoaders.size()]); final Handlebars handlebars = new Handlebars() .with(loaders) .with(new HighConcurrencyTemplateCache()) .infiniteLoops(true); handlebars.registerHelper("i18n", new HandlebarsI18nHelper(i18NResolver, i18nIdentifierFactory)); handlebars.registerHelper("cms", new HandlebarsCmsHelper()); handlebars.registerHelper("json", new HandlebarsJsonHelper<>()); return handlebars; }
Example #2
Source File: TestTemplatesCompile.java From arcusplatform with Apache License 2.0 | 5 votes |
@Test public void testTemplatesCompile() throws Exception { TemplateLoader loader = new IrisResourceLoader("templates", 0); Handlebars handlebars = new Handlebars(loader) .registerHelper(EnumHelper.NAME, EnumHelper.INSTANCE) .registerHelpers(HandlebarsHelpersSource.class) .registerHelper(IfEqualHelper.NAME, IfEqualHelper.INSTANCE) .registerHelpers(StringHelpers.class); Validator v = new Validator(); String templateDir = Resources .getResource("templates") .getUri() .toURL() .getPath(); for(File template: new File(templateDir).listFiles((file) -> file != null && file.getName() != null && file.getName().endsWith(".hbs"))) { try { String name = template.getName(); name = name.substring(0, name.length() - 4); Template tpl = handlebars.compile(name); if(name.endsWith("-email")) { // TODO validate email xml continue; } String body = tpl.apply(Context.newContext(Collections.emptyMap())); JSON.fromJson(body, Map.class); } catch(Exception e) { e.printStackTrace(); v.error("Failed to compile: " + template.getName() + " -- " + e.getMessage()); } } v.throwIfErrors(); }
Example #3
Source File: HandlebarsTemplateService.java From arcusplatform with Apache License 2.0 | 5 votes |
private void init(String prefix, int cacheSize){ LOGGER.info("Handlebars Template Service Initialization with a location of {} and cache size of {}",prefix,cacheSize); templateCache = new ConcurrentMapTemplateCache(); TemplateLoader loader=new IrisResourceLoader(prefix, cacheSize); handlebars=new Handlebars(loader) .with(templateCache) .registerHelper(EnumHelper.NAME, EnumHelper.INSTANCE) .registerHelper(IfEqualHelper.NAME, IfEqualHelper.INSTANCE) .registerHelpers(HandlebarsHelpersSource.class) .registerHelpers(StringHelpers.class) .registerHelper(PluralizeHelper.NAME, PluralizeHelper.INSTANCE); }
Example #4
Source File: HandlebarsEngineAdapter.java From openapi-generator with Apache License 2.0 | 5 votes |
public String compileTemplate(TemplatingExecutor executor, Map<String, Object> bundle, String templateFile) throws IOException { TemplateLoader loader = new AbstractTemplateLoader() { @Override public TemplateSource sourceAt(String location) { return findTemplate(executor, location); } }; Context context = Context .newBuilder(bundle) .resolver( MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE) .build(); Handlebars handlebars = new Handlebars(loader); handlebars.registerHelperMissing((obj, options) -> { LOGGER.warn(String.format(Locale.ROOT, "Unregistered helper name '%s', processing template:\n%s", options.helperName, options.fn.text())); return ""; }); handlebars.registerHelper("json", Jackson2Helper.INSTANCE); StringHelpers.register(handlebars); handlebars.registerHelpers(ConditionalHelpers.class); handlebars.registerHelpers(org.openapitools.codegen.templating.handlebars.StringHelpers.class); Template tmpl = handlebars.compile(templateFile); return tmpl.apply(context); }
Example #5
Source File: HandlebarsTemplateEngine.java From spark-template-engines with Apache License 2.0 | 5 votes |
/** * Constructs a handlebars template engine * * @param resourceRoot the resource root */ public HandlebarsTemplateEngine(String resourceRoot) { TemplateLoader templateLoader = new ClassPathTemplateLoader(); templateLoader.setPrefix(resourceRoot); templateLoader.setSuffix(null); handlebars = new Handlebars(templateLoader); // Set Guava cache. Cache<TemplateSource, Template> cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(1000).build(); handlebars = handlebars.with(new GuavaTemplateCache(cache)); }
Example #6
Source File: HandlebarsFactory.java From commercetools-sunrise-java with Apache License 2.0 | 5 votes |
protected static TemplateLoader initializeTemplateLoader(final Configuration loaderConfig) { final String type = loaderConfig.getString(TYPE_ATTR); final String path = loaderConfig.getString(PATH_ATTR); switch (type) { case CLASSPATH_TYPE: return new ClassPathTemplateLoader(path); case FILE_TYPE: return new FileTemplateLoader(path); default: throw new SunriseConfigurationException("Could not initialize Handlebars due to unrecognized template loader \"" + type + "\"", CONFIG_TEMPLATE_LOADERS); } }
Example #7
Source File: BasicUsageUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenClasspathTemplateLoaderIsGiven_ThenSearchesClasspathWithPrefixSuffix() throws IOException { TemplateLoader loader = new ClassPathTemplateLoader("/handlebars", ".html"); Handlebars handlebars = new Handlebars(loader); Template template = handlebars.compile("greeting"); Person person = getPerson("Baeldung"); String templateString = template.apply(person); assertThat(templateString).isEqualTo("Hi Baeldung!"); }
Example #8
Source File: BasicUsageUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenMultipleLoadersAreGiven_ThenSearchesSequentially() throws IOException { TemplateLoader firstLoader = new ClassPathTemplateLoader("/handlebars", ".html"); TemplateLoader secondLoader = new ClassPathTemplateLoader("/templates", ".html"); Handlebars handlebars = new Handlebars().with(firstLoader, secondLoader); Template template = handlebars.compile("greeting"); Person person = getPerson("Baeldung"); String templateString = template.apply(person); assertThat(templateString).isEqualTo("Hi Baeldung!"); }
Example #9
Source File: Templating.java From StubbornJava with MIT License | 4 votes |
public Builder withResourceLoaders() { log.debug("using resource loaders"); loaders.add(new ClassPathTemplateLoader()); loaders.add(new ClassPathTemplateLoader(TemplateLoader.DEFAULT_PREFIX, ".sql")); return this; }
Example #10
Source File: Templating.java From StubbornJava with MIT License | 4 votes |
public Templating build() { handlebars.with(loaders.toArray(new TemplateLoader[0])); return new Templating(handlebars); }
Example #11
Source File: Templating.java From StubbornJava with MIT License | 4 votes |
public Builder withResourceLoaders() { log.debug("using resource loaders"); loaders.add(new ClassPathTemplateLoader()); loaders.add(new ClassPathTemplateLoader(TemplateLoader.DEFAULT_PREFIX, ".sql")); return this; }
Example #12
Source File: Templating.java From StubbornJava with MIT License | 4 votes |
public Templating build() { handlebars.with(loaders.toArray(new TemplateLoader[0])); return new Templating(handlebars); }
Example #13
Source File: HandlebarsFactory.java From commercetools-sunrise-java with Apache License 2.0 | 4 votes |
public Handlebars create(final Configuration configuration) { final List<TemplateLoader> templateLoaders = initializeTemplateLoaders(configuration, CONFIG_TEMPLATE_LOADERS); return create(templateLoaders, i18NResolver, i18nIdentifierFactory); }
Example #14
Source File: HandlebarsFactory.java From commercetools-sunrise-java with Apache License 2.0 | 4 votes |
protected static List<TemplateLoader> initializeTemplateLoaders(final Configuration configuration, final String configKey) { return configuration.getConfigList(configKey, emptyList()) .stream() .map(HandlebarsFactory::initializeTemplateLoader) .collect(toList()); }
Example #15
Source File: HandlebarsCmsHelperTest.java From commercetools-sunrise-java with Apache License 2.0 | 4 votes |
private static TemplateEngine handlebarsTemplateEngine() { final TestableI18nResolver i18nResolver = new TestableI18nResolver(emptyMap()); final List<TemplateLoader> templateLoaders = singletonList(new ClassPathTemplateLoader("/templates/cmsHelper")); final Handlebars handlebars = HandlebarsFactory.create(templateLoaders, i18nResolver, new I18nIdentifierFactory()); return HandlebarsTemplateEngine.of(handlebars, new HandlebarsContextFactory(new PlayJavaFormResolver(msg -> msg))); }