Jekyll自定义日期 [英] Jekyll custom date

查看:232
本文介绍了Jekyll自定义日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用波纹管代码在Jekyll网站上显示自定义日期

I want use bellow code to display custom date in my Jekyll site

{% assign m = page.date | date: "%-m" %}
{% case m %}
{% when '1' %}Januar
{% when '2' %}Februar
{% when '3' %}März
{% when '4' %}April
{% when '5' %}Mai
{% when '6' %}Juni
{% when '7' %}Juli
{% when '8' %}August
{% when '9' %}September
{% when '10' %}Oktober
{% when '11' %}November
{% when '12' %}Dezember
{% endcase %}

但是我现在不在哪里放置它(我在post.html中尝试过,但是没有用)

But I don't now where to put it (I tried in post.html but does not work)

推荐答案

我已经为此创建了一个模板. 此模板以特定语言翻译日期.这里是法语,但可以随时更改数组. 此模板可用于帖子/页面的枚举(例如,索引页面)或帖子/页面的模板.

I've made a template for this. This template translate a date in a specific language. Here it's french but feel free to change month and day arrays. This template can be used in an enumeration of post/page (eg: the index page) or in a post/page template.

在枚举中使用时,您需要传递日期进行处理

When used in an enumeration, you need to pass the date to process

{% for post in site.posts %}
  <li>
    <span class="post-date">{% include custom_date_full_fr.html date = post.date %}</span>
    <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
  </li>
{% endfor %}

在页面/帖子模板中使用的地方,您只需要包括该模板,因为page.date已经可用.

Where used in a page/post template, you just have to include the template, as the page.date will already be available.

{% include custom_date_full_fr.html %}

custom_date_full_fr.html

{% if include.date %}
    {% assign processed_date = include.date %}
{% else if page.date  %}
    {% assign processed_date = page.date %}
{% endif %}

{% comment %}-------- Test if we have a date to process --------{% endcomment %}
{% if processed_date %}

    {% assign month = "janvier,février,mars,avril,mai,juin,juillet,août,septembre,octobre,novembre,décembre" | split: "," %}

    {% comment %}------ Note : sunday is the first day in this array -------{% endcomment %}
    {% assign day = "dimanche,lundi,mardi,mercredi,jeudi,vendredi,samedi" | split: "," %}

    {% assign month_index = processed_date | date: "%m" | minus: 1 %}

    {%comment%}----------------------------------------------
    Here **minus: 0** is a trick to convert day_index from string to integer and then use it as an array index.
    ----------------------------------------------{%endcomment%}
    {% assign day_index = processed_date | date: "%w" | minus: 0 %}

    {%comment%}-------- Output the date ----------{%endcomment%}
    {{ day[day_index] }} {{ processed_date | date: "%d" }} {{ month[month_index] }} {{ processed_date | date: "%Y" }}
{% endif %}

有关更多信息,请参见此处:

See here for more info :

  • Jekyll Date Formatting Examples by Alan W. Smith
  • Liquid documentation - date filters

这篇关于Jekyll自定义日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