As an experienced website operation expert, I am fully aware of the importance of content timeliness and display methods in user experience and SEO optimization in my daily work.AnQiCMS provides great convenience for us with its flexible template engine and rich tag system.tag-stampToDate.mdIn the example,publishStampHow is a variable defined, and can it be used in practice?
UnveilingpublishStampThe definition and true origin of a variable
When we first seetag-stampToDate.mdExample code in the document{% set publishStamp = 1609470335 %}At that time, we may have doubts: thispublishStampIs the variable a system built-in? What does it represent?
In fact, in this specific example,publishStampis not a globally predefined variable of a secure CMS system, but a one within the templateTemporary assignment. {% set ... %}It is a tag used in the AnQi CMS template engine (similar to Django syntax based on GoLang) to declare and assign variables at runtime. This means,publishStampThis is just a placeholder used for demonstrationstampToDateThe usage of tags. It is directly assigned a specific 10-digit integer value, namely the Unix timestamp1609470335.
Where do we actually get and format the timestamp from in the operation of an actual security CMS website?They mainly come from your website content, such as the publication time, update time, etc.Unix timestamp.
For example, intag-archiveDetail.mdWe can clearly see in the document:
- Document addition time
CreatedTimeThis is a timestamp, and it needs to be formatted when called in the template:{{stampToDate(item.CreatedTime, "2006-01-02")}}. - Document update time
UpdatedTimeThe same as a timestamp, its processing method isCreatedTimesimilar.
When you are editing an article in the background, the "Publish Time" field (such as2006-01-02 15:04:05This human-readable format) is converted into a 10-digit Unix timestamp after being saved to the database. This isarchive.CreatedTimeandarchive.UpdatedTimeVariables can be used directly in the template.stampToDateThe reason for using tag parameters.
Therefore, in summary, the variable in the example ispublishStampa 'hypothetical' variable used to help us understandstampToDatethe working principle; while in practical applications, we will use the real timestamp (such asarchive.CreatedTime) to replace it.
stampToDate: A flexible tool for timestamp conversion
Since we know the source of the timestamp, the next step is to understand how to convert these original 10-digit numeric timestamps into user-friendly date formats, which isstampToDatethe responsibility of tags.
stampToDateThe usage is very intuitive:{{stampToDate(时间戳, "格式")}}It takes two parameters:
- TimestampMust be a 10-digit integer representing the number of seconds since the Unix epoch (January 1, 1970, 00:00:00 UTC).
- FormatThis is a string that defines how you want the date and time to be displayed.It is especially important to note that the date format of Anqi CMS follows the specific format specification of the Go language.
2006-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(Depends on the specific time of the timestamp)"2006/01/02 15:04"Will be formatted as2021/06/30 12:30
This formatting flexibility allows us to dynamically adjust the way dates are displayed according to the style of the website, the reading habits of users in different regions, and the needs of multilingual sites, without modifying the underlying data.
The actual application in AnQiCMS template
UnderstoodpublishStampBackground andstampToDateBy using this, we can fully utilize it in actual security CMS templates. The most common application scenario is to display the publication or update time of the content on article lists or detail pages.
Assuming we are developing a template for an article detail page, we need to display the title, content, and publish date of the article.archiveDetailTags can help us obtain detailed information about the current article, includingCreatedTimefield.
The following is a specific template code snippet that demonstrates how to retrieve 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:
archiveis composed ofarchiveDetailLabel (or the current article object provided in thearchiveListloop) of the list page.archive.CreatedTimeDirectly provides the timestamp of the article creation.stampToDateThe label receives this timestamp and uses2006-01-02 15:04As a format string to be converted.
In this way, regardless of how the publication time of the background articles is stored, the front-end can present it to the visitors in a unified and clear format, greatly enhancing the readability and professionalism of the content.
Summary
In the Anqi CMSpublishStampExample 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 practical content operation, we usually obtain content variables (such asarchive.CreatedTimeorarchive.UpdatedTimeRetrieve these timestamps from within, and use the flexible date format strings in the Go language to meet various time display requirements.Mastering this skill not only makes your website content more timely, but also effectively improves the user experience.
Common Questions (FAQ)
1.stampToDateWhat date formats are supported by the tag? How do I find the format I need?
stampToDateLabel supports all time formats in Go language. The key to understanding the time format in Go language is to remember a special reference date:2006-01-02 15:04:05.999999999 -0700 MST.You need to use the elements of this reference date to build the format you want."2006年01月02日 星期一"The official Go language documentation provides detailed formatting examples, or you can also test them with an online Go date formatting tool.
2. I can directly use it in the templatearchive.CreatedTimeDo you show 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 the Unix timestamp, which is not convenient for user reading.stampToDateThe function of the label is to take this original timestamp numberParse and formatAre commonly used in our daily life date and time strings, making them more readable. Therefore,