Auto CMS is an efficient enterprise-level content management system, which provides a rich set of tags and filters for template creation, helping us flexibly display content. Among them,splitFilter is a very practical tool that can split a string into an array according to a specified delimiter, which is particularly convenient in scenarios such as keyword lists and multi-value fields.
splitThe working principle of the filter and expected input
We all know,splitThe main function of the filter is to "split strings". Imagine that if you have a string likesplitFilter, combined with a comma as a delimiter, easily get an array containing "Product A
Its basic usage is usually like this:
{% set products_string = "产品A,产品B,产品C" %}
{% set products_array = products_string|split:"," %}
{# 遍历并显示产品名称 #}
{% for product in products_array %}
<li>{{ product }}</li>
{% endfor %}
From this example, we can clearly see,splitthe filter expects to receive anStringdata of this type.
IfsplitWhat happens if the input received by the filter is not a string type?
In practical operation, we may sometimes encounter some unexpected situations, such as, a variable may be a string under certain conditions, but may become a number, a boolean value, or even a null value (nilornothing)。If this is the case, what will be the behavior of passing a non-string data type tosplitthe filter?
The template engine of Anqi CMS is designed to be quite strict in operations of type mismatch.splitThe filter receives input that is not the expected string typeIt will not attempt to automatically convert this data to a string(for example, converting a number123silently into a string"123"Instead of cutting again), it will be considered as an invalid input.
In this case, your template rendering is likely tointerrupt and throw an error.This means that the page will fail to render when trying to display, and the user might see a blank page or a generic error message instead of a page with normal content.splitThe filter expects a string type, but received a message like 'XX type' to help you locate the problem.
This error occurs to ensure the clarity and security of data processing by the template engine, avoiding speculative operations when the data type is uncertain, which may lead to unpredictable output results.
How to avoid errors caused by non-string input?
To ensure the stable operation and smooth user experience of the website, we usesplitFiltering, there are several tips that can help us avoid such problems:
Set default values:If you are unsure whether a variable is always of string type or may be null in some cases, you can first use
defaulta filter to set a safe default string value.{% set maybe_string = some_data|default:"" %} {# 如果some_data不是字符串或为空,则默认为空字符串 #} {% set parts = maybe_string|split:"," %}Even though this
some_dataIt is not a string or is empty,splitThe filter always receives a string (empty string), thus avoiding errors.Explicitly convert to a string:If you indeed need to split a non-string type (such as a number) according to string rules, you can first use
stringformata filter to explicitly convert it to a string.{% set number_value = 12345 %} {% set string_value = number_value|stringformat:"%s" %} {# 将数字转换为字符串"12345" #} {% set digits_array = string_value|split:"" %} {# 按空分隔符拆分,得到["1","2","3","4","5"] #}Use
ifConditional judgment:In complex scenarios, you can also useifcondition judgment to ensuresplitthe filter only executes when the variable is of the expected type.{% if my_variable is defined and my_variable is not empty %} {# 伪代码,实际模板引擎可能不直接支持is_string #} {% set result = my_variable|split:"," %} {% else %} {% set result = [] %} {# 或者设置其他默认数组 #} {% endif %}Although there is no direct in the safety CMS template engine
is_stringFilter to determine the variable type, but bydefaultorstringformatwe can achieve the same effect, ensuring that thesplitis always a string.
In short,splitThe filter is a powerful and intuitive string processing tool in the Anqi CMS template, but it requires the input to be strictly of string type.Understand this and pay a little attention when using it, and you can effectively avoid unnecessary template rendering errors, making your website content display more stable and reliable.
Common Questions (FAQ)
1. I want to take a number (such as12345)Split into an array of individual numeric characters (e.g.,)["1", "2", "3", "4", "5"])splitCan the filter do that directly?
Use directlysplitThe filter will report an error when processing numbers.splitThe filter expects a string input. To achieve this effect, you can first convert the number to a string and then usesplitFilter, split strings using empty string as delimiter. For example:{{ 12345|stringformat:"%s"|split:""|join:", " }}.
2. If I have a variable that might be empty (nil), pass it directly tosplitWhat will the filter do?
If the variable isnilor undefined, pass it directly tosplitFilter, it will also trigger a template rendering error becausenilis not a string type. To avoid this situation, it is recommended to usesplitbeforedefaultFilter provides an empty string as the default value, for example:{{ my_nullable_variable|default:""|split:"," }}. Even thoughmy_nullable_variableThe content inside the loop will not be rendered, instead, the content of the block will be displayed.splitthe empty string received is also a manageable one, and no error will occur.
3. Where should I look for more detailed error information when there is an error in template rendering?
When the template rendering fails and a general error is displayed, the most detailed error information is usually recorded in the server's runtime log. For security CMS deployed on Baota panel or Docker, you can check the server on the/www/wwwroot/您的域名/running.logA file or similar path, or a Docker container's log. These logs will provide specific line numbers and error types to help you quickly locate them.splitFilter received non-string input on which variable.