Dear partners operating the Anqi CMS, hello!As an expert who deeply understands the integration of content operation and technology, I fully realize the value of an efficient and flexible content management system for our daily work.Secure CMS, with its high-performance architecture in Go language and Django-style template engine, undoubtedly provides us with powerful content display capabilities.stampToDateFormat the output time cleverlywithDefine variables with tags to achieve more elegant and efficient reuse in templates.

The charm of Anqi CMS template language: driving content presentation efficiently

The template system of AnQi CMS has won the favor of many users with its concise and powerful Django template syntax.It is not only simple to deploy and fast to execute, but also provides great convenience to us in terms of content model, multi-language support, and SEO optimization.In daily content publishing and page design, we often need to handle dynamic data, especially dates and times.Converting the original timestamp to a user-friendly date format is a key element in enhancing user experience.

stampToDateThe bridge from timestamp to readable date

In the templates of AnQi CMS,stampToDateTags are a very practical tool, they can format the Unix timestamp stored in the database (usually a 10-digit number) into the date and time string we need. Its basic usage is:

{{stampToDate(时间戳, "格式")}}

Here the “timestamp” is usually a content object (such as an articlearchive, commentscommentetc.),CreatedTimeorUpdatedTimeThe “format” follows the unique time formatting standard of the Go language, for example:

  • "2006-01-02"representing年-月-日for example2023-10-26
  • "2006年01月02日"representing年 月 日for example2023年10月26日
  • "15:04"representing时:分for example14:35
  • "2006-01-02 15:04:05"representing年-月-日 时:分:秒for example2023-10-26 14:35:00

For example, we may display the publication time on the article detail page like this:

<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 of

However, in some scenarios, we may not just simply display the formatted date, but also hope to use the formatted result as an independent variable, repeatedly used in multiple positions of the template, or passed to other template fragments (such as throughinclude标签引入的子模板(English)使用。

例如,我们可能需要(English):

  1. 在文章标题下方显示格式化日期(English)。
  2. In the "Latest Articles" list on the article sidebar, display the formatted date again.
  3. Pass this date as a parameter to a component specifically designed to render article metadata.

If it is called every time{{stampToDate(archive.CreatedTime, "格式")}}The code may appear redundant and may affect the efficiency of template parsing, 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.

Core solution: IntroducesetLabel is assigned temporarily

The template engine provided by Anqi CMS providessetA label that allows us to define a variable and assign a value within the current scope of the template. This is the key to solving the aforementioned challenge! We can first useseta label to receivestampToDateThe formatted result, convert it to a regular string variable.

Look at a specific example. Suppose we are designing the template for the article detail page and 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:InsidebarIf we use it directly externallysetDefinedformattedDate,Then all the 'latest news' dates will show the publication date of the current article, which is clearly wrong. The correct way is to format each one individually within the loop.itemofCreatedTime.stampToDate.

This once again highlights the significance of storing output as a variable.stampToDateIt creates an *exact* string that can be used freely within its defined scope.Scope.Be used freely.

Let us re-examine and optimize this example, focusing on how to variable the formatted date variable of *this 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>

Passset formattedPublishDate = ...We successfully convertedstampToDateThe output is captured into a variableformattedPublishDateand reused at different positions in the current template.

withTag: Defines a set of variables within the scope, achieving elegant reuse

AlthoughsetTag solves the capture problemstampToDateOutput the question, but when we need to define a set of related variables in a specific code block or templateincludedefined in the sub-templatea group ofrelated variables when, `with`