How to specifically apply the `urlencode` filter to URL parameters in AnQiCMS templates?

Calendar 👁️ 73

In AnQiCMS template development, building dynamic URLs is a common requirement.The correctness of the URL is crucial whether it links to the search results page, the filter list, or passes specific parameters to the backend service.urlencodeThe filter has become a key tool to ensure that URL parameters are valid and secure.

Understanding the importance of URL encoding.

URL (Uniform Resource Locator) has a strict character specification. Some characters have special meanings in URLs, such as/used for path separation,?used to introduce query parameters,&Used to separate multiple query parameters,=Used to separate parameter names and parameter values. In addition, spaces, Chinese characters, and other non-ASCII characters, or certain punctuation marks are not allowed to appear directly in URLs.

When a URL parameter contains these special characters, if it is not processed, the browser or server may incorrectly parse this URL, causing the page to load abnormally, or the parameter to be passed incompletely, or even trigger a security vulnerability. For example,searchTerm=安企CMS & GoLangThis parameter, if directly placed in the URL,&the symbol will be incorrectly parsed as a new parameter separator, causingGoLangpartial loss.

The purpose of URL encoding (also known as percent encoding) is to convert these special characters into a URL-safe format. For example, spaces are encoded as%20,&Will be encoded as%26Chinese characters are encoded as a series of percentage signs followed by hexadecimal numbers.

AnQiCMS template inurlencodeFilter

AnQiCMS provides a template engine that offersurlencodeA filter specifically used for percent-encoding the values of variables in a URL. Its usage is very intuitive, just add it after the variable that needs to be encoded|urlencodeJust do it.

Basic syntax:

{{ 变量 | urlencode }}

This filter will check变量The content, and all characters that do not conform to URL specifications should be converted to their percent-encoded forms to ensure that the generated URL does not cause any issues during network transmission and parsing.

Application scenarios in practice

