1

I'm using Pebble template engine with Spring Boot 2, and everything works fine until I start using inheritance. The browser shows an empty page, no content is returned at all. And unfortunately the server and Catalina (I'm using Tomcat 8.5) logs show no errors.

In my pom I have the following dependencies:

  • Spring Boot 2.0.3 (as parent)
  • pebble-spring-boot-2-starter (tried 2.6.2 and 3.0.0.BETA01)
  • spring-boot-starter-data-jpa
  • spring-boot-starter-web
  • spring-boot-starter-security

I have the following application.properties

  • pebble.suffix = .html.peb
  • pebble.cache = false (also tried true)

My parent template (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>

My child template (resources/templates/child.html.peb)

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

When I remove the inheritance Pebble is working fine, and is including and showing the model, so Pebble does work.

BigJ
  • 1,990
  • 2
  • 29
  • 47
  • {% extends "base.html.peb" %} not {% extends "base_test.html.peb" %} – Pavel Molchanov Jul 02 '18 at 20:18
  • Thanks Pavel. Unfortunately that was a typo in my post. The parent filename is now corrected in the post – BigJ Jul 02 '18 at 20:26
  • Can you add a line how do you run the Template engine, something like: PebbleTemplate compiledTemplate = engine.getTemplate("templates/home.html"); ? The problem is probably with the base template path resolving. – Pavel Molchanov Jul 02 '18 at 20:29
  • The controller just returns a String: the name of the template, so "child". I didn't see any other configuration needed for Pebble with Spring Boot. – BigJ Jul 02 '18 at 20:37
  • 1
    Try: {% extends "base" %} Seems like resolver adds .html.peb automatically – Pavel Molchanov Jul 02 '18 at 20:53
  • Thanks, that works. The official docs threw me of because the example says "{% extends "parent.html" %}". (http://www.mitchellbosecke.com/pebble/documentation/guide/basic-usage) – BigJ Jul 02 '18 at 21:58
  • Let me re-do the comment as an answer to get some points ;) – Pavel Molchanov Jul 02 '18 at 22:23

1 Answers1

1

Pebble Spring Boot Starter resolves the template path by concatenation of the prefix, template name, and suffix:

public class PebbleTemplateAvailabilityProvider implements 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;
    }
}

}

If the template is specified with the suffix in the 'extends' directive, the suffix will be appended one more time and the template won't be found, something like:

resources/templates/base.html.peb.html.peb

To solve the issue, pebble template name has to be specified without prefix in the 'extends' directive:

{% extends "base" %}

For me, this is a bug. Pebble Spring Boot Starter should be able to detect that base template is specified with or without the suffix.

Pavel Molchanov
  • 2,299
  • 1
  • 19
  • 24
  • Thanks. On a side note: no errors are thrown or shown which makes it very difficult to debug. Do you know if that is just how Pebble works, or can I enable some error output? – BigJ Jul 04 '18 at 17:33
  • You can raise an issue at the https://github.com/robshep/pebble-spring-boot-2-starter/issues and the project owner will need to check and apply a fix. Or, if you are a GitHub registered user, you can fork the code, apply the fix and make a pull request to the owner to include it in the project. It's not difficult. – Pavel Molchanov Jul 04 '18 at 18:06
  • The link to the file at the GitHub: https://github.com/robshep/pebble-spring-boot-2-starter/blob/master/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleTemplateAvailabilityProvider.java – Pavel Molchanov Jul 04 '18 at 18:06
  • By the way, I used Twig while working on the community project on PHP/Symfony. I liked the syntax a lot and it worked for me very well. – Pavel Molchanov Jul 04 '18 at 20:33