Java Code Examples for com.github.jknack.handlebars.io.FileTemplateLoader

The following examples show how to use com.github.jknack.handlebars.io.FileTemplateLoader. These examples are extracted from open source projects. 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 Project: product-microgateway   Source File: CodegenUtils.java    License: 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 Project: swagger-maven-plugin   Source File: Utils.java    License: 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
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 4
Source Project: StubbornJava   Source File: Templating.java    License: MIT License 4 votes vote down vote up
public Builder withLocalResourceLoaders(String root) {
    log.debug("using local loaders");
    loaders.add(new FileTemplateLoader(root));
    loaders.add(new FileTemplateLoader(root, ".sql"));
    return this;
}
 
Example 5
Source Project: StubbornJava   Source File: Templating.java    License: MIT License 4 votes vote down vote up
public Builder withLocalResourceLoaders(String root) {
    log.debug("using local loaders");
    loaders.add(new FileTemplateLoader(root));
    loaders.add(new FileTemplateLoader(root, ".sql"));
    return this;
}
 
Example 6
Source Project: fess   Source File: ViewHelper.java    License: Apache License 2.0 4 votes vote down vote up
public String createCacheContent(final Map<String, Object> doc, final String[] queries) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final FileTemplateLoader loader = new FileTemplateLoader(ResourceUtil.getViewTemplatePath().toFile());
    final Handlebars handlebars = new Handlebars(loader);

    Locale locale = ComponentUtil.getRequestManager().getUserLocale();
    if (locale == null) {
        locale = Locale.ENGLISH;
    }
    String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
    if (url == null) {
        url = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
    }
    doc.put(fessConfig.getResponseFieldUrlLink(), getUrlLink(doc));
    String createdStr;
    final Date created = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCreated(), Date.class);
    if (created != null) {
        final SimpleDateFormat sdf = new SimpleDateFormat(CoreLibConstants.DATE_FORMAT_ISO_8601_EXTEND);
        createdStr = sdf.format(created);
    } else {
        createdStr = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
    }
    doc.put(CACHE_MSG, ComponentUtil.getMessageManager().getMessage(locale, "labels.search_cache_msg", url, createdStr));

    doc.put(QUERIES, queries);

    String cache = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCache(), String.class);
    if (cache != null) {
        final String mimetype = DocumentUtil.getValue(doc, fessConfig.getIndexFieldMimetype(), String.class);
        if (!ComponentUtil.getFessConfig().isHtmlMimetypeForCache(mimetype)) {
            cache = StringEscapeUtils.escapeHtml4(cache);
        }
        cache = ComponentUtil.getPathMappingHelper().replaceUrls(cache);
        if (queries != null && queries.length > 0) {
            doc.put(HL_CACHE, replaceHighlightQueries(cache, queries));
        } else {
            doc.put(HL_CACHE, cache);
        }
    } else {
        doc.put(fessConfig.getIndexFieldCache(), StringUtil.EMPTY);
        doc.put(HL_CACHE, StringUtil.EMPTY);
    }

    try {
        final Template template = handlebars.compile(cacheTemplateName);
        final Context hbsContext = Context.newContext(doc);
        return template.apply(hbsContext);
    } catch (final Exception e) {
        logger.warn("Failed to create a cache response.", e);
    }

    return null;
}