spring boot integrated Pebble

What is

Pebble is a Java templating engine inspired by Twig. It features templates inheritance and easy-to-read syntax, ships with built-in autoescaping for security, and includes integrated support for internationalization.

Pebble is a Java inspired by Twig template engine. It has easy to read and template inheritance syntax, built-in security autoescaping, and includes integrated support for internationalization.

twig is a very popular php template engine, is widely used many php framework. The syntax can also be seen in python django template engines.

rely

<dependency>
	<groupId>io.pebbletemplates</groupId>
   <artifactId>pebble-spring-boot-starter</artifactId>
	<version>3.0.5</version>
</dependency>
复制代码

use

 @RequestMapping("/hello")
    public String hello() {
        PebbleEngine engine = new PebbleEngine.Builder().build();
        PebbleTemplate compiledTemplate = engine.getTemplate("templates/home.html");
        Writer writer = new StringWriter();

        Map<String, Object> context = new HashMap<>();
        context.put("websiteTitle", "My First Website");
        context.put("content", "My Interesting Content");

        try {
            compiledTemplate.evaluate(writer, context);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String output = writer.toString();
        return output;
    }
复制代码

Adding Template Files

<!DOCTYPE html>
<h1>{{content}}</h1>
</html>
复制代码

As can be seen no longer Thymeleafas output variables to be written into the tag attributes inside.

Static resources

mvc:
    static-path-pattern: /static/**
复制代码

Guess you like

Origin juejin.im/post/5e1ed2d16fb9a02fe654ad95