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.
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.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>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: