How to use the `add` filter to dynamically add a uniform prefix or suffix to the single page titles in the `pageList`?

Calendar 👁️ 58

In AnQi CMS, content management is not limited to the backend operations of add, delete, modify, and query, but also reflects the flexibility and diversity of front-end display.In the face of common single-page websites (such as "About Us", "Contact Us", etc.), sometimes we hope that when they are displayed in a list or navigation, they can be unified with a specific identifier, such as If each backend title is modified individually, it is not only inefficient but also lacks uniformity and maintainability.At this moment, by utilizing the powerful template engine function of AnqiCMS and with the clever filter, this requirement can be easily achieved.

Background: Understanding the importance of single-page and titles

AnqiCMS providespageListTags, used to conveniently loop through the individual web pages created on the site. Each single page has basic attributes such as title, link, and content. By default, using{{ item.Title }}It can output the original title of a single page. However, in actual operation, a website may need to modify these titles twice to adapt to specific marketing strategies, content classification standards, or simply to make the user interface more friendly and consistent.For example, you may wish to add the prefix "[Legal Statement]" to all legal statement pages, or add the suffix "-Job List" to all recruitment pages.

Core Tool:addThe wonder of filters

The AnqiCMS template system has adopted Django-style tags and filters, providing great flexibility. Among many filters,addA filter is a seemingly simple but powerful tool that can not only be used for arithmetic operations on numbers but also be competent in string concatenation tasks. Its basic usage is{{ obj|add:obj2 }}, it will be used toobjandobj2Concatenate (if it is a string) or add (if it is a number).In the scenario of dynamically adding prefixes or suffixes to single-page titles, we are taking advantage of its excellent string concatenation ability.

Practice: Dynamically Add Prefixes and Suffixes

Let's look at a few specific examples to see how toadda filter topageListtitle on a single page.

First, a typicalpageListThe loop is roughly like this:

{% pageList pages %}
    {% for item in pages %}
        <li>
            <a href="{{item.Link}}">{{item.Title}}</a>
        </li>
    {% endfor %}
{% endpageList %}

Now, let's try to dynamically add prefixes or suffixes to these titles.

1. Add a uniform prefix.

If you want all single-page titles to have a unified prefix, such as “【Latest Release】”, you can modify the template code in this way:

{% pageList pages %}
    {% for item in pages %}
        <li>
            <a href="{{item.Link}}">{{ "【最新发布】"|add:item.Title }}</a>
        </li>
    {% endfor %}
{% endpageList %}

In this code block,"【最新发布】"is a string literal, through the pipe symbol|pass it toaddthe filter, and withitem.Titleas the second argument.addThe filter will concatenate these two strings to form effects like “【Latest Release】About Us”、“【Latest Release】Contact Information” such as this. Please note that it is usually a good habit to add a space at the end of the prefix string, for example"【最新发布】 ".

2. Add a uniform suffix

Similarly, if you want to add a uniform suffix to the title, such as “- Official Information”, just need toaddThe position and parameter order of the filter should be adjusted:

{% pageList pages %}
    {% for item in pages %}
        <li>
            <a href="{{item.Link}}">{{ item.Title|add:" - 官方信息" }}</a>
        </li>
    {% endfor %}
{% endpageList %}

here,item.TitlePassed as the first parameteraddThen concatenate the string literal after the filter" - 官方信息"Similarly, adding a space at the beginning of the suffix string can ensure better readability.

3. Add both prefix and suffix.

Of course, you can also add both prefix and suffix to the title.addThe filter supports chaining calls, making the operation very intuitive:

{% pageList pages %}
    {% for item in pages %}
        <li>
            <a href="{{item.Link}}">{{ "【公司】"|add:item.Title|add:" (必读)" }}</a>
        </li>
    {% endfor %}
{% endpageList %}

Through twiceaddChaining of filters, the single-page title will be displayed in the format “[Company] About Us (Mandatory)”.

4. Dynamically add prefixes or suffixes based on conditions.

In more flexible scenarios, you may need to decide whether to add prefixes or suffixes, or different text based on certain specific properties of a single page. At this point, the AnqiCMS template'sifLogical judgment tags can be put to use:

