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

Calendar 👁️ 71

In website content display, controlling the length of article titles is a common requirement, especially on list pages, recommended positions, and other scenarios. Long titles may destroy the page layout and affect user experience. AnQiCMS (AnQiCMS) provides powerful and flexible template tags and filters to help users easily implement article title controlarchive.TitlePerform length acquisition and truncation display.

Article title in AnQiCMS template

In the AnQiCMS template, when we process article data, we usually go througharchiveDetailTag to get the details of a single article, or inarchiveListLoop through the list of articles with the tag. Either way, the article title can be accessed through{{archive.Title}}or{{item.Title}}such a variable form directly.

For example, on the article detail page, we can directly get the title like this:

<div>文章标题:{{ archive.Title }}</div>

And on the article list page, by usingarchiveListLooping through each article obtained by the tag, the title can be accessed throughitem.TitleVisit:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <h3>{{ item.Title }}</h3>
    {% endfor %}
{% endarchiveList %}

Understood how to access the title variable, we can then use the built-in filters of Anqi CMS to further process it.

Get the character length of the article title:lengthFilter

Sometimes, we need not only to truncate the title, but also need to make some logical judgments based on the actual length of the title.For example, when the title is too long, it will display the truncated content, otherwise, it will display the full title. At this time,lengthThe filter comes into play.

lengthThe filter can return the number of characters in a string. It is worth mentioning that Anqi CMS handles Chinese characters very well, treating one Chinese character as one character rather than multiple bytes.

The usage is very simple:

{# 假设我们有一个文章标题变量 `archive.Title` #}
{% set title_length = archive.Title|length %}
<div>文章标题的长度是:{{ title_length }}</div>

In practical applications, we can use the title length for conditional judgments:

{% if archive.Title|length > 30 %}
    <p>该标题超过30个字符,需要截断显示。</p>
{% else %}
    <p>标题长度适中:{{ archive.Title }}</p>
{% endif %}

BylengthFilter, we can accurately obtain the character length of the title, providing data support for subsequent display logic.

Implement content truncation display:truncatecharsandtruncatewordsFilter

After obtaining the title length, the more direct need is often to truncate it to fit the display space. Anqi CMS provides two very practical filters to achieve this function: truncatecharsandtruncatewords.

1. Truncate by character:truncatechars

truncatecharsThe filter will truncate the string according to the specified number of characters. If the length of the original string exceeds the specified character count, it will automatically add an ellipsis at the end of the truncation....),and these three ellipses are also included in the specified total character count.

For example, we want to truncate the article title to a maximum of 20 characters:

{# 原始标题: "安企CMS:高效、可定制的企业级内容管理系统,致力于提供..." #}
<div>短标题:{{ archive.Title|truncatechars:20 }}</div>
{# 假设原始标题为“安企CMS:高效、可定制的企业级内容管理系统” #}
{# 输出可能为: 安企CMS:高效、可定制... #}

This filter is very suitable for scenarios that require strict control of display width, such as the title in card layout.

2. Truncate by word:truncatewords

Compared totruncatecharsstrict character counting,truncatewordsThe filter is more concerned with the semantic integrity. It will truncate by word count, and if the number of words in the original string exceeds the specified value, an ellipsis will also be added at the end (...This way, we can avoid words being cut off in the middle, making the text look more natural.

For example, we will truncate the article title into 5 words:

{# 原始标题: "安企CMS:高效、可定制的企业级内容管理系统,致力于提供..." #}
<div>自然截断标题:{{ archive.Title|truncatewords:5 }}</div>
{# 输出可能为: 安企CMS:高效、可定制的企业级... #}

This filter is more suitable in scenarios where the title content is relatively long and it is desired to maintain readability.

Application based on actual scenarios

In the actual template development, we usually combine it with the list page.archiveListTags use these filters to ensure that all article titles are displayed uniformly.

{# 假设这是一个文章列表页的模板文件 #}
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <div class="article-item">
            {# 将标题截断为最多 30 个字符显示 #}
            <h3><a href="{{ item.Link }}">{{ item.Title|truncatechars:30 }}</a></h3>
            <p>{{ item.Description|truncatechars:100 }}</p>
            <span class="meta">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
        </div>
    {% empty %}
        <p>暂时没有文章发布。</p>
    {% endfor %}
{% endarchiveList %}

By the above code, all article titles in the list will be uniformly truncated to 30 characters, and the parts that exceed will be automatically added with an ellipsis, thus ensuring the neatness and beauty of the list page.

Summary

The Anqi CMS template system provideslength/truncatecharsandtruncatewordsA series of powerful and intuitive filters that allow website operators and template developers to easily control the display length of article titles.By flexibly using these tools, we can create content display interfaces that are both aesthetically pleasing and user-friendly, effectively enhancing the overall quality of the website.


Frequently Asked Questions (FAQ)

1.truncatecharsandtruncatewordsWhat are the differences between filters? truncatecharsThe filter truncates by character count, including Chinese characters, with each character counting as one length, and an ellipsis is added when the specified length is reached.It is suitable for scenarios that require strict control of display width.truncatewordsThe filter is truncated by word count (separated by spaces), it adds an ellipsis after the last complete word.It is more suitable for scenarios where content truncation maintains semantic integrity and a natural reading experience.

2. What happens when my article title contains HTML tags, and it gets truncated?Of Security CMStruncatecharsandtruncatewordsThe filter defaults to treating the title content as plain text. This means that if the title contains<strong>or<em>Tags such as HTML, which are also counted in the number of characters or words, and may be damaged when truncated, causing the HTML tags to be rendered incorrectly. Therefore, it is recommended to title the articlearchive.TitleIt usually only contains plain text content, avoid using HTML tags. For rich text content containing HTML, Anqicms providestruncatechars_htmlandtruncatewords_htmlFilters that can intelligently truncate content and attempt to preserve the integrity of the HTML structure.

3. I can customize the ellipsis displayed after truncation (...) right?The built-in of AnQi CMStruncatecharsandtruncatewordsThe filter will automatically add a default ellipsis when truncating...Currently, it is not supported to directly customize the style or content of the ellipsis through parameters. If you really need to customize it, you may need to combine the use ofsliceA filter to截取 a specified length of string, then manually concatenate the custom ellipsis you want.However, this will slightly increase the complexity of the template, and in most cases, the default ellipsis can meet the needs.

Related articles

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 ensure that the AnQiCMS front-end page updates and displays the new image after the image resource is replaced?

When using AnQiCMS for website content maintenance, images are indispensable visual elements.However, sometimes the image resources are successfully replaced in the background, but the front page fails to display the updated image in time, which may confuse you.This is usually not a system failure, but due to the operation of various caching mechanisms.Understanding these mechanisms and mastering the corresponding handling methods can help you efficiently solve this problem.## The intrinsic mechanism of AnQiCMS image replacement Firstly, it is crucial to understand how AnQiCMS handles image replacement

2025-11-08

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

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

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