How to dynamically get the actual character length of the article abstract in AnQiCMS template?

Calendar 👁️ 71

In website content operation, we often need to finely control the display form of content, especially for information like article summaries, which not only affects the first impression of the user but also plays an important role in the inclusion of search engines.In the flexible and powerful template system of AnQiCMS, obtaining the actual character length of the article abstract is a very practical skill that can help us achieve more accurate content presentation.

Why do we need to get the actual character length of the article summary?

An article abstract is the core essence of the content, usually used on list pages, recommendation positions, or as the Meta Description of a page. In these scenarios, we may face a variety of needs:

  • Unified styleEnsure that all abstracts maintain consistent visual length, even if the input lengths vary during backend editing.
  • Word limitFollow the character count suggestion of search engines for Meta Description to avoid truncating key information.
  • Conditional judgmentDetermine whether to display the 'Read More' button or apply different styles based on the length of the summary.
  • Multilingual compatibilityEnsure that both Chinese and English are processed based on the 'actual character count' standard to avoid visual length inconsistency due to encoding differences.

In AnQi CMS, achieving this goal is not complicated, the core lies in skillfully using built-in template tags and filters.

Get the article summary in the AnQi CMS template

Firstly, we need to get the summary content of the article in the template. Anqi CMS providesarchiveDetailtags to obtain the detailed information of the article (document), which includes the article summary fieldDescription.

Assuming you are on an article detail page or througharchiveListWhen looping through the article list, you need to obtain the abstract, you can extract the abstract content in this way:

{# 假设这是在文章详情页,或者在archiveList循环中的item变量 #}
{% set articleDescription = archiveDetail with name="Description" %}

{# 如果是在循环中,可以直接使用 item.Description #}
{# {% set articleDescription = item.Description %} #}

<p>文章摘要内容:{{ articleDescription }}</p>

This code will retrieve the current article'sDescriptionField, which is what we call the article summary.

The key to measuring the actual character length:lengthFilter

After obtaining the abstract content, the next step is to measure its actual character length. AnQiCMS template engine provides rich filters, among whichlengthThe filter is designed for this. It can accurately calculate the actual UTF-8 character count of a string, which means that whether it is English characters, numbers, or Chinese characters, it is counted as one character, perfectly solving the problem of multi-language content length statistics.

tolengthApply the filter to the summary content we get, and we can get its character length:

{% set articleDescription = archiveDetail with name="Description" %}
{% set descriptionLength = articleDescription|length %}

<p>文章摘要内容:{{ articleDescription }}</p>
<p>摘要实际字符长度:{{ descriptionLength }}</p>

By|lengthWith such a concise syntax, we have successfully obtained it.articleDescriptionThe actual character length of the summary content stored in this variable and assigned it todescriptionLengthVariable.

Practical exercise: Get and display the summary length and application scenarios

Now, let's combine these steps to see how they can be applied in a template.For example, we want to display the abstract of each article in a list and make some simple style or logic judgments based on its length.

{% archiveList articles with type="list" limit="5" %}
    {% for article in articles %}
    <div class="article-item">
        <h2><a href="{{ article.Link }}">{{ article.Title }}</a></h2>

        {% set currentDescription = article.Description %}
        {% set currentDescriptionLength = currentDescription|length %}

        <p class="summary">
            {{ currentDescription }}
        </p>
        <p class="length-info">
            摘要实际字符数:{{ currentDescriptionLength }}个
            {% if currentDescriptionLength > 50 %}
                <span style="color: orange;">(摘要较长,建议精简)</span>
            {% else %}
                <span style="color: green;">(摘要长度适中)</span>
            {% endif %}
        </p>
        <a href="{{ article.Link }}" class="read-more">阅读更多</a>
    </div>
    {% else %}
    <p>暂时没有文章内容。</p>
    {% endfor %}
{% endarchiveList %}

In this example:

  1. We usearchiveListLooped through multiple articles.
  2. Inside the loop,article.DescriptionWe directly obtained the summary of the current article.
  3. currentDescription|lengthThen we calculated the actual character length of the summary.
  4. We also added a simpleifJudge, display different prompts according to the length of the abstract, which can be expanded into more complex logic in actual operation, such as dynamically truncating the abstract or changing the display style, etc.

Summary

The Anqi CMS template system passedarchiveDetail(or directly using the loop in theitem.Description) tag andlengthThe filter provides website operators with a convenient and efficient way to obtain the actual character length of article summaries.This feature not only helps to achieve accurate content control and optimization, but also enhances user experience and the professionalism of the website.Master these fundamental and practical skills, and you will be more adept on the path of content operation.


Frequently Asked Questions (FAQ)

  1. If the article summary content is empty,lengthwhat will the filter return?If the article'sDescriptionfield is an empty string ornilvalue,lengththe filter will return0This means you can use it to determine if the summary has been filled in and provide default text or hide related areas as needed.

  2. lengththe filter meetstruncatecharsortruncatewordsWhat are the differences between filters? lengthFilters are used forObtainThe actual character count of a string, it only provides a numerical value.truncatecharsandtruncatewordsFilters are used forTruncateA string is usually followed by an ellipsis (…), which changes the display content. If you want to know the length of the original text first, then decide how to truncate the display, you need to uselengthThen it may be used againtruncatecharsortruncatewords.

  3. In addition to the article abstract,lengthThe filter can also be used to measure the length of what kind of content? lengthThe filter is very general, it can be used to measure the length of any string variable (such as article titles, text content in custom fields), and also to get the number of elements in an array (slice), key-value pairs (map) or structure (struct).Any string type can accurately calculate the UTF-8 character count.

Related articles

The `index` filter calculates the position of Chinese characters in a string, how many positions does a Chinese character occupy?

In AnQi CMS template development, the `index` filter is a very practical tool that helps us locate the position of a specific substring in a string.However, when dealing with content containing Chinese characters, its performance in position calculation may confuse some users.How many positions does a Chinese character occupy in the `index` filter?What kind of logic is hidden behind this? Let's delve deeper together.### `index` filter's working principle In simple terms

2025-11-07

What result does the `index` filter return when the keyword does not exist in the string?

In the flexible and powerful template system of AnQiCMS, we often need to process and judge various text content on the page.Among them, the `index` filter is a very practical tool that helps us quickly locate the first occurrence of a keyword in a string or array. Then, when we need to search for a keyword that does not exist at all in the target string or array, what result will the `index` filter return to represent this situation?The answer is: it will return a clear **-1**.In the conventions of programming and template processing

2025-11-07

The `index` filter returns the index position or other identifier when searching for a specific value in an array?

In Anqi CMS template development, proficiently using various filters can make the display of page content more flexible and efficient.Among them, the `index` filter is a commonly used tool for handling array or string search tasks.However, many users may have a question when they first contact: When searching for a specific value in an array, does the `index` filter return the index position of the value or some other identifier?

2025-11-07

How to get the position of the first occurrence of a keyword in the AnQiCMS article title?

In website content operations, we sometimes need to refine the processing of article titles, such as highlighting a keyword or organizing content based on the position of the keyword.In terms of AnQiCMS, the system provides a powerful template engine, combined with its built-in filters, we can easily achieve these requirements.Today, let's discuss how to accurately obtain the position of the first occurrence of a keyword in the article title of AnQiCMS.### Core Function Unveiling: `index` Filter It can be used in AnQiCMS

2025-11-07

The `length` filter considers empty values or nil elements when calculating the length of an array or a key-value pair?

AnQiCMS template `length` filter: Deep understanding of its length calculation mechanism During the development of AnQiCMS templates, we often need to judge the length of strings, lists (arrays), or key-value pairs (Maps) to control the display logic of content.The `length` filter is designed for this purpose, it helps us get the length information of these data types.However, many users are confused about whether it considers null or `nil` elements when calculating length. Next

2025-11-07

How to use the `length_is` filter to determine if the character length of a user comment meets the specified requirements?

In the daily operation of AnQi CMS, we often need to manage user-generated content, especially user comments, which directly affect the activity and professionalism of the website.Content length control is one of the common requirements, for example, we hope that the comments are not too short to seem perfunctory, nor too long to affect the reading experience.Anqi CMS's flexible template engine provides a variety of powerful filters that can help us easily implement these seemingly complex validation logic.

2025-11-07

How will the `length_is` filter handle non-string or numeric types when comparing lengths?

AnQiCMS provides rich template filters to help us flexibly handle and display data.Among them, the `length_is` filter is often used to determine whether the length of the variable meets expectations.However, during use, one may encounter a question: What will the system do if we pass non-string or non-numeric data to the `length_is` filter for length comparison?This is not just a technical detail, but also about how we avoid potential errors in template design and ensure the accuracy of data display.

2025-11-07

How to accurately slice an array in the AnQiCMS template?

During the template development process of AnQi CMS, we often need to flexibly handle the data displayed on the page, especially when the data is presented in the form of a list or sequence.Imagine that you are designing a product list page, where you need to select the top 5 most popular products from an array of dozens of products to display at the top of the page; or, you may need to truncate a long string from the content of an article detail page to use as a summary.

2025-11-07