In this case, if all the information is stuffed into a common text field at once, it may face difficulties in parsing and inconsistent styles when displayed on the front-end. And the template filters provided by Anqi CMS, especiallysplitFilter, it is the tool that solves this challenge.It can help us easily split multi-value string data stored in a single field into independently operable data items, thereby achieving more refined and beautiful data display.
Understanding Custom Content Models and Multi-value Fields
Firstly, let's clarify what a custom content model is and what the meaning of 'multi-value field' is here.One of the core strengths of AnQi CMS is its flexible content model functionality.Through the "Content Management" -> "Content Model
When we mention "multi-value field", it usually refers to such a custom field: when we enter content in the background, we input multiple related values in this field, and use a specific symbol (such as comma, semicolon, pipe, etc.) to separate them.For example, in the "ProductWhen entering data, the user may input a string such asAlthough it is a single string in the database, we want it to be presented as independent list items such as 'Waterproof', 'Vibration Resistance', etc. in the frontend.
splitFilter: The Powerful Assistant for Data Processing
splitThe core function of the filter, as the name implies, is to split a string into an array (usually called slice in Go) according to the specified delimiter. Once the string is split into an array, we can use the loop label in the template (forLoop) Traverse and display each element of the array individually.
Its basic usage syntax is very intuitive:
{{ obj|split:"分隔符" }}
Among them,objis the string variable to be cut."分隔符"Then it is used to specify the specific character or string used to split the string (such as comma “,”, semicolon “;” and so on). If the specified delimiter does not exist in the string,splitThe filter will return an array that only contains the original string itself.If the delimiter is an empty string, it will split each UTF-8 character in the original string into an array as a separate element.
实战:在自定义模型字段中应用 EnglishsplitFilter
现在,让我们通过一个实际的例子,看看如何在自定义内容模型中利用 Englishsplit过滤器来展示多值字段数据。 English
假设我们有一个名为“产品”的自定义内容模型,其中包含一个自定义字段叫做Englishfeatures(Product features),its type is “Single-line text”,input by comma in the background,separated,for example:高性能CPU,大容量内存,SSD硬盘,独立显卡We hope to display these features in a beautiful list format on the product details page.
Step 1: Add a multi-value field to the custom model.
In the Anqi CMS backend, go to 'Content Management' -> 'Content Model', select your 'Product' model (or any custom model you have created), and click 'Edit'. In the 'Content Model Custom Fields' section, add a new field:
- Parameter name:Product Features (Display Name on Frontend)
- Field call:
features(Field Name Used in Template) - Field Type:Single Line Text
- Mandatory:Select According to Requirement
- Default value:Can Be Empty or Set Default Separator Prompt
Save your custom model settings.After publishing product content, find the "Product Features" field under "Other Parameters" and enter it in the format of
Step Two: Call in Frontend TemplatesplitFilter display
In your product details page template (such as)product/detail.html),you can call and handle it in the following way:featuresFields:
{# 假设我们已经通过 archiveDetail 获取了当前产品的详情 #}
{# 首先,获取自定义字段 'features' 的值 #}
{% archiveDetail productFeaturesStr with name="features" %}
{# 检查 productFeaturesStr 是否有值,避免对空字符串进行操作 #}
{% if productFeaturesStr %}
{# 使用 split 过滤器将字符串按逗号切割成数组,并赋值给一个新的变量 'featureList' #}
{% set featureList = productFeaturesStr|split:"," %}
<h2>产品特性</h2>
<ul class="product-features">
{# 遍历切割后的 featureList 数组 #}
{% for feature in featureList %}
{# 对每个特性项进行 trim 处理,去除可能存在的首尾空格,然后显示 #}
<li>{{ feature|trim }}</li>
{% empty %}
{# 如果 featureList 为空(例如原始字段为空或切割后没有有效项),则显示提示信息 #}
<li>暂无特性信息。</li>
{% endfor %}
</ul>
{% else %}
{# 如果整个 'features' 字段都没有值,则不显示产品特性部分 #}
<p>该产品暂未添加任何特性。</p>
{% endif %}
Code analysis:
{% archiveDetail productFeaturesStr with name="features" %}The first line extracts the content of the custom field namedfeaturesand assigns it toproductFeaturesStra variable.{% if productFeaturesStr %}This is a conditional judgment, ensuring that only whenproductFeaturesStrthere is content, the subsequent cutting and traversal will be executed, avoiding unnecessary