com.github.jknack.handlebars.helper.StringHelpers Java Examples

The following examples show how to use com.github.jknack.handlebars.helper.StringHelpers. 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 vote down vote up
/**
 * 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: TestTemplatesCompile.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@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: TestMessages.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #4
Source File: HandlebarsTemplateService.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
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 #5
Source File: HandlebarsEngineAdapter.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
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 #6
Source File: HandlebarsHelper.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
public static Template template(String templateSubFolder, String templateName) {
	try {
		Handlebars handlebars = new Handlebars(
				new ClassPathTemplateLoader("/templates/" + templateSubFolder));
		handlebars.registerHelper("replace", StringHelpers.replace);
		handlebars.registerHelper("capitalizeFirst", StringHelpers.capitalizeFirst);
		return handlebars.compile(templateName);
	}
	catch (IOException e) {
		throw new IllegalStateException(e);
	}
}
 
Example #7
Source File: TemplateEngine.java    From bonita-ui-designer with GNU General Public License v2.0 5 votes vote down vote up
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 #8
Source File: Xsd2JaxbGenerator.java    From legstar-core2 with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #9
Source File: HandlebarsHelper.java    From spring-cloud-release with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: HandlebarsHelpersSource.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static CharSequence timestampFormat(String timestampVar, Options options) throws IOException {
	Date timestampDateValue = new Date(Long.parseLong(timestampVar));
	return StringHelpers.dateFormat.apply(timestampDateValue, options);
}
 
Example #11
Source File: HandlebarsScriptEngine.java    From sling-samples with Apache License 2.0 4 votes vote down vote up
private Handlebars setupHandlebars() {
    final Handlebars result = new Handlebars();
    result.registerHelpers(StringHelpers.class);
    return result;
}