mat_boy
mat_boy

Reputation: 13666

Obtain the current date in Pebble

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.

Upvotes: 3

Views: 998

Answers (1)

maechler
maechler

Reputation: 1367

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") }}

Upvotes: 1

Related Questions