How to use the `add` filter to dynamically add 'Published on:' before the blog post's publish time?

Calendar 👁️ 60

When operating a blog, we often hope that the publication time of the articles can be presented in a more intuitive and brand style.For example, adding "Published on:" before the date makes it clear to the reader that this is a published article.AnQiCMS (AnQiCMS) with its flexible template engine makes such customization very simple.addFilter, dynamically adds the text 'Published on:' before the publication time of blog posts.

Core tool analysis: understandaddFilters andstampToDateTag

To implement this feature, we need to use two key features of the AnQiCMS template engine:archiveDetailtags,stampToDateTags andaddfilter.

  1. archiveDetailTagIn the article detail page,archiveDetailTags are the entry points to obtain all the detailed information of an article. Through it, we can easily obtain the article's publication time field, usuallyarchive.CreatedTime. ThisCreatedTimestored in the form of timestamps.

  2. stampToDateTagdue toarchive.CreatedTimeIt is a timestamp, we need to convert it to the date and time format we are accustomed to.stampToDateThe tag is born for this. It can format Unix timestamps according to the Go language time format string you specify. For example,{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}It will format the timestamp into the form "2023 February 09 10:30" such as this.

  3. addFilter addThe filter is the main character of this operation.Its function is very direct - it adds two values.Here, the 'addition' does not only refer to the addition of numbers, but more importantly, it supports string concatenation.This means that we can use it to concatenate the string 'Published at:' with the formatted date string.{{ obj|add:obj2 }}For example,{{ "你好"|add:"世界" }}It will display "Hello World".

Practice Exercise: Implement Custom Display Step by Step

Now, let's apply these tools step by step to your blog article detail page

Step 1: Locate Your Template File

Generally, the template file for the blog article detail page is located in the current theme directory of you.archive/detail.html.You can edit online through the 'Template Design' -> 'Template Management' feature in the AnQiCMS backend, or find and edit the file through FTP or other file management tools.

Step 2: Find the code for the article publish time.

Open.archive/detail.htmlFile, find the code segment that displays the article's publication date. It might look like this:

