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.
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
© ACME {{ now | date("yyyy") }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With