How to extract the article title and display an ellipsis in Anqi CMS template?

Calendar 👁️ 63

In AnQi CMS template development, it is often encountered that article titles need to be displayed on the list page, but in order to ensure the beauty of the layout and unified layout, long titles need to be truncated and accompanied by an ellipsis.This has improved the user experience and also made the page look neater and more professional.AnQi CMS with its flexible Django template engine syntax makes it very direct and efficient to meet this requirement.

Understand the template basics of Anqi CMS

In AnQi CMS, content management and display are core functions. We usually get the article list data througharchiveListtags and then loop through the data.forLabel) show the detailed information of each article. The article title is usually stored inTitlein the field.

For example, a basic article list loop might look like this:

{% archiveList articles with type="list" limit="10" %}
    {% for item in articles %}
        <div class="article-card">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>{{ item.Description }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

Here{{ item.Title }}The full title of the article will be output directly. When the title is too long, it may burst the layout and affect the overall aesthetics.

Core technique: applytruncatecharsTitle truncation filter

The Django template engine of AnQi CMS provides a series of powerful filters to process variable content, among whichtruncatecharsThe filter is the ideal tool for us to handle title truncation.

truncatecharsThe filter's function is to automatically truncate the string when its length exceeds the number of characters you specify, and add an ellipsis at the end (...It is important that the number of characters you specify isincludeincluding the total length of the ellipsis.

Its basic usage is very simple:

{{ 您的变量 | truncatechars: 期望的长度 }}

Assuming we want the title to display up to 25 characters, the rest will be indicated with an ellipsis, then we can modify the template like this:

{% archiveList articles with type="list" limit="10" %}
    {% for item in articles %}
        <div class="article-card">
            <h3><a href="{{ item.Link }}">{{ item.Title|truncatechars:25 }}</a></h3>
            <p>{{ item.Description }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

In this way, regardless of the length of the original title, the displayed title on the page is at most 25 characters, and if it is truncated, an ellipsis (...) is automatically added at the end, which is both aesthetic and practical.

Advanced application: Determine whether to add an ellipsis based on actual length

AlthoughtruncatecharsThe filter automatically adds an ellipsis, but sometimes we may want to display an ellipsis only when the title actually exceeds a specific length, and for titles that are not long, to keep them unchanged without any truncation. In this case, we can combineifLogical judgment andlengthTo implement a filter.

lengthA filter can get the actual character length of a string. Through it, we can first judge the length of the title and then decide whether to apply it.truncatechars.

Let's take a more complete example:

{% archiveList articles with type="list" limit="10" %}
    {% for item in articles %}
        <div class="article-card">
            <h3>
                <a href="{{ item.Link }}">
                    {% if item.Title|length > 30 %}
                        {{ item.Title|truncatechars:30 }}
                    {% else %}
                        {{ item.Title }}
                    {% endif %}
                </a>
            </h3>
            <p class="article-meta">发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
            <p class="article-summary">{{ item.Description|truncatechars:80 }}</p>
        </div>
    {% empty %}
        <p>暂无相关文章。</p>
    {% endfor %}
{% endarchiveList %}

In this example, we set a 30-character length limit for the title. Ifitem.Titleis longer than 30 characters, it will be applied.truncatechars:30Filter; otherwise, output the complete one directly.item.TitleThis ensures that truncation only occurs when necessary, and the ellipsis is only used when needed, providing a better user experience.

At the same time, you can also see that for the article abstractitem.Descriptionwe also appliedtruncatecharsa filter to maintain the high consistency of list items, avoiding long abstracts from destroying the layout.

Summary

In Anqi CMS template, it is a simple optimization measure that can significantly enhance the beauty and user experience of the page by truncating the article title and displaying an ellipsis. By flexibly using the Django template engine providedtruncatecharsandlengthFilter, we can easily implement intelligent title truncation.This is not only applicable to article titles, but also can be used for any text content that needs to control display length, such as article summaries, product descriptions, etc., making your Anqie CMS website content display more exquisite.

Frequently Asked Questions (FAQ)

1.truncatecharsCan the filter modify the style or content of the ellipsis (for example, change it to “…” instead of the default “…”)? truncatecharsThe ellipsis content of the filter output is fixed, currently not supported to modify its specific characters or style directly. If you need to customize the ellipsis, you may need to obtain the complete title, manually judge the length, and then usesliceThe filter extracts the string and manually concatenates the custom ellipsis character.

2. If my article title contains HTML tags,truncatecharsHow will it be handled? truncatecharsThe filter truncates based on characters and does not parse HTML tags.If the title contains HTML, it will also count the characters of the angle brackets and other HTML tags inside.truncatechars_htmlA filter that attempts to maintain the integrity of the HTML structure when truncating. However, for regular article titles, it is usually recommended to keep plain text,truncatecharsis perfectly applicable.

3. Where can I apply this truncation feature other than the article title?This truncation feature is very versatile. You can apply it to the summaries in the article list (such as in the example of theitem.Description|truncatechars:80) Product names or descriptions in the product list, long texts in the navigation menu, user comment summaries, etc. Any place where you want to limit the display length of text to maintain a tidy page and unified layout, you can use it flexibly.truncatecharsfilter.

Related articles

How to use AnQiCMS's 'Flexible Content Model' to customize the display structure and fields of articles, products, and other content?

## The secret of AnQiCMS content model: Creating a unique content display structure In the world of content management, website content is not uniform.The structure and fields of a blog article, as well as the description of an e-commerce product page, or the presentation of corporate event information, are often quite different.Traditional CMS systems may only provide fixed article or page templates, and once business needs change, the website seems to be struggling, and it is difficult to respond flexibly.However, AnQiCMS solves this pain point with its 'flexible content model' feature, allowing you to meet your actual business needs

2025-11-09

How to display a specific prompt to the visiting user when AnQiCMS is in shutdown mode?

During the use of the AnQiCMS website management, we occasionally encounter situations where we need to temporarily shut down the website, such as system upgrades, content maintenance, or data migration, etc.How can one clearly display specific prompts to the visiting user at this time, rather than a cold error page, which is particularly important.Our AnQi CMS is well-versed in this, providing us with a very user-friendly shutdown prompt function that allows the website to maintain a professional image even when temporarily 'resting'.Why do we need customized exit pop-ups?Imagine

2025-11-09

How to adjust the default language package of AnQiCMS website to adapt to different user interface display text?

In daily operations, you may wish to have the AnQiCMS website's backend management interface, system messages, or some built-in display text presented in different languages to better adapt to the working habits of team members or specific operational needs.AnQi CMS fully considers the needs of global operation and provides a convenient and quick way to adjust the default language package of the website.This feature not only makes your background operation smoother, but also lays a foundation for multilingual promotion. Adjust the AnQiCMS website's default language package, it is very intuitive.

2025-11-09

How to set the copyright information and filing number of the website in AnQiCMS and display them uniformly in the footer?

In website operation, the copyright information and filing number at the bottom (footer) of the website are not only a reflection of the professionalism of the website, but also an important part of legal compliance.For websites built with AnQiCMS, setting and displaying these information uniformly is a simple and direct process.This guide will help you easily complete these configurations in AnQiCMS, making your website look more professional and credible.### Step 1: Set copyright information and record number in AnQiCMS backend AnQiCMS provides a centralized management interface

2025-11-09

How to safely extract the content of an article containing HTML tags without destroying its structure?

In website content management and display, we often need to display the partial content of articles in article lists, homepage summaries, or related recommendation areas to attract readers to click.However, directly truncating the content of an article containing HTML tags can easily disrupt the original HTML structure, causing the page to display incorrectly and even affecting the overall layout and user experience.AnQiCMS (AnQiCMS) fully understands this pain point, its powerful template engine and built-in filters provide an elegant and secure solution, allowing you to worry-free about display issues caused by content truncation. Next

2025-11-09

Extract the article summary by word count instead of character count, which filter should be used in the template?

In website content operation, the way articles are presented often directly affects readers' click intentions and reading experience.A clear and concise summary that helps readers quickly understand the main theme of the article.Especially in scenarios such as list pages and recommendation areas, we usually hope that the abstract can be intelligently extracted, controlling the length while maintaining readability.When it comes to generating abstracts based on the content of an article, the length of the excerpt is a core consideration.There are two common ways to truncate: by character count and by word count.Cutting by character count is accurate, but it often encounters the situation where words are abruptly cut off

2025-11-09

How to prevent XSS attacks and correctly escape HTML special characters when outputting article content in a template?

Managing and displaying content in AnQiCMS is the core task of website operation, but it is also crucial to ensure that these contents are presented safely and securely to users.Among them, cross-site scripting (XSS) attacks are a risk that should not be ignored, which may allow malicious code to be executed in the user's browser accessing your website, thereby triggering a series of security issues, such as stealing user data, tampering with page content, and even hijacking user sessions.

2025-11-09

How to automatically convert line breaks in user input plain text content to the HTML `<br/>` tag?

In daily website content operations, we often need to enter some multi-line text content in the Anqi CMS backend, such as the introduction of articles, the characteristics of products, or detailed descriptions in custom fields.This content is usually entered in plain text format, however, when we expect them to be displayed on the front-end web page with the original paragraph and line break effects, we find that they are often cramped into a line, losing their original formatting.This is not a problem with the AnQi CMS system itself, but a characteristic of the HTML web page rendering mechanism.in HTML

2025-11-09