<span>{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</span>

Or even simpler:

<span>{{archive.CreatedTime}}</span>

Step 3: Combine usagestampToDateandaddFilter

Once we find the existing date display code, we can start modifying it.

First, make sure yourCreatedTimehas beenstampToDateFormat into a readable string. If not yet, please wrap it first:

{% set formattedTime = stampToDate(archive.CreatedTime, "2006年01月02日 15:04") %}

Here we usesetThe label assigns the formatted time to a temporary variableformattedTimeThis makes the code clearer.

Next, we will concatenate the string "Published on: " withformattedTimethe variable throughaddand join it with the filter:

{{ "发布于:"|add:formattedTime }}

Step 4: Apply the modified code to the template

Replace the above code with the original date display code you found in step two. The complete code block may look like this:

Example before modification:

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div>
        <a href="{% categoryDetail with name='Link' %}">{% categoryDetail with name='Title' %}</a>
        <span>{% archiveDetail with name="CreatedTime" format="2006年01月02日" %}</span> {# 或者直接是 {{archive.CreatedTime}} #}
        <span>{% archiveDetail with name="Views" %}°</span>
    </div>
    {# ... 文章内容及其他部分 ... #}
</article>

Example after modification:

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div>
        <a href="{% categoryDetail with name='Link' %}">{% categoryDetail with name='Title' %}</a>
        {# 在这里插入我们组合好的发布时间 #}
        <span>
            {%- set formattedTime = stampToDate(archive.CreatedTime, "2006年01月02日 15:04") -%}
            {{ "发布于:"|add:formattedTime }}
        </span>
        <span>{% archiveDetail with name="Views" %}°</span>
    </div>
    {# ... 文章内容及其他部分 ... #}
</article>

Please note{%- set ... -%}The dashes on both sides, their role is to remove any blank lines that the tag itself may have generated, making the final HTML output cleaner.

Save the template file you have modified.

Step 5: Clear system cache

To ensure that your changes take effect immediately, please log in to the AnQiCMS backend, click the "Update Cache" button at the bottom of the left menu, and clear all system caches.After that, refresh your blog article detail page, and you should be able to see that the word 'Published on:' has been dynamically added in front of the publish time.

温馨提示

  • Backing up is a good habit: Before making any changes to a template file, please be sure to back up the original file, in case of any issues you can recover quickly.
  • Applicable to all time fields: addFilters andstampToDateLabels are not only suitable for the article publish timeCreatedTimeYou can also apply it to the article update timeUpdatedTimeOr any timestamp field.
  • The flexibility of string formatting: stampToDateThe time format string in the label is based on the Go language formatting standard, which is very flexible. You can adjust it as needed, such as displaying only year, month, and day, or adding the day of the week, etc.

By following these steps, you have successfully utilized the template features of AnQiCMS to add a custom prefix to the publication time of your blog posts.This not only improves the personalization of the website, but also brings a clearer and more user-friendly reading experience to the readers.


Frequently Asked Questions (FAQ)

Q1: Does the prefix 'Published on:' only appear on the article detail page? What should I do if I want to display it on the article list page? A1:Yes, the modification demonstrated here is for the article detail page template (usuallyarchive/detail.html). If you want to modify the article list page (for examplearchive/list.htmlor the homepageindex.html) Also display the article time with "Published on:", you need to find the template files where the article list is displayed in a loop (usually usingarchiveListThe part marked) should then be modified at the corresponding date display location according to the method provided in this article.

Q2: Can I add other text or HTML tags besides “Published on:”? A2:Of course you can.addThe filter can concatenate any string, including strings with HTML tags. For example, if you want to use a single<em>Label it and prepend with 'Published on:' like this:{{ "发布于:<em>"|add:formattedTime|add:"</em>" }}Please note that if the concatenated content contains HTML tags, you may need to use the outermost one|safefor example, a filter (such as{{ "发布于:<em>"|add:formattedTime|add:"</em>"|safe }}),to ensure that the browser can correctly parse HTML rather than displaying it as plain text.

Q3: If I modified the template file and cleared the cache, but I didn't see any changes on the page, what could be the reason? A3:In this situation, you can check from the following aspects:

  1. Syntax error:Please carefully check if there are any spelling errors, unclosed tags, and other syntax issues in the code you have modified.The AnQiCMS Go template engine has strict syntax requirements, any small error can cause template parsing to fail.
  2. **Template

Related articles

What is the final concatenation result when one operand of the `add` filter is a number and the other is a string that cannot be converted to a number?

AnQi CMS is an enterprise-level content management system developed based on the Go language, dedicated to providing users with efficient and customizable content management solutions.In daily content operation and template development, we often use various template tags and filters to process data, where the `add` filter is a very practical feature that can help us conveniently perform numerical addition or string concatenation.However, when operands of mixed types are involved, the logic of their behavior becomes particularly important.

2025-11-07

How to use the `add` filter in a loop to implement alternating row colors or dynamically concatenate different CSS class names?

In modern web design, the visual presentation of list content has an important impact on user experience.Whether it is an article list, product display, or comment section, by giving list items different styles, such as alternating row colors or applying different class names based on specific conditions, it can significantly improve the readability and aesthetics of the page.AnQiCMS is a powerful template engine that provides flexible tools to meet these dynamic style requirements, where the `for` loop's `cycle` tag and `add` filter are our reliable assistants.###

2025-11-07

The `add` filter performs mathematical calculations in the AnQiCMS template, what is the difference from the arithmetic operation tag in `tag-calc.md`?

During the template development process of AnQiCMS, we often encounter scenarios where numerical calculations or logical judgments are required.The system provides us with various processing methods, among which the `add` filter and the arithmetic operation capabilities introduced in `tag-calc.md` are two commonly used tools.Although they all involve numerical processing, they have significant differences in functional positioning, usage, and complexity.First, let's understand the `add` filter.As the name implies, the `add` filter is mainly used to perform **addition operations or string concatenation**

2025-11-07

How to use the `add` filter to dynamically add a brand name or fixed suffix to the `Title` of the page `TDK`?

In website content operation, the page's "Title", "Keywords", and "Description", which is what we often call TDK, plays a crucial role in search engine optimization (SEO).A well-structured and brand-recognizable page title that not only helps search engines better understand the page content but also attracts users' attention in search results.AnQiCMS provides a flexible template engine, allowing us to finely control these key information

2025-11-07

Does the `add` filter support chained calls, such as `{{ var1|add:var2|add:var3 }}` to concatenate multiple variables?

When developing templates in Anq CMS, we often encounter situations where we need to combine, concatenate, or sum multiple variables.Among them, the `add` filter is a very practical tool that allows us to perform addition operations on numbers or concatenate strings.However, some users may be curious whether the `add` filter supports a chain-like call like `{{ var1|add:var2|add:var3 }}` to concatenate multiple variables at once?Familiarize yourself with the template syntax of AnQi CMS after deep study

2025-11-07

How does the `add` filter flexibly combine fixed text with variable content when building dynamic prompt information?

In Anqi CMS template design, building dynamic prompts with real-time interactive features is the key to improving user experience.Whether it is to display product inventory, user welcome messages, or article reading volume, we hope to seamlessly combine fixed text with continuously changing variable content.At this point, the `add` filter becomes a very practical and flexible tool.The `add` filter in the Anqi CMS template is very intuitive: it can add or concatenate two values.This process has high intelligence. If the two values being operated on are of numeric type, it will perform mathematical addition.

2025-11-07

Can the `add` filter be used to concatenate array elements processed by the `slice` or `split` filters to form a new string?

When developing templates with AnQi CMS, we often need to flexibly process and display data.This includes string splitting, slicing, and element connection.AnQi CMS provides a rich set of filters (filters) to help us complete these tasks, such as `add`, `slice`, and `split`.Sometimes, we might consider concatenating array elements obtained after processing with `slice` or `split` filters using the `add` filter to form a new string.But is this idea feasible

2025-11-07

How to use the `add` filter to dynamically add custom tracking parameters to `tag` links?

In website operation, we often need to track user behavior and evaluate the effectiveness of marketing through different channels.Adding tracking parameters dynamically to website links is an effective method.AnQi CMS is an efficient and flexible content management system that provides a powerful template engine and rich filters, allowing us to easily meet this requirement. Today, let's discuss how to use the `add` filter of Anqi CMS to dynamically add custom tracking parameters to the link of the "Tag".

2025-11-07