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

The template engine built into AnQi CMS provides many practical filters (filters), among whichlength

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

Translate the content of the JSON array from [auto] to [English]

The title of the article is one of the core elements of the website content, and its length directly affects the display effect on the search engine results page. In the template of Anqi CMS, we can go througharchiveDetailLabel retrieves the detailed information of the article and then combineslengthFilter calculates the length of the title.

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

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

You can also easily implement 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>

Translate the content of the JSON array from [auto] to [English]

The article description is usually used for SEO meta tags or content summaries, and its length control is also important. Similar to the title, 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

Auto CMS's powerful content model functionality allows us to customize various fields for articles, products, etc., such as "article sourceThe content length of these custom fields may also need to be counted.

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

Method one: Direct accessarchiveDetailObtain and count

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

Method two: througharchiveParamsTraverse to obtain custom fields and count

If you are not sure about the accurate name of the custom field call, or want to iterate through all custom fields, you can usearchiveParamstags. In this case,item.Valuewill be the value of the 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 classification detail page (categoryDetail), single-page detail page (pageDetail)and Tag Detail Page(tagDetail) for custom field length statistics, as they also support a similar label structure to retrieve field values.

Expansion: Count the number of words

In addition to character length, we may also need to count the number of words in the content from time to time, 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 the article description:

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

Summary

The 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 all be done simply bylengthorwordcountFiltering is easily implemented, ensuring the standardization and consistency of content release, enhancing the overall quality and SEO performance of the website.


Common Questions (FAQ)

Q1:lengthHow does the filter handle non-English characters such as Chinese and Japanese?

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

Q2: Where can I count character length besides the article?

A2:As long as you can use the template tags of Anqi CMS (such ascategoryDetail/pageDetail/tagDetail/system/contact/tdkObtaining string type data, you can uselengtha filter to count the length of its characters. For example, you can count the names of categories, single page titles, website copyright information, etc.

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

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