本文整理汇总了Java中com.mitchellbosecke.pebble.PebbleEngine类的典型用法代码示例。如果您正苦于以下问题:Java PebbleEngine类的具体用法?Java PebbleEngine怎么用?Java PebbleEngine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PebbleEngine类属于com.mitchellbosecke.pebble包,在下文中一共展示了PebbleEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSomeStuff
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Test
public void testSomeStuff() throws PebbleException, IOException {
ClasspathLoader loader = new ClasspathLoader();
loader.setPrefix(getClass().getPackage().getName().replace('.', '/'));
PebbleEngine engine = new PebbleEngine.Builder()
.loader(loader)
.build();
PebbleTemplate template = engine.getTemplate("sample.html");
StringWriter result = new StringWriter();
template.evaluate(result, ImmutableMap.of("foo",new Fake(ImmutableMap.of("bar", "bar"))));
assertEquals("-->bar<--", result.toString());
}
示例2: testVariableScope
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Test
public void testVariableScope() throws Exception {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
StringBuilder source = new StringBuilder("{% set fooList = range(1, 1) %}");
source.append("{% for item in fooList %}");
source.append("{% set foo1 = 'fooValue' %}");
source.append("Foo1 value : {{ foo1 }}");
source.append("{% endfor %}");
source.append("Foo1 value : {{ foo1 }}");
source.append("{% for item in fooList %}");
source.append("{% set foo2 = 'fooValue2' %}");
source.append("Foo2 value : {{ foo2 }}");
source.append("{% endfor %}");
source.append("Foo2 value : {{ foo2 }}");
PebbleTemplate template = pebble.getTemplate(source.toString());
Map<String, Object> context = new HashMap<>();
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("Foo1 value : fooValueFoo1 value : Foo2 value : fooValue2Foo2 value : ", writer.toString());
}
示例3: testNestedLoop
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Test
public void testNestedLoop() throws Exception {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
StringBuilder source = new StringBuilder("{% for i in 0..2 %}");
source.append("{% for j in 0..2 %}");
source.append("i={{ i }} j={{ j }} ");
source.append("{% endfor %}");
source.append("{% endfor %}");
source.append("i={{ i }} j={{ j }} ");
PebbleTemplate template = pebble.getTemplate(source.toString());
Map<String, Object> context = new HashMap<>();
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("i=0 j=0 i=0 j=1 i=0 j=2 i=1 j=0 i=1 j=1 i=1 j=2 i=2 j=0 i=2 j=1 i=2 j=2 i= j= ", writer.toString());
}
示例4: testLoopIndex
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Test
public void testLoopIndex() throws Exception {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
StringBuilder source = new StringBuilder("{% for i in 0..2 %}");
source.append("{% for j in 0..2 %}");
source.append("inner={{ loop.index }} ");
source.append("{% endfor %}");
source.append("outer={{ loop.index }} ");
source.append("{% endfor %}");
source.append("outside loop={{ loop.index }} ");
PebbleTemplate template = pebble.getTemplate(source.toString());
Map<String, Object> context = new HashMap<>();
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("inner=0 inner=1 inner=2 outer=0 inner=0 inner=1 inner=2 outer=1 inner=0 inner=1 inner=2 outer=2 outside loop= ", writer.toString());
}
示例5: pebbleEngine
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Bean
public PebbleEngine pebbleEngine() {
PebbleEngine.Builder builder = new PebbleEngine.Builder();
builder.loader(this.pebbleLoader);
builder.extension(this.pebbleSpringExtension());
if (this.extensions != null && !this.extensions.isEmpty()) {
builder.extension(this.extensions.toArray(new Extension[this.extensions.size()]));
}
if (!this.properties.isCache()) {
builder.cacheActive(false);
}
if (this.properties.getDefaultLocale() != null) {
builder.defaultLocale(this.properties.getDefaultLocale());
}
builder.strictVariables(this.properties.isStrictVariables());
return builder.build();
}
示例6: generate
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
protected void generate(String path, String name, String templatePath, Map model){
try {
String pathString = path + "/" + name;
System.out.println("Creating " + pathString);
Path filePath = Paths.get(pathString);
Files.createDirectories(filePath.getParent());
PebbleExtension extension = new PebbleExtension();
extension.getFilters().put("data_type", new DataTypeFilter());
extension.getFilters().put("camel_case", new CamelCaseFilter());
extension.getFilters().put("singular", new SingularFilter());
extension.getFilters().put("plural", new PluralFilter());
PebbleEngine engine = new PebbleEngine.Builder().extension(extension).build();
PebbleTemplate template = engine.getTemplate(templatePath);
java.io.Writer writer = new FileWriter(path + "/" + name);
template.evaluate(writer, model);
System.out.println(writer.toString());
writer.close();
} catch (PebbleException | IOException ex) {
Logger.getLogger(AbstractWriter.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例7: registerLayoutRenderer
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Override
public void registerLayoutRenderer(Directory root, StampoGlobalConfiguration configuration,
Map<String, Function<LayoutParameters, LayoutProcessorOutput>> extensionProcessor) {
PebbleEngine engine = build(root, configuration);
extensionProcessor.put(
"peb",
lParam -> {
try {
StringWriter sw = new StringWriter();
engine.getTemplate(lParam.layoutTemplate.get().toString()).evaluate(sw, lParam.model,
lParam.locale);
return new LayoutProcessorOutput(sw.toString(), "pebble", lParam.layoutTemplate,
lParam.locale);
} catch (PebbleException | IOException e) {
throw new LayoutException(lParam.layoutTemplate.get(), lParam.targetResource, e);
}
});
}
示例8: registerResourceRenderer
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Override
public void registerResourceRenderer(
Directory root,
StampoGlobalConfiguration configuration,
Map<String, Function<FileResourceParameters, FileResourceProcessorOutput>> extensionProcessor) {
PebbleEngine pebble = build(root, configuration);
extensionProcessor.put("peb", params -> {
try {
Writer writer = new StringWriter();
pebble.getTemplate(params.fileResource.getPath().toString())//
.evaluate(writer, params.model, params.locale);
return new FileResourceProcessorOutput(writer.toString(), params.fileResource.getPath(),
"pebble", params.locale);
} catch (PebbleException | IOException e) {
throw new TemplateException(params.fileResource.getPath(), e);
}
});
}
示例9: createPebbleEngine
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
/**
* Creates a PebbleEngine instance.
*
* @return a PebbleEngine object that can be used to create PebbleTemplate objects
*/
public PebbleEngine createPebbleEngine() {
PebbleEngine.Builder builder = new PebbleEngine.Builder();
builder.strictVariables(strictVariables);
if (defaultLocale != null) {
builder.defaultLocale(defaultLocale);
}
if (templateLoaders == null) {
if (templateLoaderPaths != null && templateLoaderPaths.length > 0) {
List<Loader<?>> templateLoaderList = new ArrayList<>();
for (String path : templateLoaderPaths) {
templateLoaderList.add(getTemplateLoaderForPath(path));
}
setTemplateLoader(templateLoaderList);
}
}
Loader<?> templateLoader = getAggregateTemplateLoader(templateLoaders);
builder.loader(templateLoader);
return builder.build();
}
示例10: pebbleEngine
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Bean
@ConditionalOnMissingBean(PebbleEngine.class)
public PebbleEngine pebbleEngine(final PebbleEngineConfigurer pebbleEngineConfigurer) {
final PebbleTemplateLoader loader = new PebbleTemplateLoader();
loader.setResourceLoader(new ServletContextResourceLoader(context.getServletContext()));
loader.setPrefix(this.properties.getPrefix());
loader.setSuffix(this.properties.getSuffix());
final List<Loader<?>> list = new ArrayList<>();
list.add(loader);
list.add(new ClasspathLoader());
list.add(new FileLoader());
final DelegatingLoader loaderAll = new DelegatingLoader(list);
pebbleEngineConfigurer.setLoader(loaderAll);
pebbleEngineConfigurer.setCache(this.properties.isCache());
pebbleEngineConfigurer.setCacheSize(this.properties.getCacheSize());
return pebbleEngineConfigurer.getPebbleEngine();
}
示例11: getPebbleEngine
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
public PebbleEngine getPebbleEngine() {
final PebbleEngine.Builder builder = new PebbleEngine.Builder()
.loader(this.loader)
.extension(extensions.toArray(new Extension[extensions.size()]));
if (cache) {
builder.templateCache(CacheBuilder.newBuilder().maximumSize(cacheSize).build())
.tagCache(CacheBuilder.newBuilder().maximumSize(cacheSize).build());
} else {
builder.templateCache(CacheBuilder.newBuilder().maximumSize(0).build())
.tagCache(CacheBuilder.newBuilder().maximumSize(0).build());
}
log.debug("PebbleEngine built! 这里不写几个汉字不显眼!!!");
return builder.build();
}
示例12: handleLeadingSlash
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
private void handleLeadingSlash(final HttpServletRequest req, HttpServletResponse res, Site sites) throws PebbleException,
IOException, ServletException {
if (req.getMethod().equals("GET")) {
res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
res.setHeader("Location", rewritePageUrl(req));
return;
} else if (req.getMethod().equals("POST")) {
if (CoreConfiguration.getConfiguration().developmentMode()) {
PebbleEngine engine = new PebbleEngine.Builder().loader(new StringLoader()).extension(new CMSExtensions()).build();
PebbleTemplate compiledTemplate =
engine.getTemplate("<html><head></head><body><h1>POST action with backslash</h1><b>You posting data with a URL with a backslash. Alter the form to post with the same URL without the backslash</body></html>");
res.setStatus(500);
res.setContentType("text/html");
compiledTemplate.evaluate(res.getWriter());
} else {
renderer.errorPage(req, res, sites, 500);
}
}
}
示例13: renderString
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Override
public void renderString(String templateContent, Map<String, Object> model, Writer writer) {
String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG);
if (StringUtils.isNullOrEmpty(language)) {
language = getLanguageOrDefault(language);
}
Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE);
if (locale == null) {
locale = getLocaleOrDefault(language);
}
try {
PebbleEngine stringEngine = new PebbleEngine.Builder()
.loader(new StringLoader())
.strictVariables(engine.isStrictVariables())
.templateCache(null)
.build();
PebbleTemplate template = stringEngine.getTemplate(templateContent);
template.evaluate(writer, model, locale);
writer.flush();
} catch (Exception e) {
throw new PippoRuntimeException(e);
}
}
示例14: basic
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Test
public void basic() throws Exception {
Locale locale = Locale.getDefault();
new MockUnit(Env.class, Config.class, Binder.class, PebbleEngine.class)
.expect(defLoader)
.expect(newEngine)
.expect(env("dev", locale))
.expect(cacheStatic)
.expect(cache("pebble.cache", null))
.expect(cache(0))
.expect(cache("pebble.tagCache", null))
.expect(tagCache(0))
.expect(locale(locale))
.expect(build)
.expect(bindEngine)
.expect(renderer)
.run(unit -> {
new Pebble()
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
});
}
示例15: prefixAsRoot
import com.mitchellbosecke.pebble.PebbleEngine; //导入依赖的package包/类
@Test
public void prefixAsRoot() throws Exception {
Locale locale = Locale.getDefault();
new MockUnit(Env.class, Config.class, Binder.class, PebbleEngine.class)
.expect(defLoader)
.expect(newEngine)
.expect(env("dev", locale))
.expect(cacheStatic)
.expect(cache("pebble.cache", null))
.expect(cache(0))
.expect(cache("pebble.tagCache", null))
.expect(tagCache(0))
.expect(locale(locale))
.expect(build)
.expect(bindEngine)
.expect(renderer)
.run(unit -> {
new Pebble("/", ".html")
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
});
}