Let's look at some specific examples to see.urlencodeHow does the filter work in the AnQiCMS template:

  1. Dynamically generate search results links:Assuming your website has a search function, the keywords entered by users may contain spaces, Chinese characters, or special symbols.To ensure the validity of the search link, we need to encode the keywords.

    {# 假设用户输入的搜索关键词存储在变量 searchTerm 中 #}
    {% set searchTerm = "AnQiCMS 模板开发 & SEO" %}
    
    {# 使用 urlencode 过滤器对关键词进行编码 #}
    <a href="/search?q={{ searchTerm|urlencode }}">点击搜索:{{ searchTerm }}</a>
    
    {# 渲染后的 HTML 可能是这样的: #}
    {# <a href="/search?q=AnQiCMS%20%E6%A8%A1%E6%9D%BF%E5%BC%80%E5%8F%91%20%26%20SEO">点击搜索:AnQiCMS 模板开发 & SEO</a> #}
    

    In this example,searchTermincluding spaces, Chinese and&The symbols are correctly encoded, ensuring the completeness and accessibility of the URL.

  2. Build a filter link with complex parameters: When you need to filter a list based on multiple conditions (such as category names, product attribute values), and these conditions may contain special characters,urlencodeit is indispensable as well.

    {# 假设我们有一个分类名称变量 categoryName #}
    {% set categoryName = "产品系列 (新品)" %}
    {# 假设还有一个属性值变量 attributeValue #}
    {% set attributeValue = "金属 & 塑料" %}
    
    <a href="/products?category={{ categoryName|urlencode }}&attribute={{ attributeValue|urlencode }}">
        查看 "{{ categoryName }}" 下的 "{{ attributeValue }}" 产品
    </a>
    
    {# 渲染后的 HTML 可能是这样的: #}
    {# <a href="/products?category=%E4%BA%A7%E5%93%81%E7%B3%BB%E5%88%97%20%28%E6%96%B0%E5%93%81%29&attribute=%E9%87%91%E5%B1%9E%20%26%20%E5%A1%91%E6%96%99">查看 "产品系列 (新品)" 下的 "金属 & 塑料" 产品</a> #}
    

    here,categoryandattributeParameters inside the brackets, spaces, and Chinese characters and&symbols have been properly processed, ensuring the independent transmission of parameters.

  3. Refer to an external link with parameters:Sometimes we need to dynamically reference an external link in the AnQiCMS template, and this external link itself may have parameters, or we may need to inject our own parameters into the URL of the external link.urlencodeIt can help us ensure that these parameters are encoded correctly.

    {# 假设有一个动态生成的外部链接目标,本身可能包含查询参数 #}
    {% set externalLink = "https://www.example.com/callback?data=用户详情 & token=abc" %}
    
    {# 对整个外部链接(包括其参数)进行 urlencode,以确保作为另一个 URL 的参数时安全 #}
    <a href="/redirect?url={{ externalLink|urlencode }}">跳转到外部页面</a>
    

    In the scenario of "URL within URL", the parameters of the outer URL are processed.urlencodevital.

iriencodeFilter: An important supplement.

excepturlencode,AnQiCMS also providesiriencodea filter. Although both are related to URL encoding, their focus of use is somewhat different, especially when dealing with&symbols, their performance is different.

According to the document example,iriencodeThe filter will also treat&the symbol is encoded as an HTML entity&amp;, not the URL percent-encoded version of,%26. This makesiriencodeMore suitable for securely embedding URI strings (possibly dynamically generated) into HTML attributes such as<a>label'shrefProperties in, to prevent the browser from misinterpreting it as HTML code or destroying the HTML structure.

iriencodeThe syntax with examples:

{{ 变量 | iriencode }}

{# 示例: #}
{% set dynamicUri = "?foo=AnQiCMS&bar=GoLang" %}
<a href="{{ dynamicUri|iriencode }}">链接</a>

{# 渲染后的 HTML 可能会是这样的: #}
{# <a href="?foo=AnQiCMS&amp;bar=GoLang">链接</a> #}

As you can see,&was converted to&amp;. If in the URL'sActual query parametersIs needed&As a delimiter, then it should be usedurlencodeTo ensure that it is encoded as%26So that the server can correctly identify the parameters. When it is necessary to safely display the entire URI string in an HTML environment (such as an attribute value, and not hoping&Destroying the HTML structure

Related articles

How to safely escape URL parameters in AnQiCMS templates to avoid potential risks?

When building and operating a website, URL (Uniform Resource Locator) parameters play a crucial role, helping us to achieve dynamic content display, filtering, and navigation functions.However, improper handling of URL parameters may also become a major security vulnerability for websites.This article will deeply explore how to safely escape URL parameters in the AnQiCMS template to effectively avoid potential risks.### The security risks of URL parameters should not be overlooked URL parameters usually carry user input or data generated by the system, such as search keywords, category ID

2025-11-09

How to implement pagination functionality in AnQiCMS

For any website that carries a large amount of content, how to efficiently and friendly display this content is undoubtedly one of the keys to successful operation.When there are a large number of articles, products, tag pages, and other content, it is obviously unrealistic to pile them all on one page. This not only slows down the loading speed but also makes it difficult for users to find the information they need.At this time, the pagination function is particularly important. In AnQiCMS, implementing pagination is quite intuitive and flexible.It cleverly combines the acquisition of content lists with the display of pagination navigation, making the organization and presentation of website content both beautiful and efficient.###

2025-11-09

How to display custom contact information on the website frontend, such as WhatsApp or Facebook links?

In today's digital age, allowing customers to easily find and contact you is the cornerstone of any successful website.Whether it is to provide customer support, promote sales, or build community interaction, clear and visible contact information is crucial. 幸运的是,AnQi CMS provides a set of intuitive and powerful features that allow you to easily display various custom contact methods on your website front-end, such as WhatsApp or Facebook links, ensuring that your customers can always communicate with you conveniently.Next, we will step by step introduce how to achieve this goal in Anqi CMS

2025-11-09

How to set up scheduled article publishing in AnQiCMS to achieve automated content display?

In today's internet age where content is king, the continuous update and efficient operation of website content is the key to attracting and retaining users.However, manual timed release is not only time-consuming and labor-intensive, but may also miss the**release opportunity due to negligence.AnQiCMS addresses this pain point by building a convenient scheduled publishing function, which helps us easily achieve automated content display.Why choose scheduled publication to achieve content automation?Timely publishing is not only a tool to improve efficiency, but also an indispensable part of content operation strategy.The benefits it brings are obvious: *

2025-11-09

What are the differences in application scenarios between the `iriencode` filter and `urlencode` in AnQiCMS templates?

In AnQiCMS template development, URL encoding is a detail that should not be overlooked.It not only affects the validity of the link, but also is closely related to the website's search engine optimization (SEO) and user experience.AnQiCMS provides `iriencode` and `urlencode` two filters for URL encoding, although they have similar purposes, there are obvious differences in application scenarios and encoding strategies.Understanding these differences can help us control the URL structure more accurately when building websites, ensuring the robustness and friendliness of the links.

2025-11-09

When is it necessary to manually use `urlencode` or `iriencode` to escape URL parameters in AnQiCMS?

AnQiCMS with its high efficiency and customizable features, provides powerful content management capabilities for website operators.In daily content publishing and site maintenance, AnQiCMS excels especially in URL structure optimization, such as through pseudo-static configuration and automatically generating `url_token` to enhance SEO effects.However, even such an intelligent system, in certain specific scenarios, we still need to manually intervene in the escaping of URL parameters to ensure the correctness, functionality stability, and website security of the link.### AnQiCMS

2025-11-09

Does the automatically generated URL in the AnQiCMS template, such as `item.Link`, default to parameter escaping?

When developing a website using AnQiCMS, we often use variables like `{{ item.Link }}` in templates to generate links, which raises a natural question: Have these URLs automatically generated by the system, such as the link of the article detail page, the link of the category list page, etc., been automatically parameter escaped when outputting to the HTML page to ensure the correctness and security of the link?AnQiCMS's template system uses a syntax similar to Django's Pongo2 template engine

2025-11-09

How to ensure that the dynamically generated AnQiCMS query parameters (such as the search keyword `q`, filter parameters) are correctly encoded?

In Anqi CMS, the dynamic content of the website, such as the keywords `q` entered by the user through the search box, or the filtering parameters generated by clicking the filtering conditions, as well as the page number information in the pagination links, are all passed through URL query parameters.Ensure that these dynamically generated query parameters are correctly encoded, as this is crucial for the normal operation of the website, user experience, and search engine optimization (SEO).Why does dynamic query parameter encoding need to be correct?URL (Uniform Resource Locator) is an address on the internet with a strict set of standards

2025-11-09