本文整理汇总了Java中com.mitchellbosecke.pebble.PebbleEngine.getTemplate方法的典型用法代码示例。如果您正苦于以下问题:Java PebbleEngine.getTemplate方法的具体用法?Java PebbleEngine.getTemplate怎么用?Java PebbleEngine.getTemplate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mitchellbosecke.pebble.PebbleEngine
的用法示例。
在下文中一共展示了PebbleEngine.getTemplate方法的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: 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);
}
}
示例6: 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);
}
}
}
示例7: 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);
}
}
示例8: testPropertyTreeSupport
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
@Test
public void testPropertyTreeSupport() throws PebbleException, IOException {
ClasspathLoader loader = new ClasspathLoader();
loader.setPrefix(getClass().getPackage().getName().replace('.', '/'));
PebbleEngine engine = engineWithExtensions(loader);
PebbleTemplate template = engine.getTemplate("sample.html");
StringWriter result = new StringWriter();
PropertyTree propertyTree = FixedPropertyTree.builder()
.put("bar", "bar")
.build();
template.evaluate(result, ImmutableMap.of("foo",propertyTree));
assertEquals("-->[bar]<--", result.toString());
}
示例9: testRawFilter
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
@Test
public void testRawFilter() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
PebbleTemplate template = pebble.getTemplate("{{ text | upper | raw }}");
Map<String, Object> context = new HashMap<>();
context.put("text", "<br />");
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("<BR />", writer.toString());
}
示例10: testRawFilterNotBeingLast
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
@Test
public void testRawFilterNotBeingLast() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
PebbleTemplate template = pebble.getTemplate("{{ text | raw | upper}}");
Map<String, Object> context = new HashMap<>();
context.put("text", "<br />");
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("<BR />", writer.toString());
}
示例11: testRawFilterWithinAutoescapeToken
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
@Test
public void testRawFilterWithinAutoescapeToken() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false)
.autoEscaping(false).build();
PebbleTemplate template = pebble.getTemplate("{% autoescape 'html' %}{{ text|raw }}{% endautoescape %}");
Map<String, Object> context = new HashMap<>();
context.put("text", "<br />");
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("<br />", writer.toString());
}
示例12: testRawFilterWithJsonObject
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
@Test
public void testRawFilterWithJsonObject() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
PebbleTemplate template = pebble.getTemplate("{{ text | raw }}");
Map<String, Object> context = new HashMap<>();
context.put("text", new JsonObject());
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals(JsonObject.JSON_VALUE, writer.toString());
}
示例13: testRawFilterWithNullObject
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
@Test
public void testRawFilterWithNullObject() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
PebbleTemplate template = pebble.getTemplate("{{ text | raw }}");
Writer writer = new StringWriter();
template.evaluate(writer, new HashMap<String, Object>());
assertEquals("", writer.toString());
}
示例14: testMultipleMacroCalls
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
/**
* Checks, if macros are called to often
*/
@Test
public void testMultipleMacroCalls() throws PebbleException, IOException {
// Build a pebble engine with one configured filter ("testfilter" - TestFilter.java)
PebbleEngine pebble = new PebbleEngine.Builder()
.extension(new PebbleExtension())
.strictVariables(false).build();
// Resets the counter of the filter
TestFilter.counter = 0;
/*
* Runs the test scenario:
* index-template with an import of the macro and a call to this macro (Call #1)
* index-templates includes "include.peb"
*
* include.peb with an import of the macro and a call to this macro (Call #2)
*
* We track the number of macro-calls by using a small "Filter" (TestFilter) that just
* counts, how often it is called.
*/
PebbleTemplate template = pebble.getTemplate("templates/macros/index.peb");
Writer writer = new StringWriter();
template.evaluate(writer);
// We expect, that the TestFilter was called 2x
assertEquals(2, TestFilter.getCounter());
}
示例15: testInvalidMacro
import com.mitchellbosecke.pebble.PebbleEngine; //导入方法依赖的package包/类
@Test
public void testInvalidMacro() throws IOException {
PebbleEngine pebble = new PebbleEngine.Builder().build();
try {
PebbleTemplate template = pebble.getTemplate("templates/macros/invalid.macro.peb");
Writer writer = new StringWriter();
template.evaluate(writer);
fail("expected PebbleException");
} catch (PebbleException e) {
assertEquals(e.getLineNumber(), (Integer) 2);
assertEquals(e.getFileName(), "templates/macros/invalid.macro.peb");
}
}