使用Pebble版本3.0.6。
我需要检查值'v'是否具有特定变量(转换为Java:如果Object v具有特定属性)。寻找类似的东西
{% if v instanceof test.MyClass %}
...
{% endif %}
或
{% if v has myProperty %}
...
{% endif %}
据我所知,这两个都不可用。用Pebble实现此目的的最佳方法是什么?
更新
上下文:
strictVariables
= true
答案 0 :(得分:1)
不是内置的,但卵石允许您编写custom extensions。在Java instanceof
中是一个运算符,卵石允许您为其编写扩展名。
我们需要三件事来为操作员编写自定义扩展名:
implements BinaryOperator
)的类extends BinaryExpression<Object>
)的类implements Extension
。我们将运算符定义为instanceof
,优先级为30
,根据java,instanceof
的优先级与< > <= >=
相同,{{ 3}}这些运算符的优先级为30
,因此我们使用它。评估此操作的节点是InstanceofExpression.class
,这是我们将在步骤2中创建的类。
public class InstanceofOperator implements BinaryOperator {
/**
* This precedence is set based on
* <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html">Java
* Operators</a> 30 is the same precedence pebble has set for operators like {@code instanceof}
* like <a href="https://github.com/PebbleTemplates/pebble/wiki/extending-pebble">Extending
* Pebble</a>.
*/
public int getPrecedence() {
return 30;
}
public String getSymbol() {
return "instanceof";
}
public Class<? extends BinaryExpression<?>> getNodeClass() {
return InstanceofExpression.class;
}
public Associativity getAssociativity() {
return Associativity.LEFT;
}
}
我们现在必须写出运算符的计算结果,在这种情况下,我们将返回true
if left instanceof right
。对于此评估的正确部分,我们使用String
,其中必须包含该类的完整合格名称,例如,1 instanceof "java.lang.String"
将返回false
,或1 instanceof "java.lang.Long"
返回true
。
如果无法用right
找到/加载Class.forName
类,则会引发异常。
public class InstanceofExpression extends BinaryExpression<Object> {
@Override
public Object evaluate(PebbleTemplateImpl self, EvaluationContextImpl context) {
// The left class (left instanceof right)
Object leftClass = getLeftExpression().evaluate(self, context);
// The right class, this is a string with the full qualifying name of the class eg (left
// instanceof "java.lang.String")
String rightClassname = getRightExpression().evaluate(self, context).toString();
// We must get the right class as Class<?> in order to check if left is an instanceof right
Class<?> rightClass;
try {
rightClass = Class.forName(rightClassname);
} catch (ClassNotFoundException e) {
throw new PebbleException(e.getCause(),
String.format("Cannot find class %s", rightClassname));
}
// Check if the left class is an instanceof the right class
return rightClass.isInstance(leftClass);
}
}
我们现在必须为Pebble创建扩展,这很简单。我们创建自定义InstanceofOperator
的实例,并将其作为二进制运算符返回:
public class InstanceofExtension implements Extension {
@Override
public List<BinaryOperator> getBinaryOperators() {
return Arrays.asList(new InstanceofOperator());
}
// ...
// Other methods required by implementing Extension, these other methods can just return null.
// ...
// ...
}
或者,您可以像这样实现getBinaryOperators
方法,而不是整个步骤1 :
@Override
public List<BinaryOperator> getBinaryOperators() {
return Arrays.asList(new BinaryOperatorImpl("instanceof", 30, InstanceofExpression.class,
Associativity.LEFT));
}
我们现在可以使用.extension(new InstanceofExtension())
添加自定义扩展名:
PebbleEngine engine =
new PebbleEngine.Builder().strictVariables(true)
.extension(new InstanceofExtension()).build();
PebbleTemplate compiledTemplate = engine.getTemplate("home.html");
// Test with Person as v
Writer personWriter = new StringWriter();
Map<String, Object> context = new HashMap<>();
context.put("v", new Person());
compiledTemplate.evaluate(personWriter, context);
System.out.println(personWriter.toString()); // <b>asdasdasdasds</b> is present
// Test with Fruit as v
Writer fruitWriter = new StringWriter();
context.put("v", new Fruit());
compiledTemplate.evaluate(fruitWriter, context);
System.out.println(fruitWriter.toString()); // <b>asdasdasdasds</b> is not present, but
// <b>red</b> is
我们在上面处理的Person
类被定义为扩展Entity
。为了证明这一概念的有效性,我们还有一个类Fruit
,它没有扩展Entity
。我们在v
中测试了这两个不同的类:
class Person extends Entity {
public String name = "me";
}
class Entity {
public String asd = "asdasdasdasds";
}
class Fruit {
public String color = "red";
}
home.html ,我们检查v
或 Person
的{{1}}是Fruit
的实例或com.mypackage.test.Entity
:
com.mypackage.test.Fruit
输出为:
<html>
<body>
{% if v instanceof "com.mypackage.test.Entity" %}
<b>{{ v.asd }}</b>
{% endif %}
{% if v instanceof "com.mypackage.test.Fruit" %}
<b>{{ v.color }}</b>
{% endif %}
</body>
</html>
“左侧的 not 右侧的实例”版本为:
<html>
<body>
<b>asdasdasdasds</b>
</body>
</html>
<html>
<body>
<b>red</b>
</body>
</html>