Using Pebble template engine with Spring Boot


Using Pebble template engine with Spring Boot



So I'm trying to integrate Pebble templating engine to my spring boot application but I'm confused on how to implement this.



So I've read through their site on how to implement it however it says for Spring MVC which I think not similar with Spring Boot.



I also go to their github page then trying to add their maven dependency to POM.xml but I don't know if I will configure something or it is the same as Thymeleaf or Mustache that are autoconfigured.



Link to their site: http://mitchellbosecke.com/pebble/





They provide an example on their github that you can try, to get an idea of how it works: github.com/PebbleTemplates/pebble-example-spring
– Dovmo
May 14 at 3:17




1 Answer
1



Add the starter dependency to your pom.xml:


<dependency>
<groupId>io.pebbletemplates</groupId>
<artifactId>pebble-spring-boot-starter</artifactId>
<version>2.6.2</version>
</dependency>



OR


<dependency>
<groupId>io.pebbletemplates</groupId>
<artifactId>pebble-spring-boot-2-starter</artifactId>
<version>2.6.2</version>
</dependency>



This is enough for autoconfiguration to kick in. This includes:


.pebble


/templates/


text/html


UTF-8



PLEASE NOTE: the starter depends on spring-boot-starter-web but is marked as optional, you'll need to add the dependency yourself or configure Spring MVC appropiately.


spring-boot-starter-web



A number of properties can be defined in Spring Boot externalized configuration, eg. application.properties, starting with the prefix pebble. See the corresponding PebbleProperties.java for your starter version. Notable properties are:


application.properties


pebble


pebble.prefix


/templates/


pebble.suffix


.pebble


pebble.cache


true


pebble.contentType


text/html


pebble.encoding


UTF-8


pebble.exposeRequestAttributes


false


pebble.exposeSessionAttributes


false


pebble.defaultLocale


null


pebble.strictVariables


false



Extensions defined as beans will be picked up and added to the PebbleEngine automatically:


@Bean
public Extension myPebbleExtension1() {
return new MyPebbleExtension1();
}

@Bean
public Extension myPebbleExtension2() {
return new MyPebbleExtension2();
}



CAVEAT: Spring will not gather all the beans if they're scattered across multiple @Configuration classes. If you use this mechanism, bundle all Extension @Beans in a single @Configuration class.



The autoconfigurer looks for a bean named pebbleLoader in the context. You can define a custom loader with that name and it will be used to configure the default PebbleEngine:


pebbleLoader


@Bean
public Loader<?> pebbleLoader() {
return new MyCustomLoader();
}



PLEASE NOTE: this loader's prefix and suffix will be both overwritten when the ViewResolver is configured. You should use the externalized configuration for changing these properties.



Likewise, you can build a custom engine and make it the default by using the bean name pebbleEngine:


pebbleEngine


@Bean
public PebbleEngine pebbleEngine() {
return new PebbleEngine.Builder().build();
}



And the same goes for the ViewResolver, using the bean name pebbleViewResolver:


pebbleViewResolver


@Bean
public PebbleViewResolver pebbleViewResolver() {
return new PebbleViewResolver();
}



PLEASE NOTE: you need to change the Loader's prefix and suffix to match the custom ViewResolver's values.



The main role of this starter is to configure Pebble for generating MVC View results (the typical HTML). You may define more PebbleEngine/Loader beans for other usage patterns (like generating email bodies). Bear in mind that you should not reuse the default Loader for other Engine instances.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

What's the difference between DDS and SOME/IP?