How to format output with `stampToDate` and then define a variable with `with` tag for reuse?

Calendar 👁️ 70

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:

  1. Display the formatted date below the article title.
  2. Display the formatted date again in the "Latest Articles" list on the article sidebar.
  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, "格式")}},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

Related articles

Can the `stampToDate` label handle the millisecond precision of Unix timestamps?

In a content management system, the accurate display of time is crucial, especially when dealing with user behavior, data logs, or the release of specific events.AnQiCMS as a powerful content management system based on the Go programming language provides rich template tags to meet various content display needs, among which the `stampToDate` tag is a powerful tool for formatting timestamps.However, whether this tag can handle the millisecond level precision of Unix timestamps is a concern for many operators and developers.We can see from the official documentation of AnQiCMS

2025-11-07

How can the scheduled publishing function of AnQi CMS accurately display the publish time on the front end through `stampToDate`?

As an experienced website operation expert, I am well aware of the art of content release timing and its profound impact on user experience, search engine optimization, and even operation efficiency.AnQiCMS (AnQi CMS) is exactly born to meet these needs, and its 'Time Factor-Scheduled Publication Function' is a powerful tool in the hands of content operators.But having a strong publishing capability is not enough, how to present these carefully set publishing times elegantly and accurately to the readers is equally important.This is the place where the `stampToDate` template tag really shines.### Schedule Post Function

2025-11-07

Can `stampToDate` format timestamps into ISO 8601 standard format (such as "YYYY-MM-DDTHH:MM:SSZ")?

As an experienced website operation expert who deeply understands the operation of AnQiCMS, I fully understand the importance of time formatting, especially standardized time formats, in content management.Today, let's delve into the `stampToDate` tag of Anqi CMS to see if it can format timestamps into ISO 8601 standard format, such as the `"YYYY-MM-DDTHH:MM:SSZ"` we often see.### AnQi CMS's `stampToDate`: Easily format timestamps to ISO

2025-11-07

Are there any special considerations when using the `stampToDate` tag in the Anqi CMS multi-site environment?

As an experienced website operation expert, I have accumulated rich experience in the practical application of AnQiCMS, especially feeling deeply about the flexible use of template tags.Today, let's delve deeply into the commonly used `stampToDate` tag, and when it is called in the multi-site environment of AnQiCMS, are there any points that need special attention?AnQiCMS is an efficient and customizable enterprise-level content management system, and its multi-site management function is one of its core advantages.It allows users to manage multiple independent sites on a unified backend

2025-11-07

How to ensure that the time zone is displayed as local time instead of UTC time when the `stampToDate` label formats time?

AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, whose powerful template engine enables flexible and diverse content display.In daily content operations, the formatted display of time is a common need, and the `stampToDate` tag is exactly for this purpose.It can convert Unix timestamps into the familiar date and time format.However, many operators may encounter a confusion: why does the time formatted with `stampToDate` sometimes display as UTC time

2025-11-07

Is there an easy way to check if the input timestamp of `stampToDate` is valid in the AnQi CMS template?

As an experienced website operation expert, I am well aware that in a content management system like AnQiCMS, the accuracy and display format of timestamps are crucial for user experience and data presentation.`stampToDate` this powerful label function is undoubtedly a good helper for us to process time information in the template.However, like any tool, it also requires us to use and understand its input requirements correctly.Today, let's delve deeply into a problem that everyone may encounter in the development of Anqi CMS templates: **'In the Anqi CMS template

2025-11-07

How to use `stampToDate` to format the creation time of associated documents in `tagDataList`?

As an experienced website operations expert, I am well aware of the importance of flexible data display and user experience.AnQi CMS with its powerful customization capabilities provides many conveniences for content operations.Today, let's delve deeply into a very practical skill in daily content presentation: how to elegantly format the creation time of associated documents in `tagDataList`. ### Timestamp in AnQi CMS template: Why is formatting necessary?In Anqi CMS, whether it is articles, products, or other content models

2025-11-07

How to concatenate the formatted date of the `stampToDate` tag with other text content to form a complete display message?

As an experienced website operation expert, I fully understand that how to flexibly display information is crucial for improving user experience and meeting operation requirements.AnQiCMS (AnQiCMS) is an efficient and easy-to-use content management system that provides powerful template tag functions, among which the `stampToDate` tag is a tool to process timestamps and format them into readable date strings.

2025-11-07