com.github.jknack.handlebars.Handlebars Java Examples
The following examples show how to use
com.github.jknack.handlebars.Handlebars.
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: HandlebarsScriptEngine.java From sling-samples with Apache License 2.0 | 6 votes |
@Override public Object eval(String script, ScriptContext context) throws ScriptException { final Resource resource = (Resource) context.getBindings(ScriptContext.ENGINE_SCOPE).get(SlingBindings.RESOURCE); final PrintWriter out = (PrintWriter) context.getBindings(ScriptContext.ENGINE_SCOPE).get(SlingBindings.OUT); try { final Handlebars handlebars = setupHandlebars(); final Template template = handlebars.compileInline(script); out.println(template.apply(getData(resource))); } catch(IOException ioe) { final ScriptException up = new ScriptException("IOException in eval"); up.initCause(ioe); throw up; } return null; }
Example #2
Source File: RSpecReportBuilder.java From bootstraped-multi-test-results-report with MIT License | 6 votes |
private void writeTestsPassedReport() throws IOException { Template template = new Helpers(new Handlebars()).registerHelpers().compile(TEST_OVERVIEW_REPORT); List<TestSuiteModel> onlyPassed = new ArrayList<>(processedTestSuites); for (Iterator<TestSuiteModel> it = onlyPassed.listIterator(); it.hasNext(); ) { TestSuiteModel f = it.next(); if (f.getOverallStatus().equalsIgnoreCase(Constants.FAILED)) { it.remove(); } } AllRSpecJUnitReports allTestSuites = new AllRSpecJUnitReports("Passed test suites report", onlyPassed); FileUtils.writeStringToFile(new File(TEST_OVERVIEW_PATH + "testsPassed.html"), template.apply(allTestSuites)); }
Example #3
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 #4
Source File: CodegenUtils.java From product-microgateway with Apache License 2.0 | 6 votes |
/** * Compile given template. * * @param defaultTemplateDir template directory * @param templateName template name * @return Generated template * @throws IOException if file read went wrong */ public static Template compileTemplate(String defaultTemplateDir, String templateName) throws IOException { String templatesDirPath = System.getProperty(GeneratorConstants.TEMPLATES_DIR_PATH_KEY, defaultTemplateDir); ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath)); FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath); cpTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX); fileTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX); Handlebars handlebars = new Handlebars().with(cpTemplateLoader, fileTemplateLoader); handlebars.setStringParams(true); handlebars.registerHelpers(StringHelpers.class); handlebars.registerHelper("equals", (object, options) -> { Object param0 = options.param(0, null); if (param0 == null) { throw new IllegalArgumentException("found 'null', expected 'string'"); } if (object != null && object.toString().equals(param0.toString())) { return options.fn(options.context); } return options.inverse(); }); return handlebars.compile(templateName); }
Example #5
Source File: SingularityExecutorModule.java From Singularity with Apache License 2.0 | 6 votes |
@Provides @Singleton public Handlebars providesHandlebars() { SingularityRunnerBaseLogging.quietEagerLogging(); // handlebars emits DEBUG logs before logger is properly configured final Handlebars handlebars = new Handlebars(); handlebars.registerHelper(BashEscapedHelper.NAME, new BashEscapedHelper()); handlebars.registerHelper(ShellQuoteHelper.NAME, new ShellQuoteHelper()); handlebars.registerHelper(IfPresentHelper.NAME, new IfPresentHelper()); handlebars.registerHelper( IfHasNewLinesOrBackticksHelper.NAME, new IfHasNewLinesOrBackticksHelper() ); handlebars.registerHelper( EscapeNewLinesAndQuotesHelper.NAME, new EscapeNewLinesAndQuotesHelper() ); return handlebars; }
Example #6
Source File: ReusingTemplatesUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void whenBlockIsDefined_ThenCanOverrideWithPartial() throws IOException { Handlebars handlebars = new Handlebars(templateLoader); Template template = handlebars.compile("simplemessage"); Person person = new Person(); person.setName("Baeldung"); String templateString = template.apply(person); assertThat(templateString).contains("<html>", "<body>", "This is the intro", "Hi there!", "</body>", "</html>"); }
Example #7
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
private String getBannersFooter(Handlebars handlebars, Map<String, Object> context) { try { Template template = handlebars.compile("templates/banner_footer"); context.put("bannerJSON", getActiveBannersJSON()); return template.apply(context); } catch (IOException e) { log.warn("IOException while getting banners footer", e); return ""; } }
Example #8
Source File: SingularityExecutorModule.java From Singularity with Apache License 2.0 | 5 votes |
@Provides @Singleton @Named(LOGROTATE_CRON_TEMPLATE) public Template providesLogrotateCronTemplate(Handlebars handlebars) throws IOException { return handlebars.compile(LOGROTATE_CRON_TEMPLATE); }
Example #9
Source File: SingularityExecutorModule.java From Singularity with Apache License 2.0 | 5 votes |
@Provides @Singleton @Named(LOGROTATE_HOURLY_TEMPLATE) public Template providesLogrotateHourlyTemplate(Handlebars handlebars) throws IOException { return handlebars.compile(LOGROTATE_HOURLY_TEMPLATE); }
Example #10
Source File: HandlebarsHelper.java From spring-cloud-release with Apache License 2.0 | 5 votes |
public static Template template(String templateName) { try { Handlebars handlebars = new Handlebars( new ClassPathTemplateLoader("/templates/spring-cloud/")); handlebars.registerHelper("replace", StringHelpers.replace); handlebars.registerHelper("capitalizeFirst", StringHelpers.capitalizeFirst); return handlebars.compile(templateName); } catch (IOException e) { throw new IllegalStateException(e); } }
Example #11
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
private String getTimezoneCheckFooter(Handlebars handlebars, Map<String, Object> context) { if (ServerConfigurationService.getBoolean("pasystem.timezone-check", true)) { try { Template template = handlebars.compile("templates/timezone_footer"); return template.apply(context); } catch (IOException e) { log.warn("Timezone footer failed", e); return ""; } } else { return ""; } }
Example #12
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
private String getPopupsFooter(Handlebars handlebars, Map<String, Object> context) { Session session = SessionManager.getCurrentSession(); User currentUser = UserDirectoryService.getCurrentUser(); if (currentUser == null || currentUser.getId() == null || "".equals(currentUser.getId())) { return ""; } try { if (session.getAttribute(POPUP_SCREEN_SHOWN) == null) { Popup popup = new PopupForUser(currentUser).getPopup(); if (popup.isActiveNow()) { context.put("popupTemplate", popup.getTemplate()); context.put("popupUuid", popup.getUuid()); context.put("popup", true); if (currentUser.getId() != null) { // Delivered! session.setAttribute(POPUP_SCREEN_SHOWN, "true"); } } } Template template = handlebars.compile("templates/popup_footer"); return template.apply(context); } catch (IOException | MissingUuidException e) { log.warn("IOException while getting popups footer", e); return ""; } }
Example #13
Source File: BasicUsageUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenThereIsNoTemplateFile_ThenCompilesInline() throws IOException { Handlebars handlebars = new Handlebars(); Template template = handlebars.compileInline("Hi {{this}}!"); String templateString = template.apply("Baeldung"); assertThat(templateString).isEqualTo("Hi Baeldung!"); }
Example #14
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
private Handlebars loadHandleBars(final I18n i18n) { Handlebars handlebars = new Handlebars() .with(cache); handlebars.registerHelper("t", new Helper<Object>() { @Override public CharSequence apply(final Object context, final Options options) { String key = options.param(0); return i18n.t(key); } }); return handlebars; }
Example #15
Source File: Xsd2JaxbGenerator.java From legstar-core2 with GNU Affero General Public License v3.0 | 5 votes |
public Xsd2JaxbGenerator() { Handlebars handlebars = new Handlebars(); handlebars.registerHelper("capFirst", StringHelpers.capitalize); handlebars.registerHelper("each", new com.legstar.jaxb.generator.EachHelper()); hbtJaxbWrapperFactoryClass = loadTemplate(handlebars, JAXB_WRAPPER_FACTORY_CLASS_TEMPLATE_NAME); hbtJaxbConverterClass = loadTemplate(handlebars, JAXB_CONVERTER_CLASS_TEMPLATE_NAME); modelBuilder = new Xsd2CobolTypesModelBuilder(); }
Example #16
Source File: Xsd2JaxbGenerator.java From legstar-core2 with GNU Affero General Public License v3.0 | 5 votes |
private Template loadTemplate(Handlebars handlebars, String resourceName) { try { String text = IOUtils.toString(getClass().getResourceAsStream( resourceName)); return handlebars.compileInline(text); } catch (IOException e) { throw new Xsd2ConverterException(e); } }
Example #17
Source File: Xsd2CobolTypesGenerator.java From legstar-core2 with GNU Affero General Public License v3.0 | 5 votes |
public Xsd2CobolTypesGenerator() { try { String text = IOUtils.toString(getClass().getResourceAsStream( JAVA_CLASS_TEMPLATE_NAME)); Handlebars handlebars = new Handlebars(); hbtJavaClass = handlebars.compileInline(text); modelBuilder = new Xsd2CobolTypesModelBuilder(); } catch (IOException e) { throw new Xsd2ConverterException(e); } }
Example #18
Source File: HandlebarsI18nHelperTest.java From commercetools-sunrise-java with Apache License 2.0 | 5 votes |
private static void testTemplate(final String templateName, final Locale locale, final Map<String, String> i18nMap, final Consumer<String> test) { final Handlebars handlebars = HandlebarsFactory.create(singletonList(DEFAULT_LOADER), i18nResolver(i18nMap), new I18nIdentifierFactory()); final TemplateEngine templateEngine = HandlebarsTemplateEngine.of(handlebars, new HandlebarsContextFactory(new PlayJavaFormResolver(msg -> msg))); final String html = renderTemplate(templateName, templateEngine, locale); test.accept(html); }
Example #19
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
@Override public String getFooter() { StringBuilder result = new StringBuilder(); I18n i18n = getI18n(this.getClass().getClassLoader(), "org.sakaiproject.pasystem.impl.i18n.pasystem"); Handlebars handlebars = loadHandleBars(i18n); Session session = SessionManager.getCurrentSession(); Map<String, Object> context = new HashMap<String, Object>(); try { Template template = handlebars.compile("templates/shared_footer"); context.put("portalCDNQuery", PortalUtils.getCDNQuery()); context.put("sakai_csrf_token", session.getAttribute("sakai.csrf.token")); result.append(template.apply(context)); } catch (IOException e) { log.warn("IOException while getting footer", e); return ""; } result.append(getBannersFooter(handlebars, context)); result.append(getPopupsFooter(handlebars, context)); result.append(getTimezoneCheckFooter(handlebars, context)); return result.toString(); }
Example #20
Source File: RSpecReportBuilder.java From bootstraped-multi-test-results-report with MIT License | 5 votes |
private void writeTestCaseSummaryReport() throws IOException { Template template = new Helpers(new Handlebars()).registerHelpers().compile(TEST_SUMMARY_REPORT); for (TestSuiteModel ts : processedTestSuites) { String content = template.apply(ts); FileUtils.writeStringToFile(new File(TEST_SUMMARY_PATH + ts.getUniqueID() + ".html"), content); } }
Example #21
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 #22
Source File: BuiltinHelperUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenUsedWith_ThenContextChanges() throws IOException { Handlebars handlebars = new Handlebars(templateLoader); Template template = handlebars.compile("with"); Person person = getPerson("Baeldung"); person.getAddress().setStreet("World"); String templateString = template.apply(person); assertThat(templateString).contains("<h4>I live in World</h4>"); }
Example #23
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
private String getPopupsFooter(Handlebars handlebars, Map<String, Object> context) { Session session = SessionManager.getCurrentSession(); User currentUser = UserDirectoryService.getCurrentUser(); if (currentUser == null || currentUser.getId() == null || "".equals(currentUser.getId())) { return ""; } try { if (session.getAttribute(POPUP_SCREEN_SHOWN) == null) { Popup popup = new PopupForUser(currentUser).getPopup(); if (popup.isActiveNow()) { context.put("popupTemplate", popup.getTemplate()); context.put("popupUuid", popup.getUuid()); context.put("popup", true); if (currentUser.getId() != null) { // Delivered! session.setAttribute(POPUP_SCREEN_SHOWN, "true"); } } } Template template = handlebars.compile("templates/popup_footer"); return template.apply(context); } catch (IOException | MissingUuidException e) { log.warn("IOException while getting popups footer", e); return ""; } }
Example #24
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
private String getBannersFooter(Handlebars handlebars, Map<String, Object> context) { try { Template template = handlebars.compile("templates/banner_footer"); context.put("bannerJSON", getActiveBannersJSON()); return template.apply(context); } catch (IOException e) { log.warn("IOException while getting banners footer", e); return ""; } }
Example #25
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
private Handlebars loadHandleBars(final I18n i18n) { Handlebars handlebars = new Handlebars() .with(cache); handlebars.registerHelper("t", new Helper<Object>() { @Override public CharSequence apply(final Object context, final Options options) { String key = options.param(0); return i18n.t(key); } }); return handlebars; }
Example #26
Source File: PASystemImpl.java From sakai with Educational Community License v2.0 | 5 votes |
@Override public String getFooter() { StringBuilder result = new StringBuilder(); I18n i18n = getI18n(this.getClass().getClassLoader(), "org.sakaiproject.pasystem.impl.i18n.pasystem"); Handlebars handlebars = loadHandleBars(i18n); Session session = SessionManager.getCurrentSession(); Map<String, Object> context = new HashMap<String, Object>(); try { Template template = handlebars.compile("templates/shared_footer"); context.put("portalCDNQuery", PortalUtils.getCDNQuery()); context.put("sakai_csrf_token", session.getAttribute("sakai.csrf.token")); result.append(template.apply(context)); } catch (IOException e) { log.warn("IOException while getting footer", e); return ""; } result.append(getBannersFooter(handlebars, context)); result.append(getPopupsFooter(handlebars, context)); result.append(getTimezoneCheckFooter(handlebars, context)); return result.toString(); }
Example #27
Source File: VaadinConnectTsGenerator.java From flow with Apache License 2.0 | 5 votes |
@Override public void addHandlebarHelpers(Handlebars handlebars) { super.addHandlebarHelpers(handlebars); handlebars.registerHelper("multiplelines", getMultipleLinesHelper()); handlebars.registerHelper("getClassNameFromImports", getClassNameFromImportsHelper()); handlebars.registerHelper("getModelType", getModelTypeHelper()); handlebars.registerHelper("getModelConstructor", getModelConstructorHelper()); }
Example #28
Source File: TemplateEngine.java From bonita-ui-designer with GNU General Public License v2.0 | 5 votes |
public TemplateEngine(String template) { SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider(); simpleFilterProvider.setFailOnUnknownId(false); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setFilters(simpleFilterProvider); handlebars = new Handlebars(new ClassPathTemplateLoader("/", "")); handlebars.registerHelper("json", new Jackson2Helper(objectMapper)); handlebars.registerHelper("join", StringHelpers.join); handlebars.registerHelper("ifequal", IfEqualHelper.INSTANCE); handlebars.prettyPrint(true); location = "templates/" + template; }
Example #29
Source File: BuiltinHelperUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenUsedIf_ThenPutsCondition() throws IOException { Handlebars handlebars = new Handlebars(templateLoader); Template template = handlebars.compile("if"); Person person = getPerson("Baeldung"); person.setBusy(true); String templateString = template.apply(person); assertThat(templateString).contains("<h4>Baeldung is busy.</h4>"); }
Example #30
Source File: TestMessages.java From arcusplatform with Apache License 2.0 | 5 votes |
public Templater() { templates.put(MSG_KEY_THING + "-apns", TEMPLATE_THING); templates.put(MSG_KEY_ENUM + "-apns", TEMPLATE_ENUM); handlebars = new Handlebars() .registerHelper(EnumHelper.NAME, EnumHelper.INSTANCE) .registerHelpers(HandlebarsHelpersSource.class) .registerHelpers(StringHelpers.class); }