How to concatenate `{module}` and `{id}` variables using the `add` filter in Custom URL mode to build a link?

Calendar 👁️ 71

In Anqi CMS, the way website links are constructed is crucial for SEO and user experience. Although the system provides various preset static rules and automatically generated links, in certain specific scenarios, we may need to control the URL structure more finely, such as in the custom URL mode, to customize the content model ({module})and content ID({id})variables concatenated to form a unique link.

This is not a difficult task, the powerful template engine of Anqi CMS combined with its flexible filter mechanism allows us to easily achieve this goal. Today, let's discuss how to make use ofaddFilter concatenation{module}and{id}Variables to build the link.

Understanding the URL mechanism and custom requirements of Anqi CMS

Our Aanqi CMS pays great attention to SEO-friendliness, built-in static link function, allowing you to convert dynamic links (such as/?article_id=123) into more readable and search engine friendly static links (such as/article/123.html)。In the "Function Management" under the "URL Rewrite Rules", you can choose from various preset rules such as numeric mode, model naming mode, and even enter the "Custom Mode" to fully control the link structure.

In custom mode, Anqi CMS provides like{module}(usually the URL alias or table name of the content model),{id}(the unique ID of the content),{filename}(Custom link name) and other rich variables for us to combine. For example, a pseudo-static rule might bearchive===/{module}/{id}.htmlThis means that the link of a document will be composed of its model alias and ID, such as/news/123.html.

However, setting up the pseudo-static rules does not mean that the template can directly display links that comply with this new rule. By default, when we use{{item.Link}}When outputting the links in the article list, Anqicms will automatically generate them according to the currently effective pseudostatic rules. But if we need to build these links at specific locations or with more complex logic, for example, without using{{item.Link}}Build manually in the case{module}and{id}Or need to concatenate with other fixed text or variables, then manual operation is required.

Core Tool:addFilter

In the Django template engine of Anqi CMS,addA filter is a very practical tool, mainly used for adding and concatenating numbers or strings.

Its usage is very simple, usually{{ 变量 | add: 值 }}Here, the 'value' can be another variable, a number, or a string literal.

For example:

  • If we want to add numbers:{{ 5 | add: 2 }}It will display7.
  • If we want to concatenate strings:{{ "安企" | add: "CMS" }}It will display安企CMS.
  • It can even mix types, ignoring the corresponding parts when the automatic conversion fails:{{ 5 | add: "CMS" }}It will display5CMS.

It is this ability to concatenate strings that makesaddfilters our tool for building custom URLs.

Build a custom link: Concatenation{module}and{id}

Now that we have the theoretical foundation, we can start the actual operation. Suppose our pseudo-static rules arearchive===/detail/{module}-{id}.htmlWe hope to manually construct such links in the template.

Firstly, we need to obtain{module}and{id}the values of these two variables.

  • {id}: Usually, it can be directly obtained from the current loop document object, for example{{item.Id}}.
  • {module}: The URL alias of the content model, which can be obtained byitem.ModuleIdObtaining the model ID of the current document, then usingmoduleDetailTags to get the details of the model, and extract from themUrlAliasfield.

Let's look at an example of building a custom link in a document list(archiveList) to create a custom link example:

