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.