private static Loader<String> loader(final String prefix, final String suffix) { Loader<String> loader = new ClasspathLoader(); loader.setPrefix(prefix); loader.setSuffix(suffix); return loader; }
public static PebbleEngine getNewEngine() { PebbleEngine engine; try { Loader<?> loader = new ClasspathLoader(); engine = new PebbleEngine.Builder().loader(loader).strictVariables(false).build(); } catch (PebbleException e) { e.printStackTrace(); throw new IllegalStateException(e); } return engine; }
@Override public void setPrefix(String prefix) { this.prefix = prefix; for (Loader<?> loader: this.loaders) { loader.setPrefix(prefix); } }
@Bean public Loader<?> pebbleLoader() { ClasspathLoader loader = new ClasspathLoader(); loader.setCharset(this.properties.getCharsetName()); // classpath loader does not like leading slashes in resource paths loader.setPrefix(stripLeadingSlash(this.properties.getPrefix())); loader.setSuffix(this.properties.getSuffix()); return loader; }
String zepto = readString(loader, "js/zepto.min.js"); ClasspathLoader cpathloader = new ClasspathLoader(); cpathloader.setPrefix("whoops"); cpathloader.setSuffix(".html"); PebbleEngine engine = new PebbleEngine.Builder() .loader(cpathloader)
defaultLoadingStrategies.add(new ClasspathLoader()); defaultLoadingStrategies.add(new FileLoader()); this.loader = new DelegatingLoader(defaultLoadingStrategies);
@Override public void setSuffix(String suffix) { this.suffix = suffix; for (Loader<?> loader: this.loaders) { loader.setSuffix(suffix); } }
@Override public Reader getReader(DelegatingLoaderCacheKey cacheKey) { Reader reader = null; final int size = this.loaders.size(); for (int i = 0; i < size; i++) { Loader<?> loader = this.loaders.get(i); Object delegatingKey = cacheKey.getDelegatingCacheKeys().get(i); try { reader = this.getReaderInner(loader, delegatingKey); if (reader != null) { break; } } catch (LoaderException e) { // do nothing } } if (reader == null) { throw new LoaderException(null, "Could not find template \"" + cacheKey.getTemplateName() + "\""); } return reader; }
@Override public DelegatingLoaderCacheKey createCacheKey(String templateName) { List<Object> keys = new ArrayList<>(); for (Loader<?> loader : this.loaders) { keys.add(loader.createCacheKey(templateName)); } return new DelegatingLoaderCacheKey(keys, templateName); } }
@Override public String resolveRelativePath(String relativePath, String anchorPath) { if (relativePath == null) { return null; } for (Loader<?> loader : this.loaders) { String path = loader.resolveRelativePath(relativePath, anchorPath); if (path != null) { return path; } } return null; }
private <T> Reader getReaderInner(Loader<T> delegatingLoader, Object cacheKey) { // This unchecked cast is ok, because we ensure that the type of the // cache key corresponds to the loader when we create the key. @SuppressWarnings("unchecked") T castedKey = (T) cacheKey; return delegatingLoader.getReader(castedKey); }
@Override public void setCharset(String charset) { this.charset = charset; for (Loader<?> loader: this.loaders) { loader.setCharset(charset); } }
DelegatingLoaderCacheKey(final List<Object> delegatingCacheKeys, final String templateName) { this.delegatingCacheKeys = Collections.unmodifiableList(new ArrayList<>(delegatingCacheKeys)); this.templateName = templateName; this.hashCode = this.caclulateHashCode(); }
/** * Loads, parses, and compiles a template using a StringLoader into an instance of PebbleTemplate * and returns this instance. * * @param templateName The name of the template * @return PebbleTemplate The compiled version of the template */ public PebbleTemplate getLiteralTemplate(String templateName) { return this.getTemplate(templateName, new StringLoader()); }
private PebbleTemplate getTemplate(String templateName, Loader loader) { /* * template name will be null if user uses the extends tag with an * expression that evaluates to null */ if (templateName == null) { return null; } if (loader == null) { throw new LoaderException(null, "Loader has not yet been specified."); } Object cacheKey = loader.createCacheKey(templateName); return this.templateCache .computeIfAbsent(cacheKey, k -> this.getPebbleTemplate(templateName, loader, cacheKey)); }
@Bean public Loader<?> pebbleLoader() { ClasspathLoader loader = new ClasspathLoader(); loader.setCharset(this.properties.getCharsetName()); // classpath loader does not like leading slashes in resource paths loader.setPrefix(stripLeadingSlash(this.properties.getPrefix())); loader.setSuffix(this.properties.getSuffix()); return loader; }
/** * This method resolves the given relative path based on this template file path. * * @param relativePath the path which should be resolved. * @return the resolved path. */ public String resolveRelativePath(String relativePath) { String resolved = this.engine.getLoader().resolveRelativePath(relativePath, this.name); if (resolved == null) { return relativePath; } else { return resolved; } }