When operating a website, the publication time of articles is often one of the important pieces of information that users pay attention to.A clear and readable date format not only enhances user experience but also helps improve the professionalism of website content.AnQi CMS is an efficient and customizable content management system that provides a flexible way to control the display of various information in templates, including the publication time of articles.
The AnQi CMS template system uses a concise syntax similar to Django, variables are usually referenced through double curly braces{{变量}}and logical control uses{% 标签 %}such a structure. The publication time of the article is usually stored in the system as a timestamp (a numerical sequence representing time).This means we need a method to convert this original timestamp into the date format we are accustomed to reading, such as 'June 1, 2023'.
To achieve this, AnQi CMS provides a very practical built-in tag:stampToDate.
MasterstampToDateTag
stampToDateTags are used to format timestamps into readable date and time strings. Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}
The key is the second parameter - "format". Anqi CMS'sstampToDatetags follow the Go language time formatting rules. Used by many systemsYYYY-MM-DDThis placeholder is different, the time formatting in Go language is based on a specific reference point:2006年01月02日 15点04分05秒 -0700 MST. Just replace the year, month, day, hour, minute, and second in this reference time with the display style you want.
For example, if you want to display "June 1, 2023
How to get the article publication time?
In the Anqi CMS template, you can get the original publication time (timestamp) of the article in the following way:
- Article detail page:directly
archive.CreatedTime. Here,archiveIt is usually the data object of the current article detail page. - Article list page:In
{% for item in archives %}Used in loop:item.CreatedTime.
These fields will return a 10-digit timestamp.
Practical formatting example
Now, we combinestampToDateTags and Go language formatting rules to implement various date display effects:
Displayed as "June 1, 2023":
{{stampToDate(archive.CreatedTime, "2006年01月02日")}}here,
2006represents the year,01represents the month,02Representing a date, we connect them with Chinese characters “year”, “month”, and “day”.Displayed as “2023-06-01”:
{{stampToDate(archive.CreatedTime, "2006-01-02")}}Display as “2023/06/01 15:30”:
{{stampToDate(archive.CreatedTime, "2006/01/02 15:04")}}15represents the hour (24-hour format),04Represents minutes.Only display date “June 01”:
{{stampToDate(archive.CreatedTime, "01月02日")}}Displayed as "June 1, 2023" (common English format):
{{stampToDate(archive.CreatedTime, "Jan _2, 2006")}}Note:
JanAbbreviation for the month,_2Represents the date (with a space before if it's a single digit).
Integrate into the actual template.
Let's see how these formatting methods are applied in actual template code.
In the article detail page template (detail.html) :
<article class="article-detail">
<h1>{{archive.Title}}</h1>
<div class="article-meta">
<span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</span>
<span>浏览量:{{archive.Views}}</span>
<span>分类:<a href="{{archive.Category.Link}}">{{archive.Category.Title}}</a></span>
</div>
<div class="article-content">
{{archive.Content|safe}}
</div>
</article>
In the article list page template (list.html) :
<ul class="article-list">
{% archiveList articles with type="page" limit="10" %}
{% for item in articles %}
<li>
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p class="summary">{{item.Description}}</p>
<div class="meta">
<span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
<span>阅读量:{{item.Views}}</span>
</div>
</li>
{% empty %}
<li>暂无文章内容。</li>
{% endfor %}
{% endarchiveList %}
</ul>
{% pagination pages with show="5" %}
{# 这里是分页导航的代码 #}
{% endpagination %}
BystampToDateLabel, you can flexibly control the display of article publication time so that it completely conforms to the design style of your website and meets the needs of your users.This design philosophy of AnQi CMS makes content presentation both powerful and easy to use.
Frequently Asked Questions (FAQ)
1. Why does AnQi CMS use this string for date formatting instead of the common2006-01-02 15:04:05one?YYYY-MM-DD?
This is because AnQi CMS is developed based on Go language, and Go language uses a special reference time point for time formatting:Mon Jan 2 15:04:05 MST 2006. You need to replace the reference value of the time element you want to display (for example, 2006 represents the year, 01 represents the month, 02 represents the date) with the format you want.For example, to display 'Year-Month-Day', you would write"2006-01-02". Although it looks a bit unique, once you master the pattern, you will find it very flexible.
2. In addition to the article's publish time (CreatedTime), other time fields can also be usedstampToDateCan I format it?
Of course you can. As long as it is a 10-digit timestamp field obtained from the Anqi CMS template, such as the article's update timearchive.UpdatedTimeoritem.UpdatedTimeOr other custom fields that store timestamps can be processed throughstampToDateTags should be formatted in the same way. You just need to replace the corresponding variables.
3. Can I display different formats of publish time in the same article or list item?
Absolutely.stampToDateThe label can be used multiple times, and each time it can specify a different format. For example, you may want to display a concise date {{stampToDate(archive.CreatedTime, "2006年01月02日")}}and `{{stampToDate(archive.CreatedTime, “