How to split a line of text content (such as a tag string) into an array of individual words for processing in AnQiCMS?

Calendar 👁️ 75

In the practice of AnQiCMS content management, we often encounter scenarios where it is necessary to split a seemingly simple text content into smaller, more independent 'words' for fine-grained processing.For example, the document tag (Tag), keyword list, or multiple values separated by a specific symbol in a custom field.The core of this requirement lies in converting a string into an array that can be traversed and operated on individually.

AnQiCMS based on Django template engine syntax, provides us with a powerful and flexible filter (Filter) function, which can easily achieve this goal.Next, we will explore how to use these built-in tools to split a line of text content into an array of independent words and further process it.

Core Tool:splitFilter - The tool for text splitting

In AnQiCMS template, to split a string into an array by a specified delimiter,splitThe filter is our preferred tool.Its function is intuitive and efficient: it takes a string, splits it into multiple substrings based on the delimiter you provide, and returns it as an array (or list).

How to usesplitFilter?

splitThe syntax of the filter is very concise:{{ 你的字符串 | split:"分隔符" }}.

Suppose our document tag string is"AnQiCMS, 内容运营, SEO优化, 网站专家"We hope to split it into individual tags. Since the tags are separated by English commas,We can use it like thissplitFilter:

{% set tags_string = "AnQiCMS, 内容运营, SEO优化, 网站专家" %}
{% set tag_array = tags_string|split:"," %}

