简体   繁体   中英

How to invoke custom functions in JTwig?

Is it possible to invoke custom function (say static method) from JTwig template?

For instance, in Thymeleaf framework I can call any static method via full name of its class. So I'm looking for something similar for JTwig.

This is not well documented, but here a snippet in pure 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

2) register it in extended org.jtwig.environment.EnvironmentConfiguration

3) construct custom bean for the extended configuration and pass it into the 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") }}

creates:

this is: go

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM