As an experienced website operation expert, I am well aware of the importance of content timeliness and display methods for user experience and SEO optimization in my daily work.AnQiCMS (AnQiCMS) takes advantage of its flexible template engine and rich tag system, providing us with great convenience.Today, let's delve into a topic that often comes up in template development:tag-stampToDate.mdIn the example,publishStampHow is a variable defined, can it be used in practice?

RevelationpublishStampThe definition of the variable and its real origin

When we first sawtag-stampToDate.mdExample code in the document{% set publishStamp = 1609470335 %}When, one might wonder: thispublishStampIs the variable system built-in? What does it represent?

Actually, in this specific example,publishStampIt is not a globally defined variable of an Anqi CMS system, but a variable inside the templateTemporary assignment.{% set ... %}Is an AnQi CMS template engine (based on Django-like syntax in GoLang) used to declare and assign variables at runtime in the template. This means,publishStampThis is just a placeholder to demonstratestampToDateThe usage of tags. It is assigned a specific 10-digit integer value, that is, the Unix timestamp1609470335.

Where do we usually get and format the timestamp in the actual operation of the Anqi CMS website?They mainly come from your website content, such as the publication time, update time, etc.The AnQi CMS will store these date information asUnix timestamp.

For example, intag-/anqiapi-archive/142.htmlIn the document we can clearly see:

  • Document add timeCreatedTime: This is a timestamp, which needs to be used when calling the template{{stampToDate(item.CreatedTime, "2006-01-02")}}to format.
  • Document Update TimeUpdatedTime: This is also a timestamp, with the same processing method asCreatedTimeSimilar.

When you edit an article in the background, the "publish time" field you fill in (such as2006-01-02 15:04:05This is a human-readable format) After being saved to the database, it will be converted into a 10-digit Unix timestamp by the internal conversion of the safe CMS. That isarchive.CreatedTimeandarchive.UpdatedTimeVariables can be directly used in the template asstampToDatethe reason for using tag parameters.

Therefore, in summary, the variables in thepublishStampexample are 'hypothetical' variables that help us understandstampToDateThe mechanism; in actual application, we will use the real timestamp obtained from the CMS content (such asarchive.CreatedTime) to replace it.

stampToDate: A flexible tool for timestamp conversion

Since we know the source of the timestamp, the next thing we need to understand is how to convert these original 10-digit numeric timestamps into user-friendly date formats, which isstampToDatethe responsibility of the tag.

stampToDateThe usage is very intuitive:{{stampToDate(时间戳, "格式")}}It takes two parameters:

  1. timestampIt must be a 10-digit integer representing the number of seconds since the Unix epoch (January 1, 1970, 00:00:00 UTC).
  2. FormatThis is a string that defines how you want the date and time to be displayed.It should be especially noted that the date format of Anqi CMS follows the specific format specification of the Go language.This means you need to use the predefined reference date in Go language2006-01-02 15:04:05.999999999 -0700 MSTBuild your format string. For example:
    • "2006年01月02日"Will be formatted as2021年06月30日
    • "2006-01-02"Will be formatted as2021-06-30
    • "15:04:05"Will be formatted as12:30:00(depending on the specific timestamp)
    • "2006/01/02 15:04"Will be formatted as2021/06/30 12:30

This formatting flexibility allows us to dynamically adjust the display of the date according to the style of the website, the reading habits of users in different regions, and even the needs of multilingual sites, without modifying the underlying data.

Actual application in AnQiCMS template

UnderstoodpublishStampthe background andstampToDateThe usage, and we can show our skills in the actual AnQi CMS template. The most common application scenarios are displaying the publication or update time of content in article lists or detail pages.

Assuming we are developing a template for an article detail page, we need to display the title, content, and publication date of the article.archiveDetailTags can help us get detailed information about the current article, includingCreatedTimefield.

The following is a specific template code snippet that demonstrates how to obtain the creation time of an article and format it as 'Year-Month-Day Hour:Minute':

<article>
    <h1>{{ archive.Title }}</h1> {# 直接访问 archive 变量获取标题 #}
    <div class="article-meta">
        发布时间:<span>{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}</span>
        浏览量:<span>{{ archive.Views }}</span>
    </div>
    <div class="article-content">
        {{ archive.Content|safe }} {# 内容通常需要用 safe 过滤器以避免HTML转义 #}
    </div>
</article>

In this example:

  1. archiveIt is composed ofarchiveDetailLabel (or the current article object provided in thearchiveListloop).
  2. archive.CreatedTimeDirectly provided the article's creation timestamp.
  3. stampToDateThe label receives this timestamp and uses2006-01-02 15:04Translate the format string.

In this way, regardless of how the publication time of the background articles is stored, the front-end can present it to the visitor in a unified and clear format, greatly enhancing the readability and professionalism of the content.

Summary

In AnQi CMSpublishStampAn example variable, its core lies in guiding us to understandstampToDateThe function of the tag: Converts a 10-digit Unix timestamp to a readable date format. In actual content operations, we usually get content variables (such asarchive.CreatedTimeorarchive.UpdatedTime) Retrieve these timestamps and use the flexible Go language date format string to meet various time display requirements.Mastering this skill not only makes your website content more timely, but also effectively improves the user experience.


Frequently Asked Questions (FAQ)

1.stampToDateWhat date formats does the label support? How can I find the format I need?

stampToDateTags support all time formats in Go language. The key to understanding Go language time formats is to remember a special reference date:2006-01-02 15:04:05.999999999 -0700 MSTYou need to use the elements of this reference date to build the format you want.For example, if you want to display 'October 26, 2023, Thursday', you can try to construct a format string"2006年01月02日 星期一". The official Go language documentation provides detailed formatting examples, or you can also test it with an online Go date formatting tool.

2. I can directly use it in the templatearchive.CreatedTimeDo you want to display the time? Why do you needstampToDate?

If you output directly in the template{{ archive.CreatedTime }}, you will see a 10-digit number (for example1609470335This is the original form of Unix timestamp, not convenient for users to read.stampToDateThe label's role is to convert this original timestamp numberParse and formatMake the date-time string commonly seen in our daily life more readable. Therefore,