Jtwig Spring Boot

The jtwig-spring-boot-starter project allows one to easily integrate Jtwig with Spring Boot. Just by adding the dependency to your project, spring-boot will then load JtwigViewResolver.

Integrate

Check the most recent version, go to bintray.

Gradle

repositories {
    jcenter()
}
dependencies {
    compile 'org.jtwig:jtwig-spring-boot-starter:5.X'
}

Maven

<repositories>
    <repository>
        <id>bintray</id>
        <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.jtwig</groupId>
        <artifactId>jtwig-spring-boot-starter</artifactId>
        <version>5.X</version>
    </dependency>
</dependencies>

Default Configuration

If you include jtwig-spring-boot-starter in your project, by default it will set the view resolver with prefix classpath:/templates/, suffix will be set as .twig and the Jtwig default configuration will be used. Note that, jtwig-spring-boot-starter uses jtwig-web which extends the default Jtwig Core configuration, as already mentioned.

Costumize Configuration

It is still possible to customize JtwigViewResolver to define prefix, suffix and also Jtwig Environment. For that JtwigViewResolverConfigurer interface can be extended by @Configuration annotated class. Note that, such class needs to be injected by spring-boot to the application context.

@Configuration
public class JtwigConfig implements JtwigViewResolverConfigurer {
    @Override
    public void configure(JtwigViewResolver viewResolver) {
        viewResolver.setRenderer(new JtwigRenderer(EnvironmentConfigurationBuilder
                .configuration()
                .extensions().add(new MyExtension()).and()
                .build()));
    }

    private static class MyExtension implements Extension {
        @Override
        public void configure(EnvironmentConfigurationBuilder configurationBuilder) {
            System.out.println("Hi");
        }
    }
}

The previous example sets the JtwigViewResolver renderer with an extended version of the EnvironmentConfiguration including a dummy Extension. This working example can be found in jtwig-examples.