本文整理汇总了Java中com.mitchellbosecke.pebble.template.PebbleTemplate类的典型用法代码示例。如果您正苦于以下问题:Java PebbleTemplate类的具体用法?Java PebbleTemplate怎么用?Java PebbleTemplate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PebbleTemplate类属于com.mitchellbosecke.pebble.template包,在下文中一共展示了PebbleTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFilters
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Override
public Map<String, Filter> getFilters() {
Map<String, Filter> filters = new HashMap<>();
filters.put("noArgumentsButCanAccessContext", new Filter() {
@Override
public List<String> getArgumentNames() {
return null;
}
@Override
public String apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
if (context != null && self != null) {
return "success";
} else {
return "failure";
}
}
});
return filters;
}
示例2: testLoopIndex
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的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());
}
示例3: apply
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Override
public Object apply(Object inputObject, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException {
if (inputObject == null || inputObject instanceof SafeString) {
return inputObject;
}
String input = StringUtils.toString(inputObject);
String strategy = defaultStrategy;
if (args.get("strategy") != null) {
strategy = (String) args.get("strategy");
}
if (!strategies.containsKey(strategy)) {
throw new PebbleException(null, String.format("Unknown escaping strategy [%s]", strategy), lineNumber, self.getName());
}
return new SafeString(strategies.get(strategy).escape(input));
}
示例4: testAdditionOverloading3
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Test
public void testAdditionOverloading3() throws PebbleException, IOException {
//Arrange
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
String source = "{% set arr = 1 + [0,1] %}{{ arr }}";
PebbleTemplate template = pebble.getTemplate(source);
Writer writer = new StringWriter();
thrown.expect(PebbleException.class);
thrown.expectMessage(startsWith("Could not perform addition"));
//Act + Assert
template.evaluate(writer, new HashMap<>());
}
示例5: multipleForLoops
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
/**
* There were compilation issues when having two for loops in the same
* template due to the same variable name being declared twice.
*
* @throws PebbleException
*/
@Test
public void multipleForLoops() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
String source = "" + "{% for user in users %}{{ user.username }}{% endfor %}"
+ "{% for user in users %}{{ user.username }}{% endfor %}";
PebbleTemplate template = pebble.getTemplate(source);
Map<String, Object> context = new HashMap<>();
List<User> users = new ArrayList<>();
users.add(new User("Alex"));
users.add(new User("Bob"));
context.put("users", users);
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("AlexBobAlexBob", writer.toString());
}
示例6: testProblematicSubscriptSyntax
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@SuppressWarnings("serial")
@Test
public void testProblematicSubscriptSyntax() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
String source = "{{ person ['first-name'] }}";
PebbleTemplate template = pebble.getTemplate(source);
Map<String, Object> context = new HashMap<>();
context.put("person", new HashMap<String, Object>() {
{
put("first-name", "Bob");
}
});
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("Bob", writer.toString());
}
示例7: testBinaryOperatorsBigIntegerWithLong
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Test
public void testBinaryOperatorsBigIntegerWithLong() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
String source = "{{ number1 + number2 * number1 / number2 }}-{{number1 % number2}}";
PebbleTemplate template = pebble.getTemplate(source);
Writer writer = new StringWriter();
Map<String, Object> context = new HashMap<>();
context.put("number1", BigInteger.valueOf(100));
context.put("number2", 30L);
template.evaluate(writer, context);
assertEquals("200-10", writer.toString());
}
示例8: testListIndexAttribute
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Test
public void testListIndexAttribute() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
PebbleTemplate template = pebble.getTemplate("{{ arr[2] }}");
Map<String, Object> context = new HashMap<>();
List<String> data = new ArrayList<>();
data.add("Zero");
data.add("One");
data.add("Two");
context.put("arr", data);
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("Two", writer.toString());
}
示例9: execute
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Override
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
String basename = (String) args.get("bundle");
String key = (String) args.get("key");
Object params = args.get("params");
Locale locale = context.getLocale();
ResourceBundle bundle = ResourceBundle.getBundle(basename, locale, new UTF8Control());
Object phraseObject = bundle.getObject(key);
if (phraseObject != null && params != null) {
if (params instanceof List) {
List<?> list = (List<?>) params;
return MessageFormat.format(phraseObject.toString(), list.toArray());
} else {
return MessageFormat.format(phraseObject.toString(), params);
}
}
return phraseObject;
}
示例10: testFor
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Test
public void testFor() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
String source = "{% for user in users %}{% if loop.first %}[{{ loop.length }}]{% endif %}{% if loop.last %}[{{ loop.length }}]{% endif %}{{ loop.index }}{{ loop.revindex }}{{ user.username }}{% endfor %}";
PebbleTemplate template = pebble.getTemplate(source);
Map<String, Object> context = new HashMap<>();
List<User> users = new ArrayList<>();
users.add(new User("Alex"));
users.add(new User("Bob"));
users.add(new User("John"));
context.put("users", users);
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("[3]02Alex11Bob[3]20John", writer.toString());
}
示例11: testParallelTagWhileEvaluationContextIsChanging
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
/**
* The for loop will add variables into the evaluation context during
* runtime and there was an issue where the evaluation context wasn't thread
* safe.
*
* @throws PebbleException
* @throws IOException
*/
@Test
public void testParallelTagWhileEvaluationContextIsChanging() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false)
.executorService(Executors.newCachedThreadPool()).build();
String source = "{% for num in array %}{% parallel %}{{ loop.index }}{% endparallel %}{% endfor%}";
PebbleTemplate template = pebble.getTemplate(source);
Writer writer = new StringWriter();
Map<String, Object> context = new HashMap<>();
context.put("array", new int[10]);
template.evaluate(writer, context);
assertEquals("0123456789", writer.toString());
}
示例12: testAccessingValueWithSubscript
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@SuppressWarnings("serial")
@Test
public void testAccessingValueWithSubscript() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
String source = "{{ person['first-name'] }}";
PebbleTemplate template = pebble.getTemplate(source);
Map<String, Object> context = new HashMap<>();
context.put("person", new HashMap<String, Object>() {
{
put("first-name", "Bob");
}
});
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("Bob", writer.toString());
}
示例13: testCustomEscapingStrategy
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Test
public void testCustomEscapingStrategy() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false)
.defaultEscapingStrategy("custom").addEscapingStrategy("custom", new EscapingStrategy() {
@Override
public String escape(String input) {
return input.replace('a', 'b');
}
}).build();
// replaces all a's with b's
PebbleTemplate template = pebble.getTemplate("{{ text }}");
Map<String, Object> context = new HashMap<>();
context.put("text", "my name is alex");
Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("my nbme is blex", writer.toString());
}
示例14: getFunctions
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
@Override
public Map<String, Function> getFunctions() {
return Collections.<String, Function>singletonMap("bad", new Function() {
@Override
public List<String> getArgumentNames() {
return null;
}
@Override
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
return "<script>alert(\"injection\");</script>";
}
});
}
示例15: templatesWithSameNameOverridingCache
import com.mitchellbosecke.pebble.template.PebbleTemplate; //导入依赖的package包/类
/**
* There was once an issue where the cache was unable to differentiate
* between templates of the same name but under different directories.
*
* @throws PebbleException
*/
@Test
public void templatesWithSameNameOverridingCache() throws PebbleException, IOException {
PebbleEngine engine = new PebbleEngine.Builder().strictVariables(false).build();
PebbleTemplate cache1 = engine.getTemplate("templates/cache/cache1/template.cache.peb");
PebbleTemplate cache2 = engine.getTemplate("templates/cache/cache2/template.cache.peb");
Writer writer1 = new StringWriter();
Writer writer2 = new StringWriter();
cache1.evaluate(writer1);
cache2.evaluate(writer2);
String cache1Output = writer1.toString();
String cache2Output = writer2.toString();
assertFalse(cache1Output.equals(cache2Output));
}