Java Code Examples for com.github.jknack.handlebars.Handlebars#registerHelpers()
The following examples show how to use
com.github.jknack.handlebars.Handlebars#registerHelpers() .
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: 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 2
Source File: BaragonAgentServiceModule.java From Baragon with Apache License 2.0 | 6 votes |
@Provides @Singleton public Handlebars providesHandlebars(BaragonAgentConfiguration config, BaragonAgentMetadata agentMetadata, UpstreamResolver resolver) { final Handlebars handlebars = new Handlebars(); handlebars.registerHelper(FormatTimestampHelper.NAME, new FormatTimestampHelper(config.getDefaultDateFormat())); handlebars.registerHelper(FirstOfHelper.NAME, new FirstOfHelper("")); handlebars.registerHelper(CurrentRackIsPresentHelper.NAME, new CurrentRackIsPresentHelper(agentMetadata.getEc2().getAvailabilityZone())); handlebars.registerHelper(ResolveHostnameHelper.NAME, new ResolveHostnameHelper(resolver)); handlebars.registerHelpers(new PreferSameRackWeightingHelper(config, agentMetadata)); handlebars.registerHelpers(IfEqualHelperSource.class); handlebars.registerHelpers(IfContainedInHelperSource.class); handlebars.registerHelper(ToNginxVarHelper.NAME, new ToNginxVarHelper()); return handlebars; }
Example 3
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 4
Source File: CustomHelperUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenHelperSourceIsCreated_ThenCanRegister() throws IOException { Handlebars handlebars = new Handlebars(templateLoader); handlebars.registerHelpers(new HelperSource()); Template template = handlebars.compile("person"); Person person = getPerson("Baeldung"); String templateString = template.apply(person); assertThat(templateString).isEqualTo("Baeldung - busy"); }
Example 5
Source File: StringHelpersTest.java From openapi-generator with Apache License 2.0 | 4 votes |
@BeforeMethod public void setup() { handlebars = new Handlebars(); handlebars.registerHelpers(StringHelpers.class); }
Example 6
Source File: HandlebarsScriptEngine.java From sling-samples with Apache License 2.0 | 4 votes |
private Handlebars setupHandlebars() { final Handlebars result = new Handlebars(); result.registerHelpers(StringHelpers.class); return result; }