{% pageList pages %}
    {% for item in pages %}
        <li>
            {% if item.Id == 1 %} {# 假设Id为1的页面是"关于我们" #}
                <a href="{{item.Link}}">{{ "【重磅】"|add:item.Title }}</a>
            {% elif item.Title|contain:"招聘" %} {# 假设标题包含"招聘"字样 #}
                <a href="{{item.Link}}">{{ item.Title|add:" - 最新岗位" }}</a>
            {% else %}
                <a href="{{item.Link}}">{{ item.Title }}</a>
            {% endif %}
        </li>
    {% endfor %}
{% endpageList %}

In this example, we combinedif/elif/elseas well ascontainThe filter has implemented more precise control: if the single-page ID is 1, then add the prefix “[重磅]”; if the title contains the words “Recruitment”, then add the suffix “- Latest Positions”; otherwise, display the original title.

Cautionary notes and **practice

In useaddWhen dynamically modifying the title of the filter, there are several aspects that we should pay attention to:

  • Space handling:As mentioned before, be sure to pay attention to whether the prefix or suffix string you add contains the necessary spaces to ensure the natural smoothness of the final display effect.
  • Front-end display:This modification only affects the display effect of the front-end page, and does not touch the original single-page title content stored in the database.This means you can adjust the front-end display strategy at any time without causing permanent changes to the content itself, greatly enhancing operational flexibility.
  • SEO consideration:Although this is mainly for front-end display, search engines will also see these decorated titles when crawling and rendering the page.Therefore, ensure that the prefix or suffix content is concise, relevant, and can assist rather than interfere with the search engine's understanding of the page topic, and avoid keyword stacking.
  • **Readability and User Experience:

Related articles

How does the `add` filter handle the addition/pasting operation with boolean values `true` and `false` as well as numbers or strings?

In Anqi CMS template development, we often need to process and display data.Among them, the `add` filter is a very practical tool that allows us to add numbers and concatenate strings.However, when the boolean values `true` and `false` are involved in these operations, their behavior may puzzle some users who are new to the subject.Today, let's take a deep dive into how the `add` filter handles addition or concatenation operations with boolean values, numbers, or strings.

2025-11-07

How to dynamically generate JavaScript code snippets in AnQiCMS templates and use the `add` filter to concatenate variables?

In website operations, we often need to make the page more intelligent and interactive.AnQiCMS as an efficient and flexible content management system not only provides powerful content organization and display capabilities, but also allows us to ingeniously integrate dynamic logic in templates, such as by generating JavaScript code snippets to achieve more complex functions.AnQiCMS's template engine syntax is similar to Django, which provides great convenience for us to dynamically process data.It is developed based on the Go language, runs fast

2025-11-07

Can the `add` filter be used to generate dynamic HTML attributes, such as the value of `data-` attributes?

In modern web development, dynamically generating HTML attributes, especially `data-` attributes, has become a common requirement.These properties not only provide additional data support for front-end JavaScript, but also enhance the presentation of elements without affecting the page semantics.AnQiCMS as a content management system that focuses on flexibility, its powerful template engine naturally also provides the possibility to meet this need.So, can the `add` filter in the AnQiCMS template handle the task of dynamically generating `data-` attribute values?

2025-11-07

How to use the `add` filter to dynamically add additional descriptions to the field in the AnQiCMS backend user group management (`userGroupDetail`)?

In the AnQiCMS management background, the user group management (`userGroupDetail`) module is the basis for us to divide permissions, set levels for different user groups, and even configure VIP services.Generally, each user group has its core attributes, such as name, level, price, etc., which are directly displayed on the back-end interface for convenient daily management.However, sometimes we hope that these fields are not just cold data, but can also have some additional, more descriptive information. For example

2025-11-07

How to combine other filters with the `add` filter to ensure safe concatenation and prevent XSS attacks when processing user input?

In AnQi CMS, managing website content involves entering a variety of information regularly.Whether it is the main text of the article, user comments, or form submissions, this user-generated content injects vitality into the website while also bringing potential security risks, the most common and severe of which is XSS (Cross-site Scripting attack).This article will discuss how the `add` filter works in processing user input content, how it协同acts with other security filters to build a strong defense line, effectively preventing XSS attacks.

2025-11-07

How to dynamically generate a link to the comment area based on `item.Id` in `archiveList`?

In AnQi CMS, in order to enhance the user experience of the website, we often hope that users can quickly find the specific content on the page.For example, when you click on the "View Comments" link next to an article title on a document list page, you can directly jump to the comments section of the article detail page.This not only saves users time, but also makes interactions more direct. Today, let's discuss how to dynamically generate anchor links to comment sections in the `archiveList` loop based on the unique identifier `item.Id`.

2025-11-07

`add` filter combined with `default` filter to set default values for variables that may be empty before concatenation?

In the daily operation of AnQi CMS, we often need to concatenate different field content to form a complete information, such as generating a complete address or combining an attractive product title.However, a common challenge with dynamic content is that some variables may be empty.If concatenated directly, the blank content not only affects the appearance but may also lead to missing information or disordered format.Fortunately, AnQiCMS's powerful Django template engine grammar provides us with a flexible solution

2025-11-07

How to dynamically display

Display the record number at the bottom of the website, which is not only a requirement to comply with relevant laws and regulations, but also an important factor in enhancing the professionalism and user trust of the website.For users of AnQiCMS, dynamically displaying the filing number through the built-in powerful functions of the system is a very convenient operation, eliminating the need to frequently modify the code and greatly improving the maintenance efficiency of the website. ### Set the record number content in the background First, we need to set your website record number in the AnQi CMS background management interface. This is the basis of all dynamic display content, ensuring the accuracy of information

2025-11-07