How to get the actual character length of a string (including Chinese) in Anqi CMS template?

Calendar 👁️ 88

When creating website templates, we often need to control the display length of text to ensure that the page layout is beautiful and information delivery is efficient.For example, limit the number of characters in the article summary, ensure that the text in the navigation menu is not too long, or perform character count verification before form submission.lengthIt can help us easily get the actual character length of a string, array, or key-value pair, and it supports Chinese well.This means that, whether it is English, numbers, or Chinese characters, it counts each character as a unit of length.

Core Tool:lengthFilter

When you need to know the actual length of any string, array, or key-value pair,lengthThe filter is your first choice.It can accurately count the number of characters or elements contained in a variable.lengthThe filter treats Chinese characters as single character lengths when processing, which greatly simplifies the development of websites with multilingual or Chinese content, allowing you to worry less about length calculation issues caused by character encoding.

The method of use is very intuitive, just add it after the variable you want to get the length of|lengthand it is done:

{{ 变量 | length }}

Example demonstration:

Let's see some specific examples to understandlengthHow the filter works:

{# 获取英文字符串长度 #}
<p>英文字符串 "Hello AnQiCMS" 的长度是:{{ "Hello AnQiCMS" | length }}</p> {# 输出: 13 #}

{# 获取中文字符串长度 #}
<p>中文字符串 "你好安企CMS" 的长度是:{{ "你好安企CMS" | length }}</p> {# 输出: 6 #}

{# 获取中英文混合字符串长度 #}
<p>中英文混合字符串 "Hello 你好 AnQiCMS" 的长度是:{{ "Hello 你好 AnQiCMS" | length }}</p> {# 输出: 14 #}

{# 获取数字字符串长度(按字符) #}
<p>数字字符串 "12345" 的长度是:{{ "12345" | length }}</p> {# 输出: 5 #}

{# 获取数组(slice)的元素数量 #}
{% set my_list = ["apple", "banana", "orange"] %}
<p>数组中的元素数量是:{{ my_list | length }}</p> {# 输出: 3 #}

{# 获取键值对(map)的键数量 #}
{% set my_map = {"name": "AnQiCMS", "version": "3.x"} %}
<p>键值对中的键数量是:{{ my_map | length }}</p> {# 输出: 2 #}

You can see from the above examplelengthThe filter can accurately and conveniently return the actual length of different types of data, whether it is a single character, Chinese character, or array element.

Advanced usage:length_isFilter

In some cases, you may need to not only get the length but also judge whether the length of a string is equal to a specific value. At this time,length_isThe filter can be used. It compares the length of the variable with the specified value and returns a boolean value (TrueorFalse)

The usage is as follows:

{{ 变量 | length_is: 期望长度 }}

Example demonstration:

Combinelength_isFilters and conditionals can implement more flexible template logic:

{# 判断字符串长度是否为特定值 #}
<p>"AnQiCMS" 的长度是否为 7:{{ "AnQiCMS" | length_is: 7 }}</p> {# 输出: True #}
<p>"AnQiCMS" 的长度是否为 8:{{ "AnQiCMS" | length_is: 8 }}</p> {# 输出: False #}

{# 判断中文字符串长度 #}
<p>"安企CMS" 的长度是否为 6:{{ "安企CMS" | length_is: 6 }}</p> {# 输出: True #}

{# 结合 if 语句使用 #}
{% set site_name = "我的安企网站" %}
{% if site_name | length_is: 6 %}
  <p>网站名称 "我的安企网站" 长度恰好为6个字符。</p>
{% else %}
  <p>网站名称 "我的安企网站" 长度不是6个字符。</p>
{% endif %}

Application scenarios in practice

lengthandlength_isThese length filters are widely used in template design. For example, when displaying article summaries on list pages, you can combinetruncatecharsFilters andlengthA filter to ensure that the summary does not exceed the maximum length and can dynamically adjust to the actual content to avoid unnecessary ellipses.You can also use it for form validation, to prompt users about the length limit of input content on the client side, improving user experience.For responsive design, dynamically adjusting the content of elements is also very helpful.

Summary

Bylengthandlength_isThese simple yet powerful filters allow you to precisely control the length of strings and data structures in templates, whether it's pure English or complex Chinese scenarios, you can handle them with ease, thereby creating websites with comprehensive functions and good user experience.


Frequently Asked Questions (FAQ)

  1. lengthDoes the filter calculate the length of HTML tags?No,lengthThe filter calculates the actual number of characters in the string content, not the byte length of the original string including HTML tags. If you pass a string containing HTML tags (such as<b>Hello</b>Pass directly tolengthThe filter treats it as 11 characters (including</>/betc.). If you want to get the length of the plain text after removing HTML tags, you need to usestriptagsthe filter to remove

Related articles

What is the impact of the `slice` filter on performance when handling large strings or large arrays?

Performance considerations for the `slice` filter in AnqiCMS when handling large strings or large arrays The `slice` filter is a very convenient feature in the AnqiCMS template engine, allowing users to extract specified length data segments from strings or arrays.Whether it is to extract an abstract from a long article or to select a portion of elements from a large data list, `slice` can provide flexible control.

2025-11-08

How to use the `slice` filter to get all elements of the array except the first and last?

When managing and displaying content in Anqi CMS, the flexibility of the template is the key to improving website operation efficiency.The built-in Django template engine syntax provides many practical filters, allowing us to handle various data in a concise and efficient manner.Today, let's delve deeply into a very practical filter——`slice`, which can help us accurately cut a specific part of an array (or called slice in the Go language environment), especially on how to get all elements except the first and last one.Content display is the core of website operation, whether it is an article list

2025-11-08

What result will the `slice` filter return when operating on an empty string or array?

In AnQi CMS template development, we often need to perform fine-grained control and processing of content.Among them, the `slice` filter is a very practical tool that allows us to extract the specified part from a string or array (usually referred to as a slice in Go language).This feature is very convenient when displaying article summaries, partial elements in pagination lists, or processing dynamic data.

2025-11-08

Template optimization: What is the difference between the `slice` filter and the `truncatechars` filter? When should you choose which one?

In Anqi CMS template development, we often need to control or truncate the displayed content to adapt to different layout requirements and optimize the user experience.At this point, the `slice` filter and `truncatechars` filter have become our commonly used tools.Although they can all achieve text slicing, there is a significant difference in functional focus and application scenarios.Understanding these differences can help us choose the appropriate filters more accurately, making the template code more efficient and the page display more reasonable.

2025-11-08

Check array or string length: `length_is` filter practical skills in AnQiCMS template?

When building a website, we often need to dynamically adjust the page display or execute different logic based on the length of the content.For example, if a title is too long and needs to be truncated, or if a list is empty and a prompt such as 'No content available' needs to be displayed.In AnQiCMS (AnQiCMS) template system, the `length_is` filter is exactly the tool for handling such precise length judgments.

2025-11-08

How can you quickly get the first element of an array or the first character of a string in a template?

In Anqi CMS template development, efficiently obtaining data is the key to improving website performance and user experience.Whether it is dealing with a document list or image group obtained from the background, or operating on a common string, we often encounter the need to extract the first element of an array or the first character of a string.The Anqi CMS, based on Go language and Django template engine syntax, provides various intuitive and practical methods to achieve this goal.

2025-11-08

What are the limitations of the `first` and `last` filters when getting the first and last elements of a string/array?

In Anqi CMS template design, the `first` and `last` filters are two concise and practical tools designed to help us quickly obtain the first or last character of a string, as well as the first or last element of an array.They perform well in handling simple data structures, but in practical applications, if their internal mechanisms are not well understood, they may encounter some unexpected behaviors.Firstly, let us understand their core functions. For string type data, `first` will return the first character, and `last` will return the last character

2025-11-08

`split` filter: How to split a long string into an array by a specific delimiter?

In the powerful functions of AnQi CMS, the flexibility of content display has always been our focus.Sometimes, you may need to enter a long string of information in a field on the back end, such as multiple keywords for an article, multiple parameters for a product specification, or a list of skills for a team member.This information is usually connected by specific delimiters (such as commas, semicolons, or pipes).However, when you want to display this information individually on the front page, even to style each part differently, the problem arises: how can you conveniently split this long string into independent items?

2025-11-08