How to implement dynamic styles for navigation links in Anqi CMS template, such as highlighting the current page?

Calendar 👁️ 57

In website operation, the navigation bar is not only the entry for users to explore the website content, but also a key factor in enhancing user experience and the professionalism of the website.A smart navigation system that can highlight the current page intelligently, and can directly tell the user 'Where are you', thereby greatly improving the usability of the website.As a senior AnQi CMS operation personnel, I am well aware of the strength of its template system, and I will elaborate in detail on how to implement dynamic highlighting of navigation links in the AnQi CMS template next.

Anqi CMS navigation mechanism overview

The Anqi CMS template engine uses syntax similar to Django, which provides high flexibility for content display. For navigation functions, the system is built-in.navListLabel, this is a very practical tool that allows us to retrieve preset navigation link data from the background. What is more impressive is that throughnavListEach navigation item (item) retrieved by the tag contains a boolean field namedIsCurrent. ThisIsCurrentThe field is the key to the intelligent judgment of the corresponding relationship between the current page and the navigation link in AnQi CMS. When the user visits a page that matches a certain navigation link, the system will automatically correspond to the link corresponding to theitem.IsCurrentfield totrue.

The core idea of implementing navigation highlighting

To implement navigation highlighting in Anqi CMS template, our core strategy is to combinenavListThe navigation data provided by the tags, as well as the Anqi CMS template engine'sifLogical judgment label. Specifically, when we loop through the navigation list in the template, we can check each navigation item'sIsCurrentfield value. IfIsCurrentWithtrueWe dynamically add a specific CSS class to the HTML element corresponding to the navigation item (for example<li>or<a>label)activeorcurrent. Then, by defining the styles of these classes in a CSS file, you can achieve the visual highlighting effect.

Step by step to realize the highlighting display.

In order to clearly demonstrate the implementation process of navigation highlighting, we can break it down into the following steps:

First, in your Anqi CMS template file, for examplepartial/header.html(usually used to store the header and navigation bar code of a website) or more basicbase.htmlUsenavListtags to retrieve navigation data. You can navigate through the navigation categories configured in the background bytypeIdSpecify the navigation list to be called.

{% navList navs %}
    <!-- 导航项将在此处循环遍历 -->
{% endnavList %}

Next, innavListWithin the tag, you need to usefora loop to iterate over all the retrieved navigation items. In each iteration,itemThe variable will represent the current navigation link being processed.

{% navList navs %}
    <ul class="main-menu"> {# 假设您的主菜单有一个类名为 main-menu #}
        {%- for item in navs %}
            {# 处理单个导航项 #}
        {% endfor %}
    </ul>
{% endnavList %}

InforInside the loop, we can take advantage ofitem.IsCurrentfield for conditional judgment. Ifitem.IsCurrentThe value istrueIt indicates that the current navigation item points to the page the user is visiting, at this time we can add a CSS class to its parent.<li>or<a>A CSS class can be dynamically added to the tag.

<li class="{% if item.IsCurrent %}active{% endif %}">
    <a href="{{ item.Link }}">{{item.Title}}</a>
</li>

The logic of this code is: ifitem.IsCurrentWithtrue,<li>The tag will get an extra one.activeIf not, class; on the contrary, ifitem.IsCurrentWithfalse,<li>the tag will not haveactiveClass.

If your website navigation design includes secondary or multi-level dropdown menus, Anqi CMS'navListtag can also support. Each navigation item'sNavListThe field will contain its child navigation list, with a data structure similar to the first-level navigation item. You can nest another one in the loop inside theforloop, and use aifjudgment to check.item.NavListDoes it exist, if it exists, then use one moreforLoop to traverse the second-level navigation items and apply it as wellIsCurrentPerform the highlight judgment.

{% navList navs %}
<ul class="main-menu">
    {%- for item in navs %}
        <li class="{% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}">{{item.Title}}</a>
            {%- if item.NavList %} {# 检查是否存在二级导航 #}
            <ul class="sub-menu"> {# 二级菜单可以有自己的类名,例如 sub-menu #}
                {%- for sub_item in item.NavList %}
                    <li class="{% if sub_item.IsCurrent %}active{% endif %}">
                        <a href="{{ sub_item.Link }}">{{sub_item.Title}}</a>
                    </li>
                {% endfor %}
            </ul>
            {% endif %}
        </li>
    {% endfor %}
</ul>
{% endnavList %}

It should be noted that in the case of nested navigation, if a child navigation item is activated

Related articles

In what situations would the `PageId` field of the AnQi CMS navigation link be used?

AnQiCMS is a system focused on efficient content management, and its navigation function is an important bridge for users to interact with website content.A well-designed and powerful navigation system that can significantly improve the usability and user experience of a website.In AnQiCMS navigation link configuration, the `PageId` field is a key element, which is used in certain cases to achieve deep integration between navigation and the internal content of the website.When we set up the website navigation in the AnQiCMS backend management interface, the `PageId` field is part of each navigation link data structure

2025-11-06

How to judge whether the `item.NavList` field exists in the Anqi CMS template?

As a senior Anqi CMS website operator, I am fully aware that a well-structured, user-friendly navigation system is crucial for the user experience and search engine optimization of a website.Multilevel navigation, especially navigation with submenus, can help users quickly locate the information they need and improve the overall usability of the website.In AnQi CMS template, the `item.NavList` field is the key to achieving this function.

2025-11-06

How to configure a language switch link with icon or Emoji in AnQi CMS navigation?

As a senior CMS website operator for an information security company, I fully understand that building a user-friendly and powerful multilingual website is crucial for expanding the market.AnQi CMS with its flexible multilingual support, allows you to easily provide customized content experiences for visitors from different regions.Today, I will give a detailed introduction on how to elegantly configure a language switch link with icons or Emoji in your Anqi CMS website navigation.

2025-11-06

How to judge whether the current navigation is selected in the `navList` loop with `item.IsCurrent`?

As an experienced CMS website operation personnel of an enterprise, I am well aware of the importance of content presentation to user experience and website effect.A straightforward, responsive navigation system is the first step for users to browse a website, and clearly identifying the current page's position in the navigation is also a key detail to enhance user experience.Today, let's delve deeply into how to cleverly use the `item.IsCurrent` property in the `navList` loop to determine whether the current navigation item is selected in AnQiCMS.

2025-11-06

Can I set a navigation link in AnQi CMS that displays both Chinese and English subtitles?

As website operators who deeply understand the operation of AnQiCMS, we are well aware that in today's world where content globalization and user experience are increasingly important, how to flexibly display multilingual information is a key issue many operators face.In response to your inquiry about whether the AnQi CMS navigation link can display both Chinese and English subtitles, I can clearly tell you that AnQi CMS provides comprehensive support for this feature.

2025-11-06

What will AnQi CMS do to sort navigation links if the 'display order' numbers are the same?

As a long-term website operator dealing with Anqi CMS, I am well aware of the importance of the navigation system for website user experience and content presentation.AnQi CMS provides us with flexible navigation management functions, among which the 'Display Order' field is the key to controlling the arrangement of navigation items.For website visitors, a clear and logical navigation structure is the foundation for quickly finding the information they need.The initial design of "Display Order" in AnQi CMS is to allow operators to precisely control the arrangement of navigation links.Its basic logic is very intuitive

2025-11-06

How do navigation links adapt to sites in different languages when AnQi CMS supports multi-language?

As a website operator who is deeply familiar with the operation of AnQiCMS, I know that a flexible and efficient multilingual support system is crucial for expanding the international market and improving user experience.AnQiCMS provides powerful functions in this field, especially in how navigation links adapt to sites of different languages. It ensures smooth and user expectations of global content display through intelligent mechanisms and flexible template tags.

2025-11-06

How to prevent external links in AnQi CMS navigation from being tracked by search engines?

As an experienced Anqi CMS website operations personnel, I am well aware of the key role of website navigation in user experience and search engine optimization.Manage external links in the navigation well, ensuring that they are processed according to our wishes by search engines is an indispensable aspect of website operation. I will elaborate in detail on how to prevent external links in navigation links from being tracked by search engines in Anqi CMS.

2025-11-06