In website content operation, we often need to fine-tune the presentation of content, especially for information like article summaries. It not only affects the first impression of users but also plays a significant role in search engine indexing.In the flexible and powerful template system of AnQiCMS, getting the actual character length of the article abstract is a very practical skill, which can help us achieve more precise content presentation.

Why is it necessary to get the actual character length of the article abstract?

The abstract of the article is the core essence, usually used on list pages, recommended positions, or as the Meta Description of the page. In these scenarios, we may face various needs:

  • Unified styleEnsure all abstracts visually maintain consistent length, even if the input length varies during backend editing.
  • Word limitFollow the character count suggestion of the search engine for Meta Description to avoid key information being cut off.
  • Conditional judgmentAccording to the length of the summary, decide whether to display the 'Read More' button, or apply different styles.
  • Multi-language compatibilityEnsure that both Chinese and English are processed based on the 'actual character count' to avoid visual length discrepancies due to encoding differences.

In AnQi CMS, achieving this goal is not complex, the core lies in the clever use of built-in template tags and filters.

Get the article abstract in AnQi CMS template

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

Assuming you are on an article detail page orarchiveListWhen outputting the article list in a loop, you need to obtain the summary, you can extract the summary content like this:

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

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

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

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

The key to measuring the actual character length:lengthFilter

After obtaining the summary content, the next step is to measure its actual character length. The AnQiCMS template engine provides a rich set of filters, wherelengthThe filter is born for this.It can accurately calculate the actual character count of UTF-8 strings, which means it counts both English characters, numbers, and Chinese characters as one character, perfectly solving the problem of multi-language content length statistics.

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

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

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

Pass|lengthWith such a concise syntax, we have successfully obtainedarticleDescriptionThe actual character length of the summary content stored in this variable, and it is assigned.descriptionLengtha variable.

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

Now, let's combine these steps and see how they are applied in a real 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.DescriptionThe summary of the current article was directly obtained.
  3. currentDescription|lengthThen, the actual character length of the summary was calculated.
  4. We also added a simpleifJudgment, display different prompts based on the length of the abstract. This can be expanded into more complex logic in actual operation, such as dynamically truncating the abstract or changing the display style, etc.

Summary

The template system of AnQi CMS providesarchiveDetail[or use the loop directly]item.Descriptiontag andlengthFilter, providing 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 control and optimization of content, but also enhances user experience and the professionalism of the website.Master these basic and practical skills, and you will be more agile on the path of content operation.


Common Questions (FAQ)

  1. If the article abstract 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. lengthFilter is related totruncatecharsortruncatewordsWhat are the differences between filters? lengthFilter is used forgetThe actual character count of the string, it only provides a number.truncatecharsandtruncatewordsFilter is used forTruncatedStrings are typically followed by an ellipsis (…), which alters the displayed content. If you want to know the original length of the string before deciding how to truncate the display, you need to uselengthThen, it may be used againtruncatecharsortruncatewords.

  3. Besides the article summary,lengthThe filter can also be used to measure the length of which 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 it can also be used to get the number of elements in an array (slice), key-value pairs (map), or structure (struct).As long as it is a string type, it can accurately calculate the number of UTF-8 characters.