How to count the character length of article titles, descriptions, or custom fields?

Calendar 👁️ 72

When operating content in AnQi CMS, we often need to pay attention to the character length of article titles, descriptions, or custom fields.This concerns not only SEO optimization, ensuring that the title and description comply with the **practice** of search engines, but also affects the reading experience of users on the search results page or within the website's internal list.How can you easily count the character length of this content in AnQi CMS templates?

The AnQi CMS built-in template engine provides many practical filters (filters), among whichlengthThe filter is our tool for counting character lengths. It can accurately return the number of characters in a string, whether English, numbers, or Chinese, they are all counted as one character, perfectly supporting UTF-8 encoding, and there is no need to worry about multilingual content.

Next, we will learn in detail how to use it in different scenarioslengthfilter.

Count the character length of the article title

The article title is one of the core elements of website content, its length directly affects the display effect on the search engine results page. In the Anqi CMS template, we can go througharchiveDetailTag to get the detailed information of the article and then combinelengthFilter to count the length of the title.

Assuming you are on the template of the article detail page (for example{模型table}/detail.htmlIn this way, you can obtain the current article title and count its character length:

{% archiveDetail with name="Title" %}
{# 或者先赋值给一个变量再统计,效果相同 #}
{% set articleTitle = archiveDetail with name="Title" %}
<div>文章标题:{{ articleTitle }}</div>
<div>标题长度:{{ articleTitle|length }} 个字符</div>

You can also easily achieve this by counting the title length of a specific ID article:

{# 假设要统计ID为1的文章标题长度 #}
{% archiveDetail specificArticle with name="Title" id="1" %}
<div>指定文章标题:{{ specificArticle }}</div>
<div>指定标题长度:{{ specificArticle|length }} 个字符</div>

Count the character length of the article description (Description)

Article descriptions are usually used for SEO meta tags or content summaries, and controlling their length is also important. Like titles, we can usearchiveDetailtags to obtain the article description and applylengthfilter.

In the article detail page template:

{% archiveDetail with name="Description" %}
{# 获取文章描述并统计长度 #}
{% set articleDescription = archiveDetail with name="Description" %}
<div>文章描述:{{ articleDescription }}</div>
<div>描述长度:{{ articleDescription|length }} 个字符</div>

Count the character length of a custom field

The powerful content model of AnQi CMS allows us to customize various fields for articles, products, etc., such as 'article source', 'author bio', or 'product features', etc.The length of the content of these custom fields may also need to be counted.

If your article model has a field namedauthorYou can count its length like this:

Method one: go through directlyarchiveDetailGet and count

{# 假设自定义字段的调用字段名为 'author' #}
{% archiveDetail articleAuthor with name="author" %}
<div>作者:{{ articleAuthor }}</div>
<div>作者名字长度:{{ articleAuthor|length }} 个字符</div>

Method two: througharchiveParamsTraverse to get custom fields and count

If you are unsure of the exact field name of the custom field or want to traverse all custom fields, you can usearchiveParamstags. In this case,item.ValueThis will be the value of a custom field.

{% archiveParams params %}
{% for item in params %}
    {# 假设我们只关心“作者”字段的长度 #}
    {% if item.Name == '作者' %}
    <div>
        <span>{{ item.Name }}:</span>
        <span>{{ item.Value }}</span>
        <span>长度:{{ item.Value|length }} 个字符</span>
    </div>
    {% endif %}
{% endfor %}
{% endarchiveParams %}

This method is also applicable to the category detail page (categoryDetail), single page detail page (pageDetail) and tag detail pages(tagDetailCustom field length statistics within, because they also support similar tag structures to retrieve field values.

Expansion: Counting word quantity.

In addition to character length, we may also need to count the number of words in the content, which is very useful under some English content creation or specific content review rules. Anqi CMS provideswordcountFilter to meet this requirement.

For example, count the number of words in an article description:

{% archiveDetail articleDescription with name="Description" %}
<div>文章描述:{{ articleDescription }}</div>
<div>描述单词数量:{{ articleDescription|wordcount }} 个单词</div>

Summary

AnQi CMS provides intuitive and easy-to-use tools for content operators through its flexible template tags and filters, helping us better manage and optimize website content. Whether it is to count the character length of article titles, descriptions, or various custom fields, it can be done simply by using thelengthorwordcountThe filter is easily implemented to ensure the standardization and consistency of content publishing, enhancing the overall quality and SEO performance of the website.


Frequently Asked Questions (FAQ)

Q1:lengthHow does the filter handle Chinese, Japanese, and other non-English characters?

A1: lengthThe filter supports UTF-8 encoded characters well. Whether it is Chinese, Japanese, Korean or other multibyte characters,lengthThis will recognize it as a single character for counting. This means the length of a Chinese character is 1, not the number of bytes.

Q2: Where else can I count the length of characters besides articles?

A2:As long as you can go through the Anqi CMS template tags (such ascategoryDetail/pageDetail/tagDetail/system/contact/tdkGet string type data usinglengthFilter to count the character length. For example, you can count the category name, single page title, website copyright information, etc.

Q3: I only want to display the first 20 characters of the article title, not the length, which feature should I use?

A3:If you need to truncate a string and add an ellipsis, you can usetruncatecharsFilter. For example, to display the first 20 characters of an article title and replace the rest with “…”, you can write it like this:{{ articleTitle|truncatechars:20 }}If you are worried that truncation will destroy the HTML tag structure, you can also usetruncatechars_html.

Related articles

What is the speciality of the `index` filter for Chinese position calculation when processing multi-language content?

## A clever approach: The unique features of `index` filter and Chinese position calculation in AnQi CMS multilingual content When managing multilingual content with AnQi CMS, the flexible use of its powerful template engine and rich filters can greatly improve content operation efficiency.Among them, the `index` filter is a very practical tool that helps us quickly locate the first occurrence of a specific substring in a string.However, when our content involves Chinese characters, the `index` filter has some unique behavior in position calculation.

2025-11-08

How to get the first occurrence position of a keyword in a string in AnQiCMS

In content operation, we often need to fine-tune and manage the text content on the website.In order to content review, dynamic display, or SEO optimization, sometimes we need to know the position of the first occurrence of a specific keyword in a text.AnQiCMS (AnQiCMS) is an efficient content management system that provides a convenient way to help us meet this need.Understanding the need: Why do we need to find the position of keywords?Understanding the position of keywords in strings is of great practical value in the process of content publishing and maintenance

2025-11-08

What role can the `contain` filter play in the context of sensitive word filtering?

In today's era where content is king, website operators are facing the dual challenge of actively publishing high-quality content while strictly controlling content security and compliance.Especially on platforms where user-generated content is increasing, how to efficiently and accurately filter sensitive words has become a key link in maintaining a healthy website ecosystem and protecting brand reputation.AnQiCMS (AnQiCMS) fully understands this need and provides many content security management functions, among which the `contain` filter can play a surprisingly practical role in the sensitive word filtering scenario.### The Foundation of Content Security

2025-11-08

How to check if a specific element exists in an array within a template

In the daily operation and template customization of AnQi CMS, we often encounter situations where we need to dynamically adjust the display of the page according to the data content.One common and practical requirement is to determine whether a specific element exists in an array (or list) within a template.This is crucial for realizing personalized recommendations, conditional content display, and even for functions such as permission control.The Anqi CMS adopts a template engine syntax similar to Django, providing rich tags and filters to handle data.When we get some data set from the backend, such as the list of article tags

2025-11-08

What is the difference between the `length` filter and the `length_is` filter in terms of content length judgment?

In AnQiCMS template design, we often need to judge or obtain the length of content in order to flexibly control the display of content.The `length` filter and `length_is` filter are born for this purpose.Although they are all related to 'length', in actual use, their functions and application scenarios are obviously different.Understanding these subtle differences can help us build templates more efficiently and accurately.### `length` filter: Content length 'counter' `length`

2025-11-08

How to get the number of elements in a list or key-value pair in AnQiCMS template?

AnQiCMS (AnQiCMS) makes website content display intuitive and efficient with its flexible template engine.In website operation, we often need to dynamically adjust the page layout, display different content, and even perform complex logical judgments based on the number of elements in a list or key-value pair (Map).Mastering how to obtain the number of these elements in a template is an indispensable skill for advanced customization and optimization.Luckyly, AnQiCMS template syntax provides a variety of convenient ways to help us achieve this goal.### Clever Utilization

2025-11-08

How to split a string containing multiple tags (such as “SEO, keywords, optimization”) into an array?

In the daily operation of Anqi CMS, we often encounter scenarios where we need to flexibly display some structured information in the content.For example, when setting keywords for articles or products, we may enter a string containing multiple tags in the "document keywords" field in the backend, such as Such strings are convenient to enter, but when displayed on the front end, we usually hope to display these tags separately, even to convert them into independent, clickable elements.How to convert such a comma-separated tag string

2025-11-08

What is the difference between the `split` filter and the `fields` filter when splitting strings by spaces?

During the template creation process in AnQi CMS, we often encounter scenarios where we need to split strings, such as extracting keywords from a description or parsing tag strings into independent words.AnQi CMS provides the practical filters `split` and `fields`. Although they can all split strings into arrays, there are subtle and crucial differences in their working methods and application scenarios when splitting strings by spaces.### `split` filter: The versatile separator expert `split` filter

2025-11-08