简体   繁体   English

在 Pebble 中获取当前日期

[英]Obtain the current date in Pebble

I'm using the Pebble template engine in a Java application that uses the Spring framework.我在使用 Spring 框架的 Java 应用程序中使用 Pebble 模板引擎。

I'm looking for a solution to get the current date in Pebble.我正在寻找一种解决方案来获取 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 (eg 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") }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM