In the template creation of AnQi CMS, flexible and SEO-friendly URL paths are the key to improving the website's search engine performance. Although AnQi CMS provides powerful static rule configuration capabilities in the background, by defining such/{module}-{id}.htmlGenerate static URLs using a pattern, but in certain specific scenarios, we may need to have more dynamic and fine-grained control or combination of the local URL paths within the template, in order to meet unique business needs or marketing strategies. At this point,addThe filter has become a very practical auxiliary tool.
AnQiCMS URL path SEO basics
It is very important to understand how Anqi CMS builds SEO-friendly URLs.The core of AnQi CMS lies in its powerful pseudo-static rule management feature.In the background, we can set a unified URL structure for document detail pages, category list pages, single pages, etc., for example:
- Document details:
archive===/{module}/{id}.htmlorarchive===/{module}/{filename}.html - Category list:
category===/{module}/{catid}/orcategory===/{module}/{catname}/
These rules define the basic structure of the website URL, search engines will use these clear structures to crawl and index pages. The system will automatically alias the content model names{module})、Document ID({id})、File Name({filename}), classification ID ({catid})、Category Name({catname}Fill dynamic data into the preset URL pattern to generate a static access path.This greatly simplifies the work of website administrators while ensuring the structurization and readability of URLs.
addWhat is a filter?
In AnQi CMS template syntax,addA filter is a very basic but powerful tool, mainly used for performing addition operations on numbers or strings.Just like in many programming languages, when used with numbers, it performs mathematical addition;When used for strings, it concatenates two strings.
For example:
{{ 5|add:2 }}Will be displayed7{{ "安企"|add:"CMS" }}Will be displayed安企CMS
This seemingly simple function, in scenarios related to URL paths, can still show unexpected flexibility.
addThe practical scenarios of filters in URL construction
Although the pseudo-static rules of AnQi CMS define the structure of most core URLs on the website, butaddThe filter provides us with the ability to "micro-tune" or "extend" these URLs at the template level, especially suitable for the following situations:
Dynamically append query parameters:Marketing campaigns, A/B testing, or user behavior tracking often require appending dynamic query parameters to the URL, such as
?utm_source=.../?ref=...These parameters are usually not part of the static rule, but are crucial for data analysis. We can utilizeaddThe filter dynamically appends these parameters to the end of articles, products, and other links.Example: Add UTM tracking parameters to the article linkSuppose we have an article link variable
item.LinkIts value is/article/anqi-cms-intro.htmlWe want to add UTM tracking parameters.<a href="{{ item.Link|add:'?utm_source=newsletter&utm_medium=email' }}">阅读详情</a> {# 生成的URL可能类似:/article/anqi-cms-intro.html?utm_source=newsletter&utm_medium=email #}If the parameter value itself is dynamic:
{% set campaign_source = "wechat" %} {% set article_id = item.Id %} <a href="{{ item.Link|add:'?source='|add:campaign_source|add:'&article_id='|add:article_id }}">查看文章</a> {# 如果item.Link是/article/123.html,article_id是123,campaign_source是wechat,则生成:/article/123.html?source=wechat&article_id=123 #}Build a custom URL fragment for specific business needs:In some non-standardized pages or functional modules, it may be necessary to dynamically generate a URL path segment based on multiple data fields. Although it is not recommended to use this complex logic for the core content URL of a website (pseudo-static rules should be prioritized), it is used in some special auxiliary pages, such as report download links, specific filtering result pages, etc.
addThe filter can flexibly combine different variables to form a path.Example: Build a download link for a composite query.Assuming we have a page to download reports, the path needs to be dynamically generated based on the report type and year.
{% set report_type = "annual" %} {% set report_year = "2023" %} <a href="/downloads/reports/{{ report_type|add:'-'|add:report_year|add:'.pdf' }}">下载{{ report_year }}年{{ report_type }}报告</a> {# 生成的URL:/downloads/reports/annual-2023.pdf #}here,
addThe filter effectively connects strings, variables, and delimiters to form a complete URL.Auxiliary concatenation for URL alias or SLUG:When the Anqi CMS generates URL aliases in the background, it usually generates pinyin or English based on the title. However, in the template, if you need to further concatenate these aliases, for example, by adding a language prefix or a specific identifier,
addThe filter can also do it