当前位置: 首页>>代码示例>>Java>>正文


Java PebbleTemplate类代码示例

本文整理汇总了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;
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:25,代码来源:ExtendingPebbleTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:21,代码来源:ForNodeTest.java

示例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));
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:20,代码来源:EscapeFilter.java

示例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<>());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:17,代码来源:ArraySyntaxTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:25,代码来源:CoreTagsTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:22,代码来源:ArraySyntaxTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:17,代码来源:LogicTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:17,代码来源:GetAttributeTest.java

示例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;
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:23,代码来源:i18nFunction.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:18,代码来源:CoreTagsTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:24,代码来源:CoreTagsTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:21,代码来源:AttributeSubscriptSyntaxTest.java

示例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());
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:20,代码来源:EscaperExtensionTest.java

示例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>";
        }

    });
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:17,代码来源:EscaperExtensionTest.java

示例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));

}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:26,代码来源:CacheTest.java


注:本文中的com.mitchellbosecke.pebble.template.PebbleTemplate类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。