In AnQi CMS, the way websites are linked is crucial for SEO and user experience. Although the system provides various predefined rules for pseudo-static links and automatically generated links, in certain specific scenarios, we may need to have more fine-grained control over the URL structure, such as in the custom URL mode, where the content model ({module})and content ID({id}Concatenate the variable 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 use the custom URL mode toaddFilter Concatenation{module}and{id}Build links with variables.

Understand the URL mechanism of Anqi CMS and customize your needs

AnQi CMS attaches great importance to SEO-friendliness, built-in static functionality, allowing you to convert dynamic links (such as/?article_id=123) into more readable and SEO-friendly static links (such as/article/123.html)。In the 'Feature Management' under 'Rewrite Rules', you can choose from various preset rules such as numeric patterns, model naming patterns, and even enter the 'Custom Mode' to fully control the link structure.

In Custom mode, Safe CMS provides something 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}.html, which means the link of a document will be composed of its model alias and ID, like/news/123.html.

However, simply setting up the rewrite rules does not mean that the links conforming to the new rules can be directly displayed in the template. By default, when we use{{item.Link}}When outputting the list of links in the article, the Anqi CMS will automatically generate them according to the currently effective pseudo-static rules. But if we need to construct these links at a specific location or with more complex logic, for example, without using{{item.Link}}build manually in the case or it is necessary to concatenate with other fixed text or variables.{module}and{id}If you need to concatenate with other fixed text or variables, manual operation is required.

Core Tools: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 }}will be displayed7.
  • If we want to concatenate strings:{{ "安企" | add: "CMS" }}will be displayed安企CMS.
  • It can even mix types, ignoring the corresponding parts if automatic conversion fails:{{ 5 | add: "CMS" }}will be displayed5CMS.

It is this ability to concatenate strings thataddfilters become our weapon of choice for building custom URLs.

Build Custom Links: Concatenation{module}and{id}

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

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

  • {id}: They can usually be obtained directly from the current document object in the loop, for example{{item.Id}}.
  • {module}The URL alias of the content model, which can be obtained like this: first getitem.ModuleIdthe model ID of the current document, then usemoduleDetailthe tag to get the details of the model, and extract from itUrlAliasfield.

Let's see an example of building custom links in a document list (archiveList) with an 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. Pass{% moduleDetail currentModule with id=item.ModuleId %}get the current document (itemThe details of the content model it belongs to, and assign it tocurrentModulea variable.
  2. FromcurrentModuleExtracted fromUrlAliasrepresents{module}Assign its value tomoduleAlias.
  3. Then, we usesetTag combinationaddFilter, a fixed string'/detail/'/moduleAlias, a fixed connector'-', Document IDitem.Idand a fixed suffix.htmlpiece by piece, ultimately forming a complete custom linkcustomLink.

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

Considerations in practical application

When customizing URLs, there are several important points to note:

  • Keep consistent with the back-end pseudo-static rules:The most critical point is, the URL structure you manually assemble in the template,MustThe custom pattern rule is completely consistent with the one set in the "Function Management" -> "URL Rewrite Rule" under the AnQi CMS backend. If the template generates the following:/detail/news-123.htmlHowever, the rule in the backend is:/article/{id}.htmlThen the generated link will be inaccessible. Ensuring both are synchronized 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 or without variables have semantic, concise, and easy-to-remember characteristics.
  • The availability of variables:When concatenating links, please ensure that all variables used (such asitem.Id,currentModule.UrlAliasThe value is available in the current template context. If a variable does not exist, concatenation may fail or generate an incomplete link.
  • Encoding issue: If your URL may contain Chinese or other special characters (although{module}and{id}usually English or numbers), you might consider usingurlencodeThe filter encodes parts of the URL parameters to avoid potential link parsing issues. However, for{module}and{id}this system variable, manual encoding is usually not required.

Summary

The flexibility of AnQi CMS is体现在