How to combine `split` filter with `urlencode` filter to handle multi-value strings in URL parameters?

Calendar 👁️ 64

In the powerful template system of Anqi CMS, flexibly handling and displaying website data is the key to improving user experience and SEO effectiveness.Among them, converting a specific format string stored in the database into a secure and usable multivalue string in the URL parameters is a common requirement.splitandurlencodeFilter, solve this practical problem.

Understanding core tools:splitandurlencodeFilter

In AnQiCMS template language, filters are powerful assistants for data processing.They allow us to format, convert, or clean data directly at the template level without modifying the backend code.

splitFilter: A tool for turning whole numbers into zeros

Imagine that you might be in the background content management, to facilitate batch entry or management, storing multiple tags of an article (such as:When you want to generate a separate search link for each tag on the front-end page, it is obviously not feasible to use this string containing multiple values.

At this time,splitThe filter comes into play. Its function is to split a string into an array (or list) according to the delimiter you specify.

Basic usage example:If you have a string"TagA, TagB,TagC"and you want to split it:

{% set tagString = "TagA, TagB,TagC" %}
{% set tagArray = tagString|split:"," %} {# 注意,这里我们用逗号作为分隔符 #}

{# 此时 tagArray 的内容大致是 ["TagA", " TagB", "TagC"] #}

BysplitFilter, we successfully transformed a string containing multiple values into individual elements that can be independently operated on.

urlencodeFilter: Ensure the completeness and safety of the link

Once you split the string into individual elements, the next step is to embed them as parameters in the URL.However, URLs have strict restrictions on characters.&/?/=If it appears directly in the URL, it may disrupt the structure of the URL, causing the link to fail and even trigger security issues.

urlencodeThe filter's role is to perform URL encoding on strings, converting all unsafe characters to percent encoding (such as spaces becoming%20Ensure the validity and security of the URL. This is an essential step when constructing dynamic URLs.

Basic usage example:If you have a string that may contain spaces or special characters, you need to put it in the URL parameters:

{% set keyword = "安企CMS 教程" %}
{% set encodedKeyword = keyword|urlencode %} {# 此时 encodedKeyword 的内容大致是 "安企CMS%20教程" #}

{# 如果直接使用:<a href="/search?q=安企CMS 教程">...</a> 链接会出问题 #}
{# 编码后使用:<a href="/search?q={{ encodedKeyword }}">...</a> 链接是安全的 #}

Combine cleverly: handle multiple-value strings in URL parameters:

Now, we have the ability to split multi-value stringssplitand ensure URL safetyurlencode. Let's demonstrate how they work together in a real-world scenario.

Assuming your AnQiCMS website has a custom field calledarticle_keywordsWhere it stores multiple keywords of the article, separated by commas, for example: 'website optimization, SEO skills, traffic increase'.You hope to generate a separate search link for each keyword below the article detail page, clicking on which can search for all articles within the site that contain the keyword.

Here is the template code to implement this feature:

{# 假设当前在文章详情页,并且文章有一个名为 'article_keywords' 的自定义字段 #}
{% archiveDetail keywordsString with name="article_keywords" %}

{% if keywordsString %}
    <div class="article-tags-section">
        <span class="section-label">相关搜索:</span>
        {# 将逗号分隔的关键词字符串切割成数组 #}
        {% set keywordList = keywordsString|split:"," %}
        
        {% for keyword in keywordList %}
            {# 对每个关键词进行前后空格去除,因为用户输入时可能有多余空格 #}
            {% set cleanKeyword = keyword|trim %}
            
            {# 确保关键词不为空(例如,如果有",,"这样的输入,split后可能会产生空字符串) #}
            {% if cleanKeyword %}
                {# 对清理后的关键词进行 URL 编码,然后构建搜索链接 #}
                <a href="/search?q={{ cleanKeyword|urlencode }}" class="tag-link">
                    {{ cleanKeyword }}
                </a>
            {% endif %}
        {% endfor %}
    </div>
{% endif %}

Code analysis:

  1. {% archiveDetail keywordsString with name="article_keywords" %}: This line of code retrieves the field namedarticle_keywordsCustom field value and store it inkeywordsStringthe variable.
  2. {% set keywordList = keywordsString|split:"," %}:splitFilter appears. It willkeywordsString(For example: "Website optimization,SEO skills,traffic increase") split by comma,Split to generate an arraykeywordListincluding["网站优化", " SEO技巧", "流量提升"].
  3. {% for keyword in keywordList %}we traverse this array, processing each keyword.
  4. {% set cleanKeyword = keyword|trim %}introducingtrimA filter that removes extra spaces from both ends of a string. This is an important step because users may getsplitafter entering" SEO技巧"Such a string with leading spaces, direct encoding will make the search results inaccurate.trimEnsures the purity of the keywords.
  5. {% if cleanKeyword %}This is a simple conditional judgment, used to skip empty keywords that may be caused by consecutive commas (such as
  6. <a href="/search?q={{ cleanKeyword|urlencode }}" class="tag-link"> {{ cleanKeyword }} </a>Finally, we useurlencodeThe filter has cleaned upcleanKeywordEncoded, then add it as aqparameter to/searchthe path, building a complete, secure and usable search link.

By combining the above, the originally difficult-to-directly-utilize stringThis handling method not only makes the display of dynamic content more flexible, but also greatly improves the user experience and robustness of internal links on the website.

Summary

In AnQiCMS template,splitandurlencodeThe filter is a golden combination for handling multi-value strings and safely constructing URL parameters.splitResponsible for decomposing complex data,trimEnsure data purity,urlencodeThen clothe the data with a URL-safe 'disguise'. Mastering their combination can make your website content more dynamic and interaction more smooth,

Related articles

What are the special requirements for handling delimiters in the `split` filter across multilingual content (such as multilingual tags)?

In AnQi CMS, with the increasing trend of website globalization operations, we often encounter situations where we need to deal with multilingual content.Among them, the management of document tags (Tag) is a typical example.In order to better organize and display these tags, the application of the `split` filter in the template is particularly important.It can help us convert label data stored as strings into a traversable list.However, in the context of cross-language content, the handling of delimiters by the `split` filter is not always intuitive, and this requires our special attention.

2025-11-08

What is the recommended method to debug the splitting result of the `split` filter in the template?

In Anqi CMS template development, flexible string handling is an essential part of content presentation.The `split` filter is a powerful tool that can split strings of a specific format into arrays according to a specified delimiter, which is particularly useful in scenarios such as handling article tags and multi-value fields.

2025-11-08

How to use the `split` filter in a custom content model to display and process multi-value fields?

In AnQi CMS, the custom content model provides us with great flexibility, allowing us to build personalized content structures according to different business needs.Whether it is the feature list of the product detail page, the keyword tags of the article, or the advantages of the service introduction, we often encounter the need to store multiple related information in a field and display or process these information on the front-end page in a distributed manner.In this case, if all the information is stuffed into a common text field, it may face difficulties in parsing and inconsistent styling when displayed on the front-end

2025-11-08

The `split` filter splits array elements. If a numerical operation needs to be performed, should the type conversion be done first?

In AnQi CMS template development, flexible data handling is the key to building rich pages.The `split` filter is undoubtedly a powerful tool for processing string data, it can split a string into an array based on a specified delimiter.However, when the elements cut out of the array are essentially numbers and need to perform arithmetic operations such as addition, subtraction, multiplication, and division, a common problem arises: do these elements need to be explicitly typecast?Today, let's delve deep into this issue.

2025-11-08

Does the AnQiCMS template have a direct method to remove duplicate elements from an array split by the `split` filter?

In AnQi CMS template development, we often use various filters (filters) to process and handle data to meet the needs of front-end display.Among them, the `split` filter is undoubtedly a very practical tool that can help us split strings of specific formats into arrays, such as converting comma-separated tag strings into a list of tags.However, when these sliced arrays contain duplicate elements, many friends will naturally think of: 'Does AnQiCMS template support a direct method to remove these duplicate elements?' Today

2025-11-08

How to sort the array split by the `split` filter according to custom rules?

In website operations, we often need to handle various data, sometimes these data are stored in a string in a specific format.AnQiCMS (AnQiCMS) provides powerful template tags and filters, making content display flexible and efficient.Among them, the `split` filter is a very practical tool that can split a string into an array according to a specified delimiter, making it convenient for us to traverse and display the data further.However, when we split the string into an array, we sometimes encounter the need to sort these array elements.

2025-11-08

Does the `split` filter apply to extracting specific attribute values from HTML content, such as `data-items="item1|item2"`?

In AnQi CMS template development, we often encounter situations where we need to handle different types of data.When it comes to extracting specific attribute values from HTML content, such as `data-items="item1|item2"`, and you want to further process these values, the `split` filter is a very useful tool.However, its applicability is not directly aimed at HTML parsing, but rather at the **string data already obtained**.###

2025-11-08

What error message or default behavior will occur if the input received by the `split` filter is not a string type?

Anqi CMS is an efficient enterprise-level content management system that provides a rich set of tags and filters for template creation, helping us to flexibly display content.Among them, the `split` filter is a very practical tool that can split a string into an array according to a specified delimiter, which is particularly convenient in handling scenarios such as keyword lists, multi-value fields, etc. ### `split` filter's working principle and expected input We all know that the main function of the `split` filter is to "split strings".Imagine that

2025-11-08