How to use the `add` filter to dynamically add username and timestamp to the `commentList` comments?

Calendar 👁️ 63

AnQiCMS (AnQiCMS) leverages its flexible and powerful template engine, allowing the presentation of website content to have a high degree of freedom.In website operation, the comment section is an important place for user interaction. Clearly displaying the information of the commenters can effectively enhance the reading experience and interaction atmosphere of the comment content.This article will introduce you in detail how to useaddFilter, forcommentListDynamically add user nickname and timestamp in the comments.

Flexible display of comment data: fromcommentListStart

In AnQiCMS, displaying comment content usually usescommentListTemplate tag. This tag helps us easily retrieve the comment list associated with a specific article or product. A basic comment list loop might look like this:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
        <div class="comment-item">
            <p>评论人:{{ item.UserName }}</p>
            <p>评论内容:{{ item.Content }}</p>
            <p>评论时间(原始):{{ item.CreatedTime }}</p>
        </div>
    {% endfor %}
{% endcommentList %}

Here, item.UserNameThe comment provides the nickname of the commenter, anditem.CreatedTimewhich is a timestamp, usually a series of numbers, such as1609470335. To better present to the user, we usually need to convert this timestamp into a readable date and time format.

Get to knowaddFilter: The tool for concatenation and summation.

addThe filter is a very practical tool in the AnQiCMS template engine, it is mainly used in the following two scenarios:

  1. Add numbers:If the object being operated on is a number,addThe filter will perform addition operations.
    
    {{ 5|add:2 }} {# 结果:7 #}
    
  2. string concatenation:If the object being operated on is a string, or one of them is a string,addThe filter will concatenate them.
    
    {{ "安企"|add:"CMS" }}    {# 结果:安企CMS #}
    {{ "欢迎"|add:"使用"|add:"AnQiCMS" }} {# 结果:欢迎使用AnQiCMS #}
    
    It is noteworthy that when concatenating strings, you can use multipleaddfilters to make the code more fluid.

Format timestamps:stampToDateusefulness

As mentioned earlier,item.CreatedTimeIt returns a timestamp. Displaying this number directly is not intuitive. AlthoughaddThe filter can be used for string concatenation, but it cannot directly format timestamps into the common date and time format we see (e.g.,

AnQiCMS provides a specialstampToDatetag, which is used to format timestamps into the date and time string you need. Its usage is very concise:

{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}

Here"2006年01月02日 15:04"Is the standard placeholder for date and time formatting in Golang, corresponding to year, month, day, hour, and minute.You can adjust this format string as needed to display different date and time styles.

Use in combination: Dynamically add nickname and format time

Now we have mastered how to get the user's nickname, how to format the timestamp, andaddThe function of combining filters. Next, we will cleverly combine them to dynamically display the reviewer and the review time next to the comment content.

First, we can get it like beforeitem.UserName. Next, usestampToDateLabels willitem.CreatedTimeFormat it into a readable date and time string, and assign it to a temporary variable, such asformattedTime. Finally, we useaddThe filter concatenates the user's nickname, some separator text, and the formatted time string into a complete display text.

Let's see how tocommentListTo implement this effect in a loop:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
        <div class="comment-item">
            <p>
                {# 使用 stampToDate 格式化时间戳,并赋值给 formattedTime 变量 #}
                {% set formattedTime = stampToDate(item.CreatedTime, "2006年01月02日 15:04") %}
                
                {# 使用 add 过滤器将用户昵称、固定文本和格式化后的时间拼接起来 #}
                <strong>
                    {{ item.UserName|add:" " |add:"(发布于 "|add:formattedTime|add:")" }}
                </strong>
            </p>
            <p>{{ item.Content }}</p>
        </div>
    {% endfor %}
{% endcommentList %}

Through this code, above each comment, there will dynamically display information similar to “Zhang San (Posted on October 26, 2023, 10:30)” followed by the actual content of the comment.This not only improves the readability of the information, but also makes the comment section more professional and humanized.

Summary

AnQiCMS's template engine provides rich tags and filters, which can be flexibly combined to meet various complex page display needs. Masteraddthe filter meetsstampToDateThe usage of tags, you can easily add more dynamic and user-friendly information to the website comment section, thereby significantly improving the website user experience.


Frequently Asked Questions (FAQ)

1.addCan the filter directly format timestamps?No.addThe filter is mainly used for arithmetic operations on numbers and string concatenation. To format a timestamp into a readable date and time string, you need to use the AnQiCMS providedstampToDate.

If **2.item.UserNameNick

Related articles

How to implement the dynamic combination output of the `add` filter in the custom `diy` tag?

In AnQi CMS, content operation not only seeks accurate transmission of information, but also flexibility and automation of expression.We often encounter such needs: we hope to combine some independent content preset by the background with dynamic data on the page or fixed text in a clever way, forming a complete and creative expression.Today, let's delve deep into how the `add` filter and custom `diy` tags work together in Anqi CMS to help us achieve this dynamic combination of content output.### `diy` label: your content treasure box first

2025-11-07

How to use the `add` filter to dynamically add navigation arrow symbols to the titles of `nextArchive` or `prevArchive`?

In website content operation, the subtle aspects of user experience often determine the overall effect.For the article detail page, if the "Previous" and "Next" navigation at the bottom of the page can be designed more intuitively, it will undoubtedly greatly improve the user's browsing efficiency and comfort level.This article will discuss how to utilize the powerful template function of Anqi CMS, especially the `add` filter, to dynamically add arrow symbols to these navigation titles, making them clear at a glance.Understand the requirement: Why to dynamically add navigation arrows?Imagine when the user has finished reading an excellent article

2025-11-07

How to dynamically add language parameters in the `add` filter of the `languages` tag that returns multilingual site links?

Under the flexible multi-language support of AnQiCMS, providing content to users worldwide has become very convenient.The system built-in `languages` tag can easily list all the language versions supported by your website and their corresponding links.However, in some scenarios, we may not only want to provide language switching functionality, but also dynamically add additional parameters to these multilingual links, such as for marketing tracking, A/B testing, or loading personalized content in specific languages.

2025-11-07

Can the `add` filter be used to concatenate CSS style strings to implement dynamic inline styles?

AnQiCMS with its flexible and powerful template engine provides great convenience to users, allowing content presentation to be highly customized according to actual needs.In daily template development, we often encounter scenarios where we need to dynamically adjust the page display based on backend data, such as changing the title color based on the article reading volume, or displaying different background styles based on user permissions.At this point, we might wonder whether the built-in `add` filter of AnQiCMS templates can be used to concatenate CSS style strings to achieve these dynamic inline style controls

2025-11-07

Can the `add` filter directly add two numeric strings (such as "123" and "456") to get the numeric result 779?

AnQi CMS is a high-efficiency and feature-rich enterprise-level content management system, whose template engine provides flexible data processing capabilities.During template development, making good use of various filters can help us accurately control the display format of content.Today, we will delve into a common filter——`add`, especially its specific performance in handling numeric strings, which is often a place where users are likely to have doubts.

2025-11-07

How to implement multi-site content unified display and management in Anqi CMS?

In today's era where digital marketing and brand building are increasingly important, many companies or individuals often need to manage multiple websites, whether it is different brands, product lines, or multilingual, regional sites.Faced with the need for multi-site content management, AnQiCMS (AnQiCMS) provides a graceful and efficient solution that helps us easily achieve the unified display and management of content. **The Core Concept of Multi-Site Management in AnQi CMS** One of the design philosophies of AnQi CMS is that 'one system manages all'.

2025-11-07

How to utilize a flexible content model to customize the display of different types of content?

When using AnQiCMS to manage website content, we will quickly find that its 'flexible content model' feature is a core tool for content operation.It is not like traditional CMS, which confines all content to fixed 'article' or 'page' frameworks.AnQiCMS gives us the ability to define content structure, allowing us to customize the display of various types of information according to actual business needs, thereby making the website content more accurate and more attractive.

2025-11-07

How to ensure that the website correctly switches and displays content in a multilingual environment?

Expand the global market, a multilingual website is indispensable.It can help you reach users from different cultural backgrounds and language habits directly.In AnQiCMS (AnQiCMS), ensure that the website switches and displays content correctly in a multilingual environment, mainly relying on its powerful multi-site management and flexible template mechanism. Next, we will explore how to achieve this goal in Anqin CMS, allowing your website to display smoothly on the global stage.

2025-11-07