3

I'm using the Pebble template engine in a Java application that uses the Spring framework.

I'm looking for a solution to get the current date in Pebble. Before to start with implementing this basic function, I'm wondering if the library already provides this function. I have to say that the official doc are not saying anything for this.

mat_boy
  • 12,998
  • 22
  • 72
  • 116

1 Answers1

1

If you need the date in all the templates (e.g. for usage in a footer), you could add a global variable:

Extension

public class DateNowExtension extends AbstractExtension {
    @Override
    public Map<String, Object> getGlobalVariables() {
        return Collections.singletonMap("now", new Date());
    }
}

Configuration

@Configuration
public class PebbleExtensionConfiguration {
    @Bean
    public Extension dateNowExtension() {
        return new DateNowExtension();
    }
}

Template

&copy; ACME {{ now | date("yyyy") }}
maechler
  • 1,257
  • 13
  • 18