In the template development of Anqi CMS,splitFilter is a very practical tool, which can help us quickly split strings of specific formats into arrays. For example, we often store keywords, tags, or other attributes of articles in a field in the form of comma-separated, and then display them when neededsplitFiltering is performed.
However, when usingsplitThe filter function may encounter a common situation that can be frustrating: that is, the resulting array may contain empty string elements.This usually occurs when there are consecutive delimiters in the original string (such as “keyword1,,keyword2”), or the string starts or ends with a delimiter (such as “,keyword1” or “keyword2,“).These empty strings are not only unattractive when displayed on the page, but may also cause logical errors in data processing.
How can we elegantly filter out these empty string elements to ensure that we only get a clean and valid array?The powerful Django template engine syntax of AnQi CMS provides flexible solutions.
Identify the problem: Why is there an empty string?
Imagine you have an 'associated words' field in an article, the content is“安企CMS,内容运营,,模板制作,SEO,”. When you try to use|split:","the filter to split it:
{% set raw_string = "安企CMS,内容运营,,模板制作,SEO," %}
{% set parts = raw_string|split:"," %}
{% comment %} 此时的 parts 数组可能是 ["安企CMS", "内容运营", "", "模板制作", "SEO", ""] {% endcomment %}
You will find, due to内容运营and模板制作There are two commas between them, as well as a comma at the end of the string, which causes two empty strings to appear in the array""The direct traversal and display of this array may result in excessive empty content or separators on the page, affecting user experience.
Solution: Combine withforLoop withifJudgment
In the templates of AnQi CMS, although there is no single filter for 'filtering empty elements' directly for arrays, but we canforloop andifThe combination of logical judgment, easily achieve this goal.
The core idea is: first usesplitThe filter splits the string into the original array, then traverses this array and checks each element, only processing those that are not empty.
Below is a specific code example demonstrating how to operate:
{# 假设我们有一个变量 `archive.Keywords`,其值为 "安企CMS,内容运营,,模板制作,SEO," #}
{% set raw_keywords = archive.Keywords %}
{# 第一步:使用 split 过滤器将字符串切割成数组 #}
{% set keyword_list = raw_keywords|split:"," %}
<div class="keyword-tags">
{% for keyword in keyword_list %}
{# 第二步:在循环中判断当前元素是否为空字符串 #}
{% if keyword %}
{# 如果 keyword 不是空字符串,则进行展示或进一步处理 #}
<span class="tag">{{ keyword|trim }}</span>
{% endif %}
{% endfor %}
</div>
In this example:
- We first convert
archive.Keywordsthe value through|split:","a filter to cut into a namedkeyword_listusing a comma as a delimiter. - Next, we use
{% for keyword in keyword_list %}tag to iterate over this array. - The most critical step is
{% if keyword %}. In Django template syntax, values like an empty string""/0/falseornilare always evaluated as “false” (False). Therefore,if{% if keyword %}This line of code can effectively filter out all empty string elements, only whenkeywordthe variable has actual content, the code block inside it will be executed. - I also added an extra
|trimFilter. The benefit of this is that, if there are extra spaces before or after a keyword in the original string (for example" 模板制作 ")trimThe filter can automatically remove these spaces, making the display cleaner.
Through such a combination, we can ensure that only valid keywords are displayed on the page, avoiding display issues caused by empty strings. This method is both intuitive and flexible, capable of dealing with most processing needssplitFilter results scenario.
Summary
When you use in AQB CMS,splitThe most direct and effective method is to use when filtering a string into an array and hoping to exclude any empty strings that may exist.forLoop through and split the array, and within the loop, useifto filter non-empty elements. This not only ensures the accurate display of content but also enhances the robustness of template data processing.
Common Questions and Answers (FAQ)
Q1: If my original string contains spaces between delimiters (for example, "keyword1 , keyword2"),splitthen usetrimcan the filter handle?A1: Yes. When you use|split:","Splitting, if the element is like" 关键词2"such strings with leading or trailing spaces, combined with|trimfilters (for example{{ keyword|trim }})can effectively remove these extra spaces, ensuring that the final displayed keywords are clean.
Q2: Besides an empty string, how can I filter out elements with specific content (such as 'meaningless')?A2: The same logic can be applied, you can inifAdd more specific conditions in the judgment. For example, if you want to exclude elements with the value 'meaningless', you can write it like this:{% if keyword and keyword != "无意义" %}. You can also combine|loweror|upperFilter performs case-insensitive matching, for example{% if keyword and keyword|lower != "无意义" %}.
Q3: Is there a way tosplitremove consecutive delimiters before, thus reducing the generation of empty strings?A3: In some cases, you can try to pre-process the original string. For example, you can usesplitbefore.|replaceThe filter replaces consecutive delimiters with a single delimiter. For example,"关键词1,,,关键词2"|replace:",,,":","|replace:",,":","You can replace multiple commas with a single comma. But please note,replaceIt may increase the complexity of template processing, usually, it is direct inforused in the loopifFiltering empty strings is already efficient and concise enough.