简体   繁体   English

如何在JTwig中调用自定义函数?

[英]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.

相关问题 Jtwig如何获取HttpSession / HttpServletRequest? - Jtwig How to obtain HttpSession / HttpServletRequest? 如何在Java中创建/调用AWS Lambda函数? - How to create/invoke AWS Lambda functions in Java? 如何在Jbehave中调用TestNG自定义测试侦听器 - How to invoke TestNG Custom Test Listener with in Jbehave Jtwig获取参数显示 - Jtwig get parameters display jtwig:忽略不正确的变量/函数 - jtwig : Ignore incorrect variables / function 龙目岛的@ Builder / @ AllArgsConstructor如何调用自定义构造函数? - How can Lombok's @Builder/@AllArgsConstructor invoke a custom constructor? 如何从自定义 ToolWindow 调用 IntelliJ 重命名重构? - How to invoke IntelliJ rename refactor from custom ToolWindow? 将 JDialog 调用到自定义 JInternalFrame 中 - Invoke JDialog into Custom JInternalFrame 如何在java / graalvm中加载具有多个函数(每个文件同名)的js文件并通过文件名调用函数 - How to load js files with multiple functions (of the same name per file) in java/graalvm and invoke functions by file name 如何从自定义Gson JsonSerializer调用另一个序列化程序? - How to invoke another serializer from a custom Gson JsonSerializer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM