How does AnQiCMS template truncate and display the description content of a navigation item if it is very long?

Calendar 81

As an experienced website operations expert, I know that in today's user experience-centric era, even details like website navigation contain huge optimization space.AnQiCMS as an efficient and flexible content management system, its powerful template functions provide us with enough 'magic' to meet various front-end display challenges.Today, let's talk about a common yet often overlooked issue: how can we elegantly truncate the display of navigation item descriptions in the AnQiCMS template.

Optimize navigation experience: Smart truncation display strategy in AnQiCMS template

The website navigation is the first window for users to explore the website content.A clear, concise, and responsive navigation system that can significantly improve user experience and even directly affect the conversion rate of the website.In the AnQiCMS backend, we can add a "navigation description" for navigation items (DescriptionThis field is used to provide more detailed information or SEO-related keywords.These descriptions undoubtedly enrich the context of navigation, but when displayed on the front end, if handled improperly, overly long description text can easily stretch the layout, especially on mobile devices, leading to a chaotic interface and the opposite effect.

AnQiCMS understands that the flexibility of content display is crucial for operation.It adopts a syntax similar to the Django template engine, allowing content operators and front-end developers to easily customize page layouts and data presentation methods.When it comes to truncating long descriptions in navigation, the template tags and filters (Filter) provided by AnQiCMS are exactly the tools to solve this problem.

Obtaining navigation item description content

In the AnQiCMS template, we usually usenavListTag to retrieve navigation data. This tag will return an array object of navigation items, each containingTitle(Navigation title),Link(navigation link) and the ones we are interested inDescription(navigation description) and other fields.

For example, we might display navigation items in a template like this:

{% navList navs %}
<ul>
    {% for item in navs %}
        <li>
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            <p>{{ item.Description }}</p> {# 这里的描述可能过长 #}
            {%- if item.NavList %}
            <dl>
                {%- for inner in item.NavList %}
                    <dd><a href="{{ inner.Link }}">{{inner.Title}}</a></dd>
                {% endfor %}
            </dl>
            {% endif %}
        </li>
    {% endfor %}
</ul>
{% endnavList %}

As you can see,{{ item.Description }}The navigation description is output directly. If this description text is too long, it will directly affect<li>The element's layout, even destroying the overall aesthetics. At this point, it is necessary to introduce a truncation strategy.

AnQiCMS's 'Content Scissors': Practical Truncation Filter

The AnQiCMS template engine built-in a variety of powerful filters, among which there are several specifically for string truncation, which is the key to solving the problem of long navigation descriptions:truncatechars/truncatechars_html/truncatewordsandtruncatewords_html.

  1. truncatecharsCharacter precise truncationThis filter is very suitable for truncating plain text content. It will truncate according to the number of characters you specify and add an ellipsis at the end....)。For example, if you want the navigation description to display a maximum of 50 characters:

    <p>{{ item.Description|truncatechars:50 }}</p>
    

    It will start counting from the beginning for 50 characters (including spaces and punctuation), and then truncate directly. It should be noted that ifitem.Descriptionit contains HTML tags,truncatecharsIt will be truncated directly, which may cause unclosed HTML tags and thus destroy the page structure.

  2. truncatechars_html: HTML-friendly truncationThis is highly recommended for processing descriptions containing HTML tags.It not only truncates characters to a specified length, but more importantly, it can intelligently handle HTML tags to ensure that the truncated HTML structure remains complete and valid.This means that even if your navigation description has been edited in the background rich text editor, including bold, links, and other formats, this filter can ensure that there will be no style chaos or unclosed tags after truncation.

    <p>{{ item.Description|truncatechars_html:80|safe }}</p>
    

    This specifies a length of 80 characters and adds extra|safefilter.|safeIt is an important filter in the Django template engine, which tells the template engine that this content is 'safe' HTML code and does not need to be automatically escaped. Becausetruncatechars_htmlThe processed content may still be HTML, plus|safeIt can ensure that the browser correctly parses it, rather than displaying the HTML code as it is.

  3. truncatewordsandtruncatewords_html: Truncate by wordThese filters are similar to the ones mentioned above.truncatecharsandtruncatechars_htmlSimilar, but they are truncated based on 'words' rather than 'characters'.This provides a more natural reading experience in English or other space-separated languages, avoiding the word being abruptly cut off in the middle.

    • truncatewords:10It will truncate to the 10th word.
    • truncatewords_html:15It will truncate to the 15th word while maintaining the HTML structure intact.

    For Chinese and other continuous writing languages, the concept of words is not as clear as in English, so in most casestruncatecharsortruncatechars_htmlit may be more appropriate.

Integration and practice: applying truncation to navigation

In general, the most universal and reliable solution is to usetruncatechars_htmlCombine|safefilter. Let's apply this strategy to the navigation code in front:

{% navList navs %}
<ul>
    {%- for item in navs %}
        <li class="{% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            {%- if item.Description %} {# 仅当有描述时才显示 #}
            <p class="nav-description">{{ item.Description|truncatechars_html:80|safe }}</p>
            {%- endif %}
            {%- if item.NavList %}
            <dl>
                {%- for inner in item.NavList %}
                    <dd class="{% if inner.IsCurrent %}active{% endif %}">
                        <a href="{{ inner.Link }}">{{inner.Title}}</a>
                    </dd>
                {% endfor %}
            </dl>
            {% endif %}
        </li>
    {% endfor %}
</ul>
{% endnavList %}

Through this optimized code, the description content of each navigation item will be intelligently truncated to within 80 characters, and the structure of the HTML tags in the description will also be preserved intact.This way, no matter how long the original description is, the front-end can maintain a tidy and consistent layout.

Beyond truncation: deeper thoughts on content operation thinking

Although template truncation can solve the urgent problem, as a senior operator, we should not stop here. In the long run, content planning and management are also important:

  • Source control: When editing the navigation description in the AnQiCMS backend, it should be controlled as much as possible according to the actual needs of the front-end layout.Some descriptions are more suitable for the detail page than for navigation prompts.
  • Interaction optimizationIf the truncated description is not sufficient to convey complete information, consider combining frontend techniques (such as CSS'stext-overflow: ellipsiscooperatehoverDisplay the full content or use JavaScript to implement click to expand/collapse to enhance user experience.
  • Responsive Design: The ideal length of navigation descriptions may vary depending on screen size. AnQiCMS templates support adaptive, code adaptation, PC+

Related articles

How to use the `forloop.Counter` variable in the AnQiCMS navigation list tag in multi-level navigation?

As an experienced website operations expert, I am well aware of the importance of an efficient and flexible CMS system for content management and user experience.AnQiCMS (AnQiCMS) provides strong support for content creators and operators with its high-performance architecture based on the Go language and a Django-style template engine.In AnQiCMS template development, flexibly using various tags is the key to enhancing website interactivity and maintainability.

2025-11-07

How to integrate search boxes, shopping carts, and other functional elements into the AnQiCMS navigation menu?

##驾驭AnQiCMS navigation: Skillfully integrate search, shopping cart and other functional elements to create an efficient user experience As an experienced website operation expert, I know the importance of an efficient and user-friendly navigation menu for the success of the website.In today's era of information overload, users expect to quickly find the content they need, and elements such as search boxes and shopping carts, if cleverly integrated into navigation, can undoubtedly greatly enhance user experience and website conversion rates.AnQiCMS, this is an enterprise-level content management system built based on the Go language, with its high efficiency, customizable, and easy to expand features

2025-11-07

Does the AnQiCMS navigation menu display different navigation structures according to device type (PC/Mobile)?

Good, as an experienced website operation expert, I am very willing to deeply analyze the ability of AnQiCMS in the differentiation display of navigation menus and transform it into an article that is easy to understand and practical. --- ## Safe CMS Navigation Menu: The Secret of PC/Mobile End Differentiation Display In today's multi-screen interconnection era, users visit websites with various types of devices.Whether it is the wide screen on the PC end or the compact interface on the mobile end, users expect to get a smooth and intuitive browsing experience.

2025-11-07

How to display dynamic data in the AnQiCMS navigation menu, such as the number of unread messages?

As an experienced website operations expert, I fully understand the core status of the navigation menu in user experience and website architecture.It is not only the index of the content, but also the key to users quickly obtaining the information they need and improving conversion efficiency.Today, we will delve into how to cleverly integrate dynamic data into the navigation menu of AnQiCMS (AnQi CMS), such as the number of unread messages that everyone cares about, which will undoubtedly greatly enhance user interaction and the vitality of the site.### Insight requirements: The challenge of integrating static navigation with dynamic data Traditional website navigation menus are often static.

2025-11-07

How to combine AnQiCMS navigation tags with TDK information in `tag-tdk.md` for page title optimization?

As an experienced website operation expert, I know that every detail is crucial for the visibility and user experience of the website, especially the core elements of search engine optimization (SEO) - page title (Title), keywords (Keywords) and description (Description), which we usually call TDK.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, with its powerful TDK management functions and flexible template tag system, which can help operators efficiently build SEO-friendly websites. Today

2025-11-07

How to pass specific variables to the navigation template of the AnQiCMS template using the `with` tag?

How to pass specific variables to the navigation template of the AnQiCMS template through the `with` tag?As an expert well-versed in website operations, I often encounter such a need: in order to maintain the neatness and maintainability of the website code, we usually break down commonly used modules such as headers, footers, sidebars, and navigation into independent template fragments.AnQiCMS (AnQiCMS) with its efficient architecture based on Go language and Django template engine syntax, provides us with great convenience. However

2025-11-07

Does the AnQiCMS navigation menu backend management interface provide batch operation functions?

## AnQiCMS navigation menu: Does it currently support batch operations?Expert interpretation and practical suggestionsFor website operators, efficiently managing navigation menus, especially in scenarios where frequent adjustments or large-scale updates are required, the need for batch operation features becomes particularly prominent.

2025-11-07

How to add a custom image as the navigation item background in AnQiCMS navigation menu?

## Add custom images as navigation item backgrounds in AnQiCMS navigation menu: Create a more visually striking website navigation As an experienced website operations expert, I fully understand the importance of an attractive navigation menu for user experience and brand image.AnQiCMS (AnQi CMS) provides a solid foundation for us to realize these creative ideas with its flexible template mechanism and powerful content management capabilities.Today, we will delve into how to add a custom image background to a specific navigation item in the AnQiCMS menu

2025-11-07