In the daily operation of AnQi CMS, we know that the details of content presentation often determine the quality of user experience.A high-quality article is not only engaging in content, but also crucial in terms of layout and information display professionalism.Among them, the unified formatting of the article publishing time is a small detail that can greatly enhance the professionalism of the website.Today, let's delve into how to巧妙运用in Anqi CMSprevArchiveandnextArchiveLabel the document time obtained and throughstampToDateLabel, unify its format to highlight your website's excellence in detail.
Unified time format: the key to improving user experience and website professionalism.
When browsing a website, users often want to quickly understand the publication or update time of the article.When the display format of the time for the "Previous Article" and "Next Article" is inconsistent, such as one showing "2023-10-26", another showing "October 26, 2023", or even a string of hard-to-understand numbers (timestamp), it will undoubtedly affect the reading experience and reduce the professionalism of the website.For search engine optimization (SEO), consistent and clear date information also helps crawlers better understand the freshness of the content.
The AnQi CMS template system has provided us with great flexibility, especially throughprevArchiveandnextArchiveThese tags allow us to easily get the previous and next document information of the current document. Each one returns a complete document object, which includesCreatedTime(creation time) andUpdatedTime(Updated time) These are two key time fields. These time fields are usually stored in the form of Unix timestamps, which is a string representing the number of seconds from 00:00:00 UTC on January 1, 1970 to the present.To make these timestamps readable and uniform,stampToDateThe tag is our powerful tool.
Deep understandingstampToDate: Time formatting wizard
stampToDateThe tag plays an important role in the AnQi CMS template system in converting the original timestamp into the familiar date and time format. Its usage is concise and clear, usually requiring only two parameters:
- Timestamp (Timestamp)This is the original Unix timestamp you want to convert, which is usually a 10-digit number.
- Format String (Format String)This is a string that defines the output date and time format.
It is especially important to note that the Anqi CMS'sstampToDateThe format string used by the tag follows the specific time formatting standard of the Go language, not the common one we usually useY-m-d H:i:sThis is a PHP or Java style. Go language uses a fixed reference time"2006-01-02 15:04:05"Use it as a formatting template. You can combine this reference time to create any date and time format you want.
For example:
- If you want to display it in the format 'Year-Month-Day', you can write it as
"2006-01-02". - If you want to display
"2006/01/02 15:04". - You need the complete format of
"2006-01-02 15:04:05". - Even complex formats like '02 Jan 2006 (Mon)' can be combined
"02 Jan 2006 (Mon)"to achieve.
This seems special2006-01-02 15:04:05This is an accurate time point in Go language, down to milliseconds, where each number or character represents a different part of time, for example2006Represents the year,01Represents the month,02represents the day,15represents the hour (24-hour format),04represents the minute,05Represents seconds. Master this reference point, and you'll be able to format time at will.
Practical exercise: Steps to unify the time format in front and back documents.
Now, let's put theory into practice and see how to unify in your Anqi CMS template.prevArchiveandnextArchiveThe document time format. Assuming we want to format the document creation time as "YYYY-MM-DD".
First, in your article detail page template (usually{模型table}/detail.htmlIn this section, you will find the part for displaying links to the previous and next documents. We obtain the creation timeCreatedTimeFor example:
{# 获取上一篇文档信息 #}
{% prevArchive prev %}
<div class="prev-archive">
{% if prev %}
<a href="{{ prev.Link }}">
<span class="label">上一篇:</span>
<span class="title">{{ prev.Title }}</span>
{# 将时间戳 prev.CreatedTime 格式化为“YYYY年MM月DD日” #}
<span class="date">发布时间:{{ stampToDate(prev.CreatedTime, "2006年01月02日") }}</span>
</a>
{% else %}
<span class="no-archive">没有上一篇了</span>
{% endif %}
</div>
{% endprevArchive %}
{# 获取下一篇文档信息 #}
{% nextArchive next %}
<div class="next-archive">
{% if next %}
<a href="{{ next.Link }}">
<span class="label">下一篇:</span>
<span class="title">{{ next.Title }}</span>
{# 将时间戳 next.CreatedTime 格式化为“YYYY年MM月DD日” #}
<span class="date">发布时间:{{ stampToDate(next.CreatedTime, "2006年01月02日") }}</span>
</a>
{% else %}
<span class="no-archive">没有下一篇了</span>
{% endif %}
</div>
{% endnextArchive %}
In this code, we first use{% prevArchive prev %}and{% nextArchive next %}We obtained the objects of the previous and next documents and named themprevandnext. To avoid errors when the document does not exist, we added{% if prev %}and{% if next %}judgment.
The core formatting steps are:{{ stampToDate(prev.CreatedTime, "2006年01月02日") }}
here,prev.CreatedTime(ornext.CreatedTime) is the original Unix timestamp we get from the document object."2006年01月02日"It is the format string defined in the Go language style, which tellsstampToDatehow the function converts a timestamp into the Chinese date format we want.
If you also need to display the update timeUpdatedTimeThe operation method is also the same:
{# 假设在同一段落中,同时显示创建时间和更新时间 #}
<span class="date">发布时间:{{ stampToDate(prev.CreatedTime, "2006年01月02日") }}</span>
<span class="date">更新时间:{{ stampToDate(prev.UpdatedTime, "2006-01-02 15:04") }}</span>
Through this method, regardless ofprevArchiveandnextArchiveWhat is the timestamp of the document obtained, they will all be presented in the format you define, greatly enhancing the professionalism and user experience of the website content.
Summary
The template tag design of AnQi CMS aims to provide operators with strong and flexible control capabilities. ThroughprevArchive/nextArchiveobtaining information about the front and back documents, and combiningstampToDatePerform fine-grained time format control, you can not only ensure the consistency and beauty of the website date display, but also bring a smoother reading experience to users.This is the embodiment of the concept of Anqicms helping small and medium-sized enterprises and content operation teams to improve efficiency, reduce costs, and present content efficiently through technical means.
Frequently Asked Questions (FAQ)
Q1:stampToDateWhat will happen if the timestamp is not 10 digits (Unix timestamp)?A1:stampToDateThe label expects to receive a standard Unix timestamp (usually 10 digits representing seconds). If the input is not a valid Unix timestamp or the format is not