In the template development of Anqi CMS,replaceThe filter is a very flexible string processing tool that can help users quickly replace specific strings in text content.However, when its application scenarios involve dealing with URL (Uniform Resource Locator) parameters, we need to invest more thought and caution, because URL parameters have their own unique structure and encoding specifications.

KnowreplaceBasic functions of the filter

Firstly, let's review.replaceFilter basic usage. Its syntax structure is usually{{ 字符串变量|replace:"旧字符串,新字符串" }}. It will find all matching parts of the string "old string" and replace them with "new string".

For example, if you have an array namedtitlewith a value of"欢迎使用安企CMS内容管理系统"and want to replace 'AnQi' with 'AnQi', you can write it like this:

{{ title|replace:"安企,AnQi" }}
{# 显示结果: 欢迎使用AnQiCMS内容管理系统 #}

Here, special attention is needed.replaceThe two special behaviors of the filter:

  1. When the 'old string' is empty:replaceThe filter will exhibit unusual behavior. It will insert the 'new string' between each UTF-8 character of the original string. For example,{{ "ABC"|replace:",-" }}It will outputA-B-C-This feature may be useful in general string processing, but is almost always disastrous in URLs.
  2. When the 'new string' is empty: At this timereplaceThe filter will directly remove the matched 'old string' from the original string. For example,{{ "欢迎使用安企CMS"|replace:"安企," }}It will output欢迎CMS.

The specificity of URL and its parameters

The composition and parameter passing methods of URLs are strictly standardized. A complete URL usually includes the protocol, domain name, path, and query parameters. The query parameter part is preceded by a question mark (?)start,the parameters are separated by and signs&)are connected,each parameter is composed of a key (key) and a value (value) connected by an equal sign=)connected.

It is more critical that certain characters in the URL (such as spaces,&/?/=etc.) must be URL-encoded when transmitted as parameter values, converting them into%XXof the form, to avoid confusion with URL structure separators. For example, spaces will be encoded as%20or+,and ampersand&will be encoded as%26.

in handling URL parameters,replacerisks may bring from filters

Due toreplaceThe filter performs matching and replacement based on a common string, it does not know which part of the URL it is processing, so it is directly used to modify the URL string, especially when it contains parameters, which can easily introduce problems:

  1. Destroy the URL structureIf you try to replace the key separators in the URL directly, the structure of the URL will be immediately destroyed, causing the page to fail to parse correctly. For example, replacing the URL with?/&/=will immediately break the structure of the URL, causing the page to fail to parse correctly. For example, replacing the URL withsearch?q=cmsof?Replace it with other characters, and the entire query parameter will become invalid.
  2. Incorrect replacement of URL-encoded characters: If the value of the URL parameter has already been URL-encoded (for example, replacing spaces with %20),you try usingreplacefilter to replace one of the original characters (for example, to%20with-),it may not match the expected character, or may cause incorrect secondary encoding.
  3. The danger of the old stringAs mentioned before, ifreplaceFilters in the URL string withreplace:",-"This method uses, it will insert hyphens between each character in the URL. Imagine that,{{ "/search?q=cms"|replace:",-" }}This operation will generate a completely unrecognizable link, resulting in a 404 error.

**Practice: How to Use SafelyreplaceFiltering URL Parameters

To avoid the above risks, when we want to usereplaceThe filter should follow the following principles and steps when modifying URL parameters:

  1. Explicitly replace the target: only modify the parameter "value".replaceThe safest usage of the filter is to apply it to URL parametersValueInstead of the URL structure part or the entire URL string.This means you should first obtain the original value of the parameter, perform the replacement operation, and then reassemble the replaced value into the URL.

  2. Replace it after the replacement must be URL encoded. Before the parameter value isreplaceoperated, if this value is eventually to be transmitted as part of a URL,Be sure toPerform URL encoding. The Anqi CMS providesurlencodeandiriencodefilterers to complete this task.

    • urlencodeThe filter will encode all special characters in the string, including?/&/=It is usually used to encode the entire query string or individual parameter values.
    • iriencodeThe filter is relatively mild, as it retains some structural characters in the URL (such as:/