1

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.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241

3 Answers3

2

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 %}
)
Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
1

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>
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
1

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

  • your answer is appreciated, please try though not to just paste a part of code but to add some more info about it as well, so the future people reading this can easily understand what you are trying to do :) – Sir. Hedgehog Oct 03 '18 at 14:08