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:
- 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. - From
currentModuleExtract fromUrlAlias, it represents{module}The value, and assign it tomoduleAlias. - Then, we use
setLabel 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 as
item.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