The Anqi CMS, with its flexible content model and powerful template system, provides great convenience for us to manage website content.In daily operations, we often encounter the need to process user-submitted form data, especially those fields that allow multiple selections, such as product specifications, article tags, or user interests, etc.These data are usually stored as a string in the background, for example, How to beautifully and independently display these collective data in front-end templates has become a problem that content operators need to face.
Fortunately, AnQi CMS comes with a built-in template engine that provides many practical filters, among whichsplitThe filter is the tool to solve such problems. It can easily split a string of a specific format into an array according to the delimiter we specify, allowing us to process and display each element individually in the template.
splitFilter: The key to breaking down collective data into individual pieces
Imagine that we have defined a multi-select field for 'Color' in the product model, and the user has checked 'Red', 'Blue', and 'Green' when submitting the form.This information is likely to be saved in the database as a comma-separated string of 'red, blue, green'.If we output this string directly in the template, it will display 'red, blue, green' on the page, which is not only unattractive but also difficult to control the style or interact with further design.
At this time,splitThe filter comes into play. Its basic function is to receive a string and a delimiter, and then return an array containing multiple substrings. For example,{{ product.colors|split:"," }}This line of code, can convert the string "red, blue, green" into an array containing["红色", "蓝色", "绿色"]Once the data becomes an array, we can use loop labels such asforTraverse each element and display it independently according to the design requirements.
Typical application scenarios
1. Checkbox (Checkbox) and Dropdown Multi-select (Multi-select Dropdown) value handling
This issplitThe most common application scenario of the filter. When we set a custom field of 'Multiple Selection' type for a content model or comment form, no matter how many items the user selects, the final string received by the background may be connected by a specific character (such as comma, semicolon, or pipe).
For example, a product detail page may need to display various characteristics of the product, such as product.featuresIn the field, separated by commas, we can display them like this in the template:
<div class="product-features">
<strong>产品特性:</strong>
{% set features = product.features|split:"," %}
{% if features %}
<ul>
{% for item in features %}
{% if item|trim %} {# 确保不显示空元素 #}
<li>{{ item|trim }}</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<span>暂无特性信息</span>
{% endif %}
</div>
In this way, each feature is independently wrapped in<li>Labels in the list make it easier for us to style list items, such as adding icons, borders, or different background colors.
2. Fine-grained display of article tags (Tags)
Although Anqicms provides built-in Tag management functionality, sometimes we may also store additional, closely related keyword lists in custom fields.For example, an article on technology may have system-level tags, and may also have a "related technology stack" field, which includes "Go language, Vue.js, MySQL", etc.
In order to display these technology stacks as independent tags instead of a long string, we can also usesplit:
<div class="tech-stack-list">
<strong>相关技术栈:</strong>
{% set tech_tags = archive.techStack|split:"、" %} {# 假设用顿号分隔 #}
{% if tech_tags %}
{% for tag in tech_tags %}
{% if tag|trim %}
<span class="tech-tag">{{ tag|trim }}</span>
{% endif %}
{% endfor %}
{% else %}
<span>暂无相关技术信息</span>
{% endif %}
</div>
Here we assume a custom fieldtechStackThe value is "Go language, Vue.js, MySQL", by passingsplit:"、"Split it, and combinespanWith tags and CSS styles, you can present an independent and beautiful tag effect.
3. User-defined list information processing
Sometimes, we may want users to enter a series of custom items in a single-line text field, such as a list of participants for an event or a list of ingredients for a recipe, using a specific symbol (such as a semicolon;)to separate.splitThe filter can also handle such non-standardized list data very well.
Suppose we have a namedingredientsThe custom field, the user entered 'flour; eggs; milk; sugar', we can display it like this:
<div class="recipe-ingredients">
<h3>所需配料:</h3>
{% set ingredients_list = page.ingredients|split:";" %}
{% if ingredients_list %}
<ol>
{% for item in ingredients_list %}
{% if item|trim %}
<li>{{ item|trim }}</li>
{% endif %}
{% endfor %}
</ol>
{% else %}
<p>暂无配料信息。</p>
{% endif %}
</div>
This processing method makes long strings easy to read and format, greatly improving the user experience.
splitCooperates with other filters and tags.
splitFilters are often not used in isolation; they are usually combined with other template tags and filters to achieve more powerful functions:
forLoop tagsThis issplitThe golden partner of filters, used for iteratingsplitThe array generated later, render each element separately.iflogical judgment tagWe often use it when traversing the array:{% if item %}To determine if the current element is empty, to avoid rendering unnecessary blank items, especially when there are consecutive delimiters in the original string (such as"A,,B") or delimiters at the beginning and end (such as",A,B,") when.trimFilterInsplitAfter, each element of the array may contain leading or trailing spaces (for example, “Red”).{{ item|trim }}These unnecessary spaces can be removed to make the display cleaner.lengthFilter: can be used to judge.splitThe array is empty to decide whether to display a certain block or display a prompt like "No data available".joinFilter: withsplitOn the contrary,joinThe array elements can be concatenated into a single string. This is useful in some scenarios, such as converting multiple user selections from an array format back to a string for display as a default value in another form.
Summary
splitThe filter is a powerful tool used in the development of Anqi CMS templates to handle multiple value form data submitted by users.It will decompose the seemingly complex string data into manageable arrays, allowing us to have precise control and flexible display over each independent data item.Whether it is a product feature list, a custom tag cloud, or