com.github.jknack.handlebars.io.ClassPathTemplateLoader Java Examples

The following examples show how to use com.github.jknack.handlebars.io.ClassPathTemplateLoader. 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: Utils.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
public static TemplatePath parseTemplateUrl(String templatePath) throws GenerateException {
    if (templatePath == null) {
        return null;
    }
    TemplatePath tp;
    if (templatePath.startsWith(CLASSPATH)) {
        String resPath = templatePath.substring(CLASSPATH.length());
        tp = extractTemplateObject(resPath);
        tp.loader = new ClassPathTemplateLoader(tp.prefix, tp.suffix);
    } else {
        tp = extractTemplateObject(templatePath);
        tp.loader = new FileTemplateLoader(tp.prefix, tp.suffix);
    }

    return tp;
}
 
Example #3
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 #4
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 #5
Source File: Handlebars.java    From template-benchmark with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Setup
public void setup() throws IOException {
  template = new com.github.jknack.handlebars.Handlebars(new ClassPathTemplateLoader("/", ".html"))
          .registerHelper("minus", new Helper<Stock>() {
            @Override
            public CharSequence apply(final Stock stock, final Options options)
                throws IOException {
              return stock.getChange() < 0 ? new SafeString("class=\"minus\"") : null;
            }
          }).compile("templates/stocks.hbs");
  this.context = getContext();
}
 
Example #6
Source File: HandlebarsTemplateEngine.java    From spark-template-engines with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #7
Source File: HandlebarsFactory.java    From commercetools-sunrise-java with Apache License 2.0 5 votes vote down vote up
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 #8
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 #9
Source File: BasicUsageUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 #10
Source File: BasicUsageUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 #11
Source File: Templating.java    From StubbornJava with MIT License 4 votes vote down vote up
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 vote down vote up
public Builder withResourceLoaders() {
    log.debug("using resource loaders");
    loaders.add(new ClassPathTemplateLoader());
    loaders.add(new ClassPathTemplateLoader(TemplateLoader.DEFAULT_PREFIX, ".sql"));
    return this;
}
 
Example #13
Source File: HandlebarsCmsHelperTest.java    From commercetools-sunrise-java with Apache License 2.0 4 votes vote down vote up
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)));
}