Hello everyone! As an expert who deeply understands the integration of content operation and technology, I know the value of an efficient and flexible content management system for our daily work.AnQi CMS, with its high-performance architecture in Go language and Django-style template engine, undoubtedly provides us with powerful content display capabilities.Today, let's delve deeply into a common and practical little trick in template design: how tostampToDateFormat the output time and cleverly utilizewithDefine variables with tags to achieve more elegant and efficient reuse in templates.
The charm of Anqi CMS template language: efficiently drive content presentation
The Anqi CMS template system has won the favor of many users with its concise and powerful Django template syntax.It is not only easy to deploy and fast in execution, but also provides great convenience to us in aspects such as content model, multilingual support, and SEO optimization.In daily content publishing and web design, we often need to handle dynamic data, especially dates and times.Converting the original timestamp into a user-friendly date format is a key element in enhancing user experience.
stampToDateThe bridge from timestamp to readable date
In the AnQi CMS template,stampToDateThe label is a very practical tool that can format the Unix timestamp stored in the database (usually 10 digits) into the date and time string we need. Its basic usage is:
{{stampToDate(时间戳, "格式")}}
The "timestamp" here is usually the content object (such as an articlearchive, commentscommentetc.)'sCreatedTimeorUpdatedTimefield. The "format" follows the unique time formatting standard of the Go language, for example:
"2006-01-02"Indicates年-月-日For example2023-10-26"2006年01月02日"Indicates年 月 日For example2023年10月26日"15:04"Indicates时:分For example14:35"2006-01-02 15:04:05"Indicates年-月-日 时:分:秒For example2023-10-26 14:35:00
For example, we might display the publication time like this on the article detail page:
<p>发布时间:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</p>
This can directly output a formatted date and time, which is very convenient.
Challenge: reuse directlystampToDateoutput
However, in some scenarios, we may not just simply display the formatted date, but also want to use the formatted result as a separate variable, repeated at multiple locations in the template, or passed to other template fragments (such as throughincludeThe template included by the label is used.
For example, we may need:
- Display the formatted date below the article title.
- Display the formatted date again in the "Latest Articles" list on the article sidebar.
- Pass this date as a parameter to a component specifically designed to render article metadata.
If it is called every time{{stampToDate(archive.CreatedTime, "格式")}},Not only does the code appear redundant, it may also affect the efficiency of template parsing, and more importantly, when the format needs to be adjusted, we have to modify multiple places. At this point, we need a way to “capture”stampToDateThe output, and assign it to a reusable variable.
The core of the solution is to introducesetTemporarily assign the tag
Anqi CMS template engine providessetA tag that allows us to define a variable within the current scope of the template and assign a value to it. This is the key to solving the above challenge! We can use it first bysettags to receivestampToDateThe formatting result, convert it to a plain string variable.
Look at a specific example. Suppose we are designing the template for an article detail page, and we need to format the creation time of the article:
{% set articleTimestamp = archive.CreatedTime %}
{% set formattedDate = stampToDate(articleTimestamp, "2006年01月02日 星期一 15:04") %}
<div class="article-header">
<h1>{{ archive.Title }}</h1>
<p class="meta">发布于:{{ formattedDate }}</p>
</div>
<div class="article-content">
{{ archive.Content|safe }}
</div>
<div class="sidebar">
<h3>最新动态</h3>
<ul>
{% archiveList latestArchives with type="list" limit="5" %}
{% for item in latestArchives %}
<li>
<a href="{{ item.Link }}">{{ item.Title }}</a>
<span class="date">{{ formattedDate }}</span> {# 这里复用formattedDate #}
{# 噢,等一下!这里是循环中的最新文章,应该用item.CreatedTime来格式化,上面的formattedDate是当前页面的。#}
{# 那么应该这样: #}
<span class="date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
</li>
{% endfor %}
{% endarchiveList %}
</ul>
</div>
Correct a common misconception in the previous example:InsidebarPart, if we use it directly from outsidesetDefinedformattedDateSo all the dates of the "latest news" will show the publication date of the current article, which is obviously wrong. The correct way is to handle each one separately inside the loopitemofCreatedTimeseparatelystampToDateformatted.
This once again highlights the significance of storing the output as a variable - it creates a *precise* string that can be freely used within its definedstampToDatescopeandis used freely.
Let us re-examine and optimize this example, focusing on how to variableize the formatted date of *the current article* so that it can be reused in multiple places in the current template:
{# 1. 捕获当前文章的创建时间戳 #}
{% set currentArticleTimestamp = archive.CreatedTime %}
{# 2. 使用 stampToDate 格式化时间戳,并将结果赋值给一个新变量 #}
{% set formattedPublishDate = stampToDate(currentArticleTimestamp, "2006年01月02日 星期一 15:04") %}
<div class="article-header">
<h1>{{ archive.Title }}</h1>
{# 3. 在这里使用格式化后的日期变量 #}
<p class="meta">发布于:{{ formattedPublishDate }}</p>
</div>
<div class="article-content">
{{ archive.Content|safe }}
</div>
<div class="article-footer">
{# 4. 假设我们需要在页脚也显示,直接复用变量即可 #}
<span>本文最近更新于:{{ formattedPublishDate }}</span>
</div>
Byset formattedPublishDate = ..., and we successfully transformedstampToDateThe output is captured into a variableformattedPublishDateAnd it is reused at different positions in the current template.
withLabel: Defines a collection of variables within the scope, achieving elegant reuse
AlthoughsetLabel solves the capture issuestampToDateThe output question, but when we need to define variables related toincludein a specific code block or sub-templatea set ofwith