In the daily operation of AnQi CMS, we know that the details of content presentation often determine the quality of user experience.An excellent article not only has engaging content, but also the professionalism of layout and information presentation is crucial.Among them, the unified formatting of the article release time is a small detail that can greatly enhance the professionalism of the website.prevArchiveandnextArchiveLabel the document time obtained, andstampToDateLabel, unify the format, so that your website can highlight excellence in detail.

Unified time format: the key to improving user experience and website professionalism

The template system of Anqi CMS provides us with great flexibility, especially throughprevArchiveandnextArchiveThese tags allow us to easily retrieve the previous and next document information of the current document. Each of them returns a complete document object, which includesCreatedTime(Creation time) andUpdatedTime(Updated time) These two key time fields.These time fields are usually stored in the form of Unix timestamps, that is, a string representing the number of seconds from 00:00:00 UTC on January 1, 1970 to the present.stampToDateThe label is our powerful tool.

Deep understandingstampToDate: The magician of time formatting

stampToDateTags play an important role in the template system of AnQi CMS in converting raw timestamps into the date and time format we are familiar with. Its usage is simple and straightforward, usually requiring only two parameters:

  1. TimestampThis is the original Unix timestamp you want to convert, which is usually 10 digits.
  2. Format StringThis is a string that defines the output date and time format.

It is especially important to note that, in the AQCMS.stampToDateThe format string used by the label follows the specific time formatting standard of the Go language, rather than the common oneY-m-d H:i:sThis is PHP or Java style. Go language uses a fixed reference time"2006-01-02 15:04:05"auto as the format template. You can combine any desired date and time format through this reference time.

举例来说:

  • If you want to display the format as "year-month-day", you can write it as"2006-01-02".
  • If you want to display “Year/Month/Day Hour:Minute”, it can be written as"2006/01/02 15:04".
  • To get a complete “Year-Month-Day Hour:Minute:Second” format, it is"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:05It is a precise timestamp in milliseconds in Go language, each digit or character representing a different part of the time, for example2006representing a year,01Represents the month, :02Represents the day, :15represents the hour (24-hour clock),04represents the minutes,05Representing seconds. Master this reference point, and you can format time at will.

Practice Session: Steps to unify the time format in front and back documents.

Now, let's put the theory into practice and see how to unify in your security CMS templateprevArchiveandnextArchiveRetrieved document time format. Assume that we want to format the document creation time as "YYYY年MM月DD日".

Firstly, in your article detail page template (usually){模型table}/detail.html)中,You will find the section used to display links to the previous and next documents. We take obtaining the creation time as an exampleCreatedTimeas an 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 %}Retrieved the objects of the previous and next documents and named themprevandnextTo 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(or}next.CreatedTime)is the original Unix timestamp obtained from the document object."2006年01月02日"is the format string defined in the Go language style, which tellsstampToDateHow does the function convert a timestamp to the Chinese date format we want?

If you also need to display the update timeUpdatedTimeThe operation method is also completely the same:

{# 假设在同一段落中,同时显示创建时间和更新时间 #}
<span class="date">发布时间:{{ stampToDate(prev.CreatedTime, "2006年01月02日") }}</span>
<span class="date">更新时间:{{ stampToDate(prev.UpdatedTime, "2006-01-02 15:04") }}</span>

In this way, no matterprevArchiveandnextArchiveWhat 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 powerful and flexible control capabilities. ThroughprevArchive/nextArchiveobtaining information about the preceding and succeeding documents, and combiningstampToDatePerform refined date format control, you can not only ensure the consistency and beauty of the website date display, but also bring a more fluid reading experience to the user.This is the concept embodiment of the idea that AnQi CMS helps small and medium-sized enterprises and content operation teams improve efficiency, reduce costs, and achieve efficient content presentation through technical means.


Common Questions (FAQ)

Q1:stampToDateWhat happens if the timestamp is not a 10-digit number (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 incorrect,