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 of the data being split.For example, an article tag field may store "SEO, operation, content marketing", or a product attribute field may store "color: red, size: L".The AnQi CMS template engine provides powerfulsplitFilters andifLogic judgment label, used together, it can very flexibly realize this requirement.
Get to knowsplitFilter: the tool for splitting strings
splitThe filter is a very practical tool used for string processing in the Anqi CMS template.The primary function is to split a string into an array using a specified delimiter (commonly referred to as 'slicing' or 'slice' in programming languages).
Its basic usage method is very intuitive:
{{ 你的字符串变量|split:"分隔符" }}
For example, if your article has oneTagsfield, the content is stored for"安企CMS,教程,模板":
{% set tag_string = archive.Tags %}
{% set tag_array = tag_string|split:"," %}
{# 此时 tag_array 会是一个包含 ["安企CMS", "教程", "模板"] 的数组 #}
It is noteworthy that if the specified delimiter does not exist in the string,splitThe filter will return 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 by each character (including Chinese characters).
MasterifLogical judgment label: the core of conditional branching
ifLogical judgment tags are the basis for conditional control, which can decide whether to execute a specific template code block according to the truth value 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 %}
Conditional expressions can be variables, comparison operations (such as==/>/</!=), logical operations (such asand/or/notOr other expressions that return boolean values (true/false).
splitwithifCombined strength: execute different operations according to the split results
Now, let's see how to convertsplitthe filter meetsifCombine logical judgment tags to achieve 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.) to make conditional judgments.
Scenario one: Perform different operations based on the number of split elements
Suppose we have a custom fieldproduct.Benefits, storing the main advantages of the product, such as"高效,安全,稳定". We might want to:
- If there is only one advantage, display it in bold letters.
- 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 code above, we first usetrimFilter clearedbenefits_strTrailing whitespace characters, then check if it is empty. Only when it is not empty, do the executionsplitOperation. Then, we getbenefits_array|lengthThe length of the array, and combineif/elif/elseTo determine different display methods.
Scenario two: Perform different operations based on whether the split contains a specific element
Assuming an article list page, each article may have onearticle.StatusField, content is"置顶,精选"or"精选"Or empty. We hope:
- 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 make use ofsplitSplit 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 operations on theStatusfield of each article'ssplitto getstatus_array. Then, twicecontainA filter (it can check if an array contains a specified value and returns a boolean value), it determines whether it contains 'Top' and 'Featured' to control the icon display and title style.
Scene three: Handling complex data structures, extracting and judging key information
Sometimes, the data structure in strings can be more complex, such as product configuration fieldsproduct.ConfigurationIt could 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.
`twig {% set config_str = product.Configuration|default:” %} {% set config_items = config_str|split:“,” %} {# Split into [“CPU:i7-12700”, “RAM:16GB”, “SSD:512GB”] #} {% set cpu_model = “ %}
{%- for item