[英]How to invoke custom functions in JTwig?
Is it possible to invoke custom function (say static method) from JTwig template? 是否可以从JTwig模板调用自定义函数(例如静态方法)?
For instance, in Thymeleaf framework I can call any static method via full name of its class. 例如,在Thymeleaf框架中,我可以通过其类的全名来调用任何静态方法。 So I'm looking for something similar for JTwig.
所以我正在为JTwig寻找类似的东西。
This is not well documented, but here a snippet in pure Java: 这没有很好的文档记录,但是这里是纯Java的摘要:
final SimpleJtwigFunction myFunction = new SimpleJtwigFunction() {
@Override
public String name() {
return "get_type";
}
@Override
public Object execute(FunctionRequest functionRequest) {
return "toto";
}
};
final EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder
.configuration()
.functions()
.add(myFunction)
.and()
.build();
final JtwigTemplate template = JtwigTemplate.classpathTemplate("hive_create_table.sql.twig", configuration);
And template: 和模板:
CREATE EXTERNAL TABLE sample2(
{% for field in fields%}{% if field.name != "serialVersionUID" %}
`{{ field.name }}`:{{ field.type | get_type | upper }},{% endif %}{% endfor %}
)
I've found the following solution: 我发现以下解决方案:
1) create own function via extending org.jtwig.functions.SimpleJtwigFunction
1)通过扩展
org.jtwig.functions.SimpleJtwigFunction
创建自己的函数
2) register it in extended org.jtwig.environment.EnvironmentConfiguration
2)在扩展的
org.jtwig.environment.EnvironmentConfiguration
注册
3) construct custom bean for the extended configuration and pass it into the JtwigViewResolver
: 3)为扩展配置构造自定义bean,并将其传递到
JtwigViewResolver
:
<bean id="jtwigDec" class="...ExtendedEnvironmentConfiguration"/>
<bean id="jtwigRenderer" class="org.jtwig.web.servlet.JtwigRenderer">
<constructor-arg ref="jtwigDec"/>
</bean>
<bean class="org.jtwig.spring.JtwigViewResolver">
...
<property name="renderer" ref="jtwigRenderer"/>
</bean>
i had a few problems to understand the first answer so i changed it a little... 我在理解第一个答案时遇到了一些问题,因此我对其进行了一些更改...
final SimpleJtwigFunction myFunction = new SimpleJtwigFunction() {
@Override
public String name() {
return "translate";
}
@Override
public Object execute(FunctionRequest request) {
String value1 = "a Problem";
if (request.getNumberOfArguments() == 1) {
if (request.get(0) instanceof String) {
value1 = request.getEnvironment().getValueEnvironment().getStringConverter().convert(request.get(0));
}
}
return ("This is: "+value1);
}
};
final EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder
.configuration()
.functions()
.add(myFunction)
.and()
.build();
String templatenamex = defaultpath + templatename;
JtwigTemplate template = JtwigTemplate.classpathTemplate(templatename,configuration);
JtwigModel model = JtwigModel.newModel();
for (String key : map.keySet()) {
model.with(key, map.get(key));// .replace("\"", "\\\""));
}
String resultx = template.render(model);
and in template 并在模板中
{{ translate("go") }} {{translation(“ go”)}}
creates: 创建:
this is: go 这是:去
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.