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:
- We use
archiveListLooped through multiple articles. - Inside the loop,
article.DescriptionWe directly obtained the summary of the current article. currentDescription|lengthThen we calculated the actual character length of the summary.- We also added a simple
ifJudge, 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)
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.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.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.