{# 此时,tag_array 就是一个包含 ["AnQiCMS", " 内容运营", " SEO优化", " 网站专家"] 的数组 #}

It is worth noting that when we use the comma,When used as a delimiter, the words split may retain the spaces after the commas in the original string (for example" 内容运营"To obtain a cleaner word, we can apply atrimfilter to remove leading and trailing spaces.

Shortcut:fieldsFilter - for space splitting

If you are sure that your text content is purely separated by spaces (for example"网站 运营 专家"ThenfieldsThe filter provides a more concise splitting method, it will split the string into an array by default with spaces, without explicitly specifying a delimiter.

How to usefieldsFilter?

fieldsThe usage of the filter is simpler:{{ 你的字符串 | fields }}.

{% set keyword_phrase = "网站 运营 专家" %}
{% set keyword_array = keyword_phrase|fields %}

{# 此时,keyword_array 就是一个包含 ["网站", "运营", "专家"] 的数组 #}

Split after: loop traversal and fine processing

Once we split the text content into an array, we can make full use of the powerful AnQiCMS template,forLoop tags to iterate over each 'word' in the array and perform further display or logical judgment.

{% set tags_string = "AnQiCMS, 内容运营, SEO优化, 网站专家" %}
{% set tag_array = tags_string|split:"," %}

<div class="tags-list">
    {% for tag in tag_array %}
        {# 对每个标签应用 trim 过滤器去除潜在的空格,然后展示 #}
        <span class="tag-item">{{ tag|trim }}</span>
    {% endfor %}
</div>

By such a loop, we can beautifully display the original string as an independent tag cloud or keyword list.

Advanced Application: Verification and Statistics

In actual operation, we may not only display these split words, but also need to make some logical judgments, such as checking whether a specific word is in the list or counting the number of times a word appears.AnQiCMS also provides the corresponding filters to meet these needs.

  1. Check if it contains a specific word (containFilter) containThe filter can determine whether an array contains a specific value, it will returnTrueorFalse.

    {% set tags_string = "AnQiCMS, 内容运营, SEO优化, 网站专家" %}
    {% set tag_array = tags_string|split:"," %}
    
    {% if tag_array|contain:"内容运营" %}
        <p>此文章属于“内容运营”范畴。</p>
    {% else %}
        <p>此文章不属于“内容运营”范畴。</p>
    {% endif %}
    

    Please note,containThe filter performs an exact match.

  2. Count the number of occurrences of a specific word (countFilter) countThe filter can calculate the number of times a specific value appears in an array.

    {% set keyword_string = "安企CMS,CMS,内容管理,CMS系统" %}
    {% set keyword_array = keyword_string|split:"," %}
    
    {% set cms_count = keyword_array|count:"CMS" %}
    <p>“CMS”一词在此关键词列表中出现了 {{ cms_count }} 次。</p>
    
  3. Recombine words (joinFilter)Although we aim to split, but in some cases, we may need to recombine the processed array into a string, but using different connectors.joinThe filter can achieve this function.

    {% set tag_array = ["AnQiCMS", "内容运营", "SEO优化"] %}
    {% set formatted_tags = tag_array|join:" | " %} {# 结果: "AnQiCMS | 内容运营 | SEO优化" #}
    <p>格式化后的标签:{{ formatted_tags }}</p>
    

Actual case: dynamically display article tags

Suppose we have a blog post, its tags arearchive.TagsStored in the database separated by commas. We hope to display these tags at the bottom of the article detail page and add links to each tag.

{# 假设 archive.Tags 的值是 "网站优化,搜索引擎,内容营销" #}
{% set raw_tags = archive.Tags %} {# 获取原始标签字符串 #}
{% if raw_tags %}
    {% set tag_names = raw_tags|split:"," %} {# 拆分成数组 #}
    <div class="article-tags">
        <strong>标签:</strong>
        {% for tag in tag_names %}
            {% set cleaned_tag = tag|trim %} {# 清理每个标签的首尾空格 #}
            {% if cleaned_tag %}
                {# 假设我们有一个名为 'tag_link_prefix' 的变量存储标签页面的前缀 #}
                <a href="{{ tag_link_prefix }}/{{ cleaned_tag }}" class="tag-badge">{{ cleaned_tag }}</a>
            {% endif %}
        {% endfor %}
    </div>
{% endif %}

By this method, we can flexibly split a line of text content in AnQiCMS into an independent array of words and perform various useful processing.This has enhanced the flexibility of content display and has laid a foundation for more complex business logic processing based on these "words" (such as related content recommendations, data analysis, etc.).


Frequently Asked Questions (FAQ)

**Q1:

Related articles

The `escape` and `escapejs` filters in AnQiCMS are applicable to which HTML/JS escaping scenarios?

In AnQiCMS template development, it is very important to understand and properly use escape filters to ensure website security and correct content display.The system uses a template engine syntax similar to Django, which means it takes some security measures by default when handling variable output.Today, let's talk about the `escape` and `escapejs` filters to see in which scenarios they can be used.

2025-11-08

How to quickly view the detailed structure and value of complex variables during debugging in AnQiCMS template?

During AnQiCMS template development, we often need to understand what data and structure a variable contains internally, especially when dealing with complex data objects or debugging template issues.Sometimes, direct output of a variable can only yield a simple value or error message, and cannot delve into its detailed composition.At this point, it is particularly important to master some effective methods for viewing the complete structure and value of variables.### The Challenge of AnQiCMS Template Debugging The template syntax of AnQiCMS is versatile, whether it is the system built-in `archive`

2025-11-08

In AnQiCMS template, how to determine if one number can be evenly divided by another to achieve conditional display?

In Anqi CMS template development, we often need to display content based on specific conditions, such as adding a special style to every few elements in a list or inserting separators at specific positions.When this condition is to determine whether a number can be divided by another number, Anqi CMS provides a concise and efficient solution with its powerful template engine.The Anqi CMS template system uses a syntax similar to the Django template engine, which makes it very intuitive in handling such logical judgments.To determine if a number can be evenly divided by another number

2025-11-08

What are the differences and applicable scenarios between the `default` and `default_if_none` filters when the template variable is empty?

In AnQi CMS template design, reasonably handling variables that may be empty is the key to ensuring the integrity and smooth user experience of website content display.When a template variable has no value or is in an 'empty' state, we usually do not want blank or error messages to appear on the page, but rather we would like to display a preset default content.At this time, the `default` and `default_if_none` filters provided by Anqicms come into play.They can all provide default values for variables

2025-11-08

How to precisely control the display of floating-point numbers in the AnQiCMS template, such as retaining two decimal places?

In website content operation, the way numbers are presented often affects user experience and the accuracy of information.Especially for floating-point numbers, such as product prices, statistics, and ratings, it is common to require accuracy to several decimal places or rounding according to business needs.AnQiCMS is a powerful template system that provides us with a flexible way to handle these data.Today, let's delve into how to efficiently and accurately control the display of floating-point numbers in the AnQiCMS template.

2025-11-08

How to extract specific number information from a long numeric string in AnQiCMS template?

In website operations, we often encounter scenarios where we need to handle some structured long numeric string.For example, a product code may contain the production date and batch information; an order number may imply regional codes and serial numbers; or may be a unique identifier generated by specific business logic.These long digital strings often carry rich metadata, and we may only need the numerical information at specific positions for display, filtering, or further processing.

2025-11-08

How to find the first occurrence index of a character or substring in the AnQiCMS template?

During the process of displaying website content or template development, we often encounter situations where we need to process specific text, such as checking if a keyword exists or locating the first occurrence of a character or substring.The template engine of AnQiCMS (AnQiCMS) provides a series of powerful filters (Filters) to help us complete these tasks efficiently.Today, let's discuss how to use the `index` filter to accurately find the position index of the first occurrence of a character or substring in the AnQiCMS template.Understand

2025-11-08

In AnQiCMS template, how to convert a string number into an actual `integer` or `float` type?

When working with the Anqi CMS template to display content and make logical judgments, we often encounter situations where we need to perform operations on numbers.However, data obtained from a database or content model, even if it looks like a number in the background, is sometimes passed as a string (``string``) at the template level.If this is directly performed arithmetic operations or numerical comparisons, unexpected results may occur.

2025-11-08