[英]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. 因此,我试图将Pebble模板引擎集成到我的Spring Boot应用程序中,但是我对如何实现这一点感到困惑。
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. 因此,我已经阅读了他们的网站,了解如何实现它,但是它说的是Spring MVC,我认为它与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. 我也转到他们的github页面,然后尝试将他们的maven依赖项添加到POM.xml中,但是我不知道是否要配置某些东西,或者它与自动配置的Thymeleaf或Mustache相同。
Link to their site: http://mitchellbosecke.com/pebble/ 链接到他们的网站: http : //mitchellbosecke.com/pebble/
Add the starter dependency to your pom.xml: 将启动器依赖项添加到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
from /templates/
dir on the classpath 一个加载器,它将从类路径中的/templates/
dir中选择以.pebble
结尾的模板文件 text/html
in UTF-8
一个将以UTF-8
输出text/html
的ViewResolver 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
但被标记为可选,您需要自己添加依赖项或适当地配置Spring MVC。
A number of properties can be defined in Spring Boot externalized configuration, eg. 可以在Spring Boot外部化配置中定义许多属性,例如。 application.properties
, starting with the prefix pebble
. application.properties
,从前缀pebble
。 See the corresponding PebbleProperties.java for your starter version. 请参阅相应的PebbleProperties.java以获取您的入门版本。 Notable properties are: 值得注意的属性是:
pebble.prefix
: defines the prefix that will be prepended to the mvc view name. pebble.prefix
:定义将在mvc视图名称之前添加的前缀。 Defaults to /templates/
默认为/templates/
pebble.suffix
: defines the suffix that will be appended to the mvc view name. pebble.suffix
:定义将添加到mvc视图名称的后缀。 Defaults to .pebble
默认为.pebble
pebble.cache
: enables or disables PebbleEngine caches. pebble.cache
:启用或禁用PebbleEngine缓存。 Defaults to true
默认为true
pebble.contentType
: defines the content type that will be used to configure the ViewResolver. pebble.contentType
:定义将用于配置ViewResolver的内容类型。 Defaults to text/html
默认为text/html
pebble.encoding
: defines the text encoding that will be used to configure the ViewResolver. pebble.encoding
:定义将用于配置ViewResolver的文本编码。 Defaults to UTF-8
默认为UTF-8
pebble.exposeRequestAttributes
: defines whether all request attributes should be added to the model prior to merging with the template for the ViewResolver. pebble.exposeRequestAttributes
:定义在与ViewResolver的模板合并之前,是否应将所有请求属性添加到模型中。 Defaults to false
默认为false
pebble.exposeSessionAttributes
: defines whether all session attributes should be added to the model prior to merging with the template for the ViewResolver. pebble.exposeSessionAttributes
:定义在与ViewResolver的模板合并之前,是否应将所有会话属性添加到模型中。 Defaults to false
默认为false
pebble.defaultLocale
: defines the default locale that will be used to configure the PebbleEngine. pebble.defaultLocale
:定义将用于配置PebbleEngine的默认语言环境。 Defaults to null
默认为null
pebble.strictVariables
: enable or disable the strict variable checking in the PebbleEngine. pebble.strictVariables
:在pebble.strictVariables
启用或禁用严格变量检查。 Defaults to false
默认为false
Extensions defined as beans will be picked up and added to the PebbleEngine automatically: 定义为bean的扩展将被自动提取并添加到PebbleEngine中:
@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. CAVEAT:如果它们分散在多个@Configuration类中,则Spring不会收集所有的bean。 If you use this mechanism, bundle all Extension @Beans in a single @Configuration class. 如果使用此机制,请将所有Extension @Bean捆绑在一个@Configuration类中。
The autoconfigurer looks for a bean named pebbleLoader
in the context. 自动配置pebbleLoader
在上下文中查找名为pebbleLoader
的bean。 You can define a custom loader with that name and it will be used to configure the default PebbleEngine: 您可以使用该名称定义一个自定义加载器,它将用于配置默认的PebbleEngine:
@Bean
public Loader<?> pebbleLoader() {
return new MyCustomLoader();
}
PLEASE NOTE: this loader's prefix and suffix will be both overwritten when the ViewResolver is configured. 请注意:配置ViewResolver时,此加载程序的前缀和后缀都将被覆盖。 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
: 同样,您可以使用Bean名称pebbleEngine
来构建自定义引擎并将其设置为默认引擎:
@Bean
public PebbleEngine pebbleEngine() {
return new PebbleEngine.Builder().build();
}
And the same goes for the ViewResolver, using the bean name pebbleViewResolver
: 使用豆名称pebbleViewResolver
的ViewResolver也是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. 请注意:您需要更改Loader的前缀和后缀以匹配自定义ViewResolver的值。
The main role of this starter is to configure Pebble for generating MVC View results (the typical HTML). 该入门程序的主要作用是配置Pebble以生成MVC View结果(典型的HTML)。 You may define more PebbleEngine/Loader beans for other usage patterns (like generating email bodies). 您可以为其他使用模式(如生成电子邮件正文)定义更多的PebbleEngine / Loader bean。 Bear in mind that you should not reuse the default Loader for other Engine instances. 请记住,您不应将默认的Loader重用于其他Engine实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.