How to judge the length of the article summary `archive.Description` to decide whether to display the 'Read More' link?

Calendar 👁️ 69

In website operation, the cleanliness of the content list page and the user experience are crucial.When the article introduction is too long, it may not only occupy too much page space, affect the overall layout beauty, but may also dilute the guiding role of the 'Read More' link.Therefore, it is an effective strategy to intelligently decide whether to display the "Read More" link based on the actual length of the article summary, which enhances the professionalism and user-friendliness of the website.AnQiCMS (AnQiCMS) provides a powerful and flexible template function, making it simple and efficient to meet this requirement.

Understandingarchive.Description: The core of the article introduction

In AnQi CMS,archive.DescriptionRepresents the article's summary content. This field is usually filled in during the background editing of the article, used to summarize the main theme of the article, often displayed on the article list page, search results summary, or as part of the page.meta description. As the length of its content is uncertain, we need to judge and control it at the template level to ensure consistency in the front-end display.

Core implementation: using a template to judge the length of a string

The Anqi CMS template engine provides rich filters (Filters) and logical tags, allowing us to easily operate on variables. To judgearchive.DescriptionThe length of the string, we can uselengthFilter. CombineifWith the logical judgment tag, we can determine the display of the 'Read More' link according to the length.

First, we can obtain the length of the article introduction in the template and store it in a temporary variable for later use. For example, we can get it like thisarchive.Descriptionlength:

{% set descriptionLength = archive.Description|length %}

here,|lengththe filter will returnarchive.DescriptionThe number of characters in a string. Next, we can set a threshold, such as 150 characters, to determine whether to display the 'Read more' link.

{% set descriptionLength = archive.Description|length %}
{% if descriptionLength > 150 %}
    <a href="{{ archive.Link }}">阅读更多</a>
{% endif %}

In the above code snippet, if the number of characters in the article summary exceeds 150, then a "Read more" link to the article detail page will be displayed in the article list item.archive.LinkIs the link to the article details, througharchiveListIt will automatically provide when the tag cycles articles.

Optimize the display effect: control the length of the introduction content

It may not be enough to just judge whether to display the 'Read More' link.If the introduction itself is long, even without displaying the link, the list page will still seem redundant.To maintain the layout neat, we usually also truncate the introduction content. At this time,truncatecharsThe filter comes into play.truncatecharsThe filter can truncate a string to a specified number of characters and add an ellipsis (...) at the end.

The key is: When determining length, the original content should be used, and the truncated content should be displayed.

Let's look at a more complete example, which combines length judgment and content truncation:

{% archiveList archives with type="list" limit="10" %} {# 假设这里正在循环文章列表 #}
    {% for archive in archives %}
        <div class="article-item">
            <h3><a href="{{ archive.Link }}">{{ archive.Title }}</a></h3>

            {# 获取原始简介内容,并设定一个希望显示的简介最大长度 #}
            {% set rawDescription = archive.Description %}
            {% set displayMaxLength = 150 %} {# 定义希望显示的简介最大字符数 #}

            {# 显示截取后的简介内容,确保列表页的整洁 #}
            <p>{{ rawDescription|truncatechars:displayMaxLength }}</p>

            {# 判断原始简介的长度是否超过了设定的最大长度,如果超过则显示“阅读更多”链接 #}
            {% if rawDescription|length > displayMaxLength %}
                <a href="{{ archive.Link }}" class="read-more">阅读更多</a>
            {% endif %}
        </div>
    {% endfor %}
{% endarchiveList %}

In this code block:

  1. We pass{% set rawDescription = archive.Description %}Store the original introduction content inrawDescriptionIn the variable. The advantage of doing this is that we can still retain the original content for length judgment while extracting the content.
  2. {% set displayMaxLength = 150 %}Defined the maximum number of characters for the summary to be displayed on the list page, this value can be flexibly adjusted according to your website design.
  3. {{ rawDescription|truncatechars:displayMaxLength }}The excerpt of the brief introduction will be output. For example, if the original brief introduction has 200 characters, here will output the first 150 characters with an ellipsis.
  4. {% if rawDescription|length > displayMaxLength %}This line of code is the core logic judgment. It checksOriginal introductionWhether the length exceedsdisplayMaxLength. Only when the original introduction is long enough and there is indeed more content that needs to be 'read', will the link be displayed.
  5. archive.LinkTags will automatically output the link to the current article's detail page.

In this way, we not only ensure that the length of the introduction on the list page is consistent, improve the visual experience, but also intelligently provide the "Read More" navigation when necessary, making the user interface more interactive and avoiding unnecessary link display.

Consideration of practical application scenarios.

In the operation of a website, this intelligent display strategy has many advantages:

  • User experience optimization: Keep the content list concise and unified, avoid long introductions that disrupt the layout, allowing users to browse information more quickly.
  • Search Engine Optimization (SEO): Display a truncated summary on the list page to avoid content repetition between the list page and the detail page, which is helpful for search engines to better identify the main content and reduce the risk of potential duplicate content penalties.
  • Responsive DesignOn different devices (such as mobile phones, tablets), you can adjustdisplayMaxLengthto make the length of the introduction adapt to different screen sizes, thereby improving the user experience on mobile devices.
  • template flexibilitySet the truncation length to a variable, allowing website administrators to easily adjust the display length of the introduction without modifying the core logic, to accommodate different design requirements or test effects.

In summary, the template engine of Anqi CMS provides strong support for the fine management of website content. By flexibly usinglengthandtruncatecharsCombine filters, withifLogical tag, we can easily implement intelligent control over the article summary, thereby significantly improving the user experience and operational efficiency of the website.


Frequently Asked Questions (FAQ)

Q1: I set the excerpt length, but the "Read More" link is still not displayed, what's the matter? A1:Please check if the variable you are using to determine the length is the original introduction content. For example, if you use{% set displayDescription = archive.Description|truncatechars:150 %}and then usedisplayDescription|lengthTo judge, then this length is already the length after truncation, and may never exceed the threshold you set. The correct approach is to use the originalarchive.Description|lengthTo judge, as shown in the article examplerawDescription|length.

Q2: If the article summary is empty, will the 'Read More' link still be displayed? A2:will not. Ifarchive.DescriptionIf it is empty, thenarchive.Description|lengthIt will return 0. In{% if rawDescription|length > displayMaxLength %}Under such a judgment condition, 0 will never be greater than the one you set.displayMaxLength(usually greater than 0), therefore the 'Read More' link will not be displayed. This is a logical default behavior.

Q3:truncatecharsandtruncatewordsWhat is the difference between these filters, and how should I choose? A3:`

Related articles

How to get the character length of the article title `archive.Title` for content truncation display?

In website content display, controlling the length of article titles is a common requirement, especially in list pages, recommended positions, and other scenarios, where overly long titles may destroy the page layout and affect user experience.AnQiCMS provides powerful and flexible template tags and filters to help users easily obtain and truncate the display of article titles `archive.Title`. ### Article Title in AnQiCMS Template In the AnQiCMS template, when we handle article data, we usually go through

2025-11-08

How to implement the combination of document title and link display with the `combineId` and `combineFromId` parameters in the `archiveList` tag?

In website content operation, we often encounter scenarios where it is necessary to display the relevance between different contents, such as the "From A to B" in travel products, the "Comparison of Product A and Product B" on product detail pages, or the "Basic service with value-added service" in service solutions. Anqi CMS provides two very practical `archiveList` tag parameters - `combineId` and `combineFromId`, which can help us display the title and link of documents in a flexible and dynamic way, thereby enhancing the richness of content and user experience.

2025-11-08

How to retrieve and flexibly display custom parameter fields in the document model with the `archiveParams` tag?

When managing content in AnQi CMS, we often encounter situations where we need to add specific attributes for different types of content (such as articles, products).The 'Content Model' feature provided by AnQi CMS allows us to customize fields according to business requirements, greatly enhancing the flexibility and scalability of content.How can I elegantly and flexibly obtain and display these custom parameter fields in the website front-end template?The `archiveParams` tag is the key to solving this problem.### Understanding the Custom Parameters in the Document Model Diving deeper into

2025-11-08

How does the `archiveDetail` tag handle the Markdown rendering and lazy loading (lazyload) display of document content?

Manage and display content in Anqi CMS, the `archiveDetail` tag is undoubtedly one of the core tools, responsible for extracting document content stored in the database and rendering it on the web.For modern websites, the way content is presented and the loading efficiency are crucial, and `archiveDetail` provides very practical capabilities in handling Markdown rendering of document content and image lazy loading.### Cleverly Utilize `archiveDetail` Tag

2025-11-08

How to limit the maximum number of characters that `archive.Content` displays in the summary in the template?

In website operation, how to efficiently display content summaries that can attract visitors to click and maintain the page tidy is a common and important issue.For those who use AnQiCMS, we often need to extract the essence of the document content as a summary to be displayed on list pages, search result pages, or related article recommendation modules.This article will introduce how to precisely control the maximum number of characters displayed in the `archive.Content` field in the AnQiCMS template summary.### Understand `archive.Content`

2025-11-08

How to check how many images are included in the group image `archive.Images` array on the article detail page?

It is crucial to manage the image display on the article detail page flexibly and accurately when building website content in AnQi CMS.Especially when we need to use multiple images to enrich the content of an article, the collage feature (usually corresponding to the `archive.Images` field) provides an efficient and convenient solution.In the operation, sometimes we need to understand how many images a specific article detail page contains, whether it is for page layout design, content statistics, or executing logic judgments under specific conditions.This article will elaborate on how to accurately check in AnQi CMS templates

2025-11-08

How to get the number of top-level menu items in the website navigation list `navList`?

In website content operation, the navigation menu is the first door for users to interact with the website content.AnQiCMS (AnQiCMS) provides a flexible `navList` tag to help us easily manage and display website navigation.Sometimes, in order to achieve a specific layout, style, or dynamic adjustment, we need to know the specific number of top-level navigation menu items on the website.This article will thoroughly introduce how to obtain the number of top-level menu items in the `navList` navigation list of AnQiCMS templates.### Understand `navList`

2025-11-08

How to judge if the length of the user comment content `comment.Content` exceeds the limit for front-end verification?

In website operation, the comment feature is an important part of user interaction.To maintain a good community environment and data quality, we usually limit the length of the comments submitted by users.Front-end validation plays a crucial role in this process, providing immediate feedback before submission to avoid failure due to content length, thereby enhancing user experience and reducing server load.How can we judge whether the length of the user comment content `comment.Content` exceeds the limit in the website built using AnQiCMS?

2025-11-08