{% comment %}
假设后台伪静态规则设置为:archive===/detail/{module}-{id}.html
例如:/detail/article-123.html
{% endcomment %}

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        {% comment %} 获取当前文档所属模型的URL别名 {% endcomment %}
        {% moduleDetail currentModule with id=item.ModuleId %}
        {% set moduleAlias = currentModule.UrlAlias %}

        {% comment %} 使用add过滤器拼接字符串和变量来构建链接 {% endcomment %}
        {% set customLink = '/detail/'|add:moduleAlias|add:'-'|add:item.Id|add:'.html' %}

        <li>
            <a href="{{ customLink }}">
                <h5>{{item.Title}}</h5>
                <p>自定义链接: {{ customLink }}</p>
                <p>系统自动生成链接: {{ item.Link }}</p>
            </a>
        </li>
    {% empty %}
        <li>暂无内容。</li>
    {% endfor %}

    {% comment %} 分页代码(略,与链接构建无关) {% endcomment %}
    {% pagination pages with show="5" %}
    {# ... 分页链接通常也会自动生成,但也可使用类似方法定制 #}
    {% endpagination %}
{% endarchiveList %}

In the above example, we:

  1. By{% moduleDetail currentModule with id=item.ModuleId %}get the current document(item)The details of the content model it belongs to, and assign it tocurrentModuleVariable.
  2. FromcurrentModuleExtract fromUrlAlias, it represents{module}The value, and assign it tomoduleAlias.
  3. Then, we usesetLabel combinationaddFilter, to fix the string'/detail/'/moduleAliasFixed connection symbol'-'Document IDitem.IdAnd fixed suffix.htmlStep by step, eventually forming a complete custom linkcustomLink.

Thus, you can flexibly build links that comply with your custom static rules in the template.

Considerations in practical applications.

There are several important points to note when customizing URLs:

  • Keep consistent with the background pseudo-static rules:The most critical point is, the URL structure you manually concatenate in the template,it mustIt is completely consistent with the "Custom Pattern" rule set in the "Function Management" -> "URL Rewrite Rule" of AnQi CMS backend. If the template generates/detail/news-123.html, but the backend rule is/article/{id}.htmlThat link will not be accessible. Ensuring synchronization is the key to success.
  • Link readability and SEO:One of the original purposes of custom URLs is for SEO and user-friendliness. Make sure your{module}and{id}links combined with other variables have semantic, concise, and easy-to-remember characteristics.
  • Variable availability:When concatenating links, please ensure that all variables used (such asitem.Id,currentModule.UrlAliasIt is available in the current template context. If a variable does not exist, concatenation may fail or generate an incomplete link.
  • Encoding issues:If your URL may contain Chinese or other special characters (though{module}and{id}it is usually English or numbers), you can consider usingurlencodeThe filter encodes part of the URL parameters to avoid potential link parsing issues. However, for{module}and{id}such system variables, it is usually not necessary to encode manually.

Summary

The flexibility of Anqi CMS is manifested in

Related articles

In the AnQiCMS template, can a simple counter dynamic display be realized through the `add` filter?

AnQiCMS is an enterprise-level content management system developed based on the Go language, providing strong support for content operators with its efficient and flexible features.In daily content operation and template creation, we often encounter the need to process and dynamically display data, such as adding numbers to list items and calculating totals.Among them, the template filter is an important tool for realizing such needs.Today, let's discuss the `add` filter in the AnQiCMS template to see if it can help us achieve a simple dynamic counter display.###

2025-11-07

Does the `add` filter cause garbled or incorrect output when concatenating Chinese and English mixed strings?

When using AnQiCMS for template development, we often need to concatenate different text content, such as dynamically generated titles, descriptions, etc.At this time, the `add` filter has become a powerful tool in our hands.However, when dealing with mixed Chinese and English strings, many friends may worry: Will such concatenation produce garbled characters or cause program errors?Today, let's discuss this issue in detail.

2025-11-07

How to use the `add` filter to dynamically generate the complete path segment in the breadcrumb navigation (`breadcrumb`)?

In website operation, breadcrumb navigation is one of the key elements to improve user experience and website SEO performance.It clearly shows the user's position on the website and provides a convenient way to return to the previous level page.AnQi CMS provides a powerful and flexible template tag system, where the `breadcrumb` tag can help us easily implement breadcrumb navigation.But if we need to dynamically adjust the path text or links in the breadcrumb navigation, the `add` filter of Anqi CMS can play a unique role.`breadcrumb`

2025-11-07

How to combine the `add` filter with the `stampToDate` function to concatenate a formatted date and time string?

When managing content in AnQi CMS, we often need to display dates and times in a specific format.The system provides very convenient template tags and filters to handle these requirements.Today, let's talk about how to combine the `add` filter with the `stampToDate` function to concatenate a formatted date-time string, making our content display more flexible and diverse.### Get to know the `stampToDate` function: Format timestamps First, let's review the `stampToDate` function

2025-11-07

How does the `add` filter assist in building dynamic `<img>` tag `alt` or `title` attribute text to optimize SEO?

In Anqi CMS, every detail of the website is related to the final operating effect, especially in search engine optimization (SEO).We all know that high-quality images can attract users, but if these images are not 'understood' by search engines, their value will be greatly reduced.At this time, the `alt` and `title` attributes of the `<img>` tag are particularly important.They can not only improve the accessibility of the website but are also a key window for search engines to convey the content and context of the image.However, manually writing a unique

2025-11-07

When it comes to concatenating a large number of strings, which filter, `add` or `join`, performs better?

In AnQi CMS template development, we often need to combine different text fragments into a complete string to meet the display requirements of the page.AnQi CMS provides various template filters to complete this task, among which the `add` and `join` filters are two commonly used string concatenation tools.However, when faced with the need to concatenate a large number of strings, choosing the right tool becomes particularly important, as this directly affects the speed of page rendering and user experience.###

2025-11-07

How to pass the `add` filter as a parameter to the `macro` macro function and perform internal text processing?

In AnQi CMS daily content operation, we pursue efficient and flexible management and display of website content.The template system is the core of achieving this goal, among which the `macro` macro function and the `add` filter are two very practical tools.Understand how they work together, especially how to use the `add` filter for internal text processing in macro functions, which can greatly enhance the reusability and dynamism of our templates.The AnQi CMS template system uses syntax similar to Django, which allows us familiar with Web development to quickly get started.

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