In website content management, we often encounter a situation where a field stores a string of data separated by a specific symbol, and we need to perform different operations based on the results after the data is split.For example, an article tag field may store 'SEO, operation, content marketing', or a product attribute field may store 'Color: red, Size: L'.splitfilters andifLogical judgment label, when used together, these two can very flexibly meet this requirement.

KnowsplitFilter: the sword for splitting strings

splitThe filter is a very practical tool used for string processing in the Anqi CMS template.It is mainly used to split a string into an array using a specified delimiter (commonly referred to as 'slicing' or 'slice' in programming languages).

Its basic usage is very intuitive:

{{ 你的字符串变量|split:"分隔符" }}

For example, if your article has aTagsfield, storing the content of"安企CMS,教程,模板":

{% set tag_string = archive.Tags %}
{% set tag_array = tag_string|split:"," %}
{# 此时 tag_array 会是一个包含 ["安企CMS", "教程", "模板"] 的数组 #}

It is worth noting that if the specified delimiter does not exist in the string,splitThe filter returns an array containing the original string as the only element.If the delimiter is an empty string, it will split the string into an array based on each character (including Chinese characters).

MasterifLogic judgment label: the core of conditional branches

ifLogic judgment tags are the foundation of our conditional control, which can determine whether to execute specific template code blocks based on the truth or falsity of the expression. Combinedelif(else if) andelseWe can build complex logical judgment processes.

The basic syntax structure is as follows:

{% if 条件1 %}
    {# 当条件1为真时执行的代码 #}
{% elif 条件2 %}
    {# 当条件1为假且条件2为真时执行的代码 #}
{% else %}
    {# 当所有条件都为假时执行的代码 #}
{% endif %}

The conditional expression can be variables, comparison operations (such as)==/>/</!=), logical operations (such as)and/or/not)or other expressions returning a boolean value (true/false).

splitWithifThe strong combination of: execute different operations according to the split results.

Now, let's see how to convertsplitFilter is related toifLogic judgment tag combination, achieving more refined content display. The key is,splitThe filter returns an array, we can use various properties of this array (such as length, whether it contains specific elements, etc.) for conditional judgment.

Scene one: Perform different operations based on the number of split elements

Suppose we have a custom fieldproduct.Benefitswhich stores the main advantages of the product, such as"高效,安全,稳定". We might want:

  • If there is only one advantage, display it in bold font.
  • If there are multiple advantages, display them in a list format.
  • If there are no advantages, display 'No features available.'
{% set benefits_str = product.Benefits|default:'' %} {# 先获取字段值,并设置默认空字符串以防nil #}

{%- if benefits_str|trim != '' %} {# 首先判断字符串是否有效,避免只有空格或空字符串导致误判 #}
    {% set benefits_array = benefits_str|split:"," %}

    {%- if benefits_array|length == 1 %}
        <p class="highlight-benefit">✨ {{ benefits_array[0]|trim }}</p>
    {%- elif benefits_array|length > 1 %}
        <ul class="benefits-list">
        {%- for benefit in benefits_array %}
            <li>✅ {{ benefit|trim }}</li>
        {%- endfor %}
        </ul>
    {%- else %} {# 理论上,如果benefits_str不为空,这里不会被触发,除非split返回空数组,但安企CMS的split不会 #}
        <p>暂无特色。</p>
    {%- endif %}
{%- else %}
    <p>暂无特色。</p>
{%- endif %}

In the above code, we first usetrimFilter clearedbenefits_strLeading and trailing whitespace, then check if it is empty. Only when it is not empty, do the operation.splitNext, we go throughbenefits_array|lengthto get the length of the array, and combineif/elif/elseto determine different display methods.

Scene two: Perform different operations based on whether the split contains a specific element

Assume a list page of articles, each article may have aarticle.StatusField, the content is"置顶,精选"or"精选"Or empty. We want:

  • If the article is “top”, display a special icon before the title.
  • If the article is “selected”, add an eye-catching style to the title.

We can utilizesplitSplit the status string into an array and then usecontaina filter to check if a specific status exists in the array.

{% for article in archives %}
    {% set status_str = article.Status|default:'' %}
    {% set status_array = status_str|split:"," %} {# 拆分状态字符串 #}

    <div class="article-item">
        {%- if status_array|contain:"置顶" %} {# 判断数组是否包含“置顶” #}
            <span class="icon-top">🔝</span>
        {%- endif %}

        <h3 class="article-title {% if status_array|contain:"精选" %}featured{% endif %}"> {# 判断是否包含“精选”,并添加样式 #}
            <a href="{{ article.Link }}">{{ article.Title }}</a>
        </h3>

        <p class="article-description">{{ article.Description }}</p>
        {# ... 其他文章内容 #}
    </div>
{% endfor %}

In this example, we perform an operation on theStatusfield for each article.splitand getstatus_arrayThen, twice usingcontainFilter (it checks if the array contains a specified value and returns a boolean value), respectively to judge whether it contains "Top" and "Featured", so as to control the display of icons and the style of the title.

Scene three: Handling complex data structures, extracting and judging key information

Sometimes, the data structure within strings can be more complex, such as product configuration fieldsproduct.Configurationmay be"CPU:i7-12700,RAM:16GB,SSD:512GB"We may need to extract the value of a specific configuration and make a judgment based on its value.

auto

{%- for item in items %}