首页 > 解决方案 > Pebble 模板继承失败且没有错误

问题描述

我在 Spring Boot 2 中使用 Pebble 模板引擎,在我开始使用继承之前一切正常。浏览器显示一个空白页面,根本没有返回任何内容。不幸的是,服务器和 Catalina(我使用的是 Tomcat 8.5)日志显示没有错误。

在我的 pom 中,我有以下依赖项:

我有以下 application.properties

我的父模板 (resources/templates/base.html.peb)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head></head>
<body>Template test</body>
</html>

我的子模板 (resources/templates/child.html.peb)

{% extends "base.html.peb" %}

当我删除继承时,Pebble 工作正常,并且包含并显示模型,所以 Pebble 确实可以工作。

标签: javaspringspring-bootpebble

解决方案


Pebble Spring Boot Starter 通过连接前缀、模板名称和后缀来解析模板路径:

公共类 PebbleTemplateAvailabilityProvider 实现 TemplateAvailabilityProvider {

@Override
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader,
        ResourceLoader resourceLoader) {
    if (ClassUtils.isPresent("com.mitchellbosecke.pebble.PebbleEngine", classLoader)) {
        String prefix = environment.getProperty("pebble.prefix", PebbleProperties.DEFAULT_PREFIX);
        String suffix = environment.getProperty("pebble.suffix", PebbleProperties.DEFAULT_SUFFIX);
        return resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + prefix + view + suffix).exists();
    } else {
        return false;
    }
}

}

如果模板在 'extends' 指令中指定了后缀,则后缀将被再附加一次,并且找不到模板,例如:

资源/模板/base.html.peb.html.peb

为了解决这个问题,必须在 'extends' 指令中指定不带前缀的 pebble 模板名称:

{% extends "base" %}

对我来说,这是一个错误。Pebble Spring Boot Starter 应该能够检测到指定的基本模板是否带有后缀。


推荐阅读