How to judge the current link status in the navigation list tags and add a highlight style?

Calendar 👁️ 67

In website operation, clear navigation is a key factor in improving user experience.When users can intuitively see their position on the current page, it not only reduces a sense of confusion but also enhances the professionalism and usability of the website.Adding a highlight style to the current link in the navigation bar is a very effective method to achieve this goal.

In Anqi CMS, highlighting the current navigation link is actually simpler and more direct than you imagine.This is mainly due to the built-in template tags of the system providing ready-made judgment mechanisms, we do not need to write complex backend logic, just perform simple conditional judgments and style additions in the template file.

Understand the Core:navListwith the tag andIsCurrentproperty

The AnQi CMS template system uses a syntax similar to the Django template engine, making content display control very flexible. When dealing with navigation lists, we often use tonavListLabel. The function of this label is to obtain the preset navigation link list from the background, and then we can display these links in the front-end template in a loop.

What's even better is,navListThe label in each returned navigation item (usually referred to asitem) contains a very practical boolean attribute calledIsCurrent. ThisIsCurrentThe attribute will compare the current URL visited by the user with the URL linked to the navigation item. If they match, thenIsCurrentThe value istrueThis indicates that this navigation item is the link corresponding to the current page; otherwise, it is not.false.

Just this one.IsCurrentAttribute, which provides the most direct and convenient basis for us to judge the current link status and add highlight styles.

Implementation steps: judge and add styles in the template

Next, let's see how to highlight the navigation item in the Anqi CMS template file through specific operations.

Step 1: Get navigation list data

Firstly, you need to callnavListthe tag to get navigation data. Typically, we will assign these navigation data to a variable, such asnavsThen proceedforLoop to iterate through these navigation items.

{% navList navs %}
    <ul>
    {%- for item in navs %}
        {# 这里是每个导航项的HTML结构,接下来我们会在这里添加高亮逻辑 #}
    {% endfor %}
    </ul>
{% endnavList %}

Second step: useIsCurrentHighlight class name added

In each loopitemWe can check itsIsCurrentproperties. We can useifconditional judgment, whenitem.IsCurrentWithtrueWhen, add a predefined CSS class name to the corresponding HTML element (usually<li>or<a>tag), for exampleactive.

{% navList navs %}
    <ul class="main-nav">
    {%- for item in navs %}
        <li class="{% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}">{{item.Title}}</a>
        </li>
    {% endfor %}
    </ul>
{% endnavList %}

In the above code snippet,{% if item.IsCurrent %}active{% endif %}The purpose of this line of code is: if the currentitemis the current page, then give<li>label toactiveCSS class name.

Step 3: Define your highlight style

Finally, you just need to add this in your website's CSS file (usually)/public/static/你的模板名/css/style.cssor a similar path), for thisactiveClass name defines the highlight style you want.

For example, you can set it like this:

.main-nav li a {
    color: #333;
    text-decoration: none;
    padding: 10px 15px;
    display: block;
}

.main-nav li.active a {
    color: #007bff; /* 蓝色字体 */
    font-weight: bold; /* 加粗 */
    background-color: #e9ecef; /* 浅灰色背景 */
    border-radius: 5px;
}

/* 如果你的导航有鼠标悬停效果,确保高亮样式优先级更高 */
.main-nav li a:hover {
    color: #0056b3;
}
.main-nav li.active a:hover {
    color: #007bff; /* 高亮状态下鼠标悬停,颜色保持不变 */
}

With such settings, when the user visits the "About Us" page, the "About Us" link in the navigation bar will automatically display in a blue bold background, clearly indicating the current location.

Advanced: Handling Highlighting in Multi-level Navigation

Of Security CMSnavListTags also support displaying multi-level navigation, andIsCurrentProperties are also effective in multi-level navigation. If your navigation includes submenus, you can use nestedforprocess in a loop,IsCurrentthe attribute will intelligently judge the corresponding navigation item on the current page, whether it is the main navigation or the sub-navigation item.

{% navList navs %}
    <ul class="main-nav">
    {%- for item in navs %}
        <li class="{% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}">{{item.Title}}</a>
            {%- if item.NavList %} {# 如果有子导航 #}
            <dl class="sub-nav">
                {%- for inner in item.NavList %} {# 遍历子导航 #}
                    <dd class="{% if inner.IsCurrent %}active-sub{% endif %}"> {# 为子导航添加不同的高亮类名 #}
                        <a href="{{ inner.Link }}">{{inner.Title}}</a>
                    </dd>
                {% endfor %}
            </dl>
            {% endif %}
        </li>
    {% endfor %}
    </ul>
{% endnavList %}

In this example of a multi-level navigation, we use for the sub-navigation items.active-subAs a highlight class name, this allows you to set different highlight styles for the main navigation and sub-navigation to meet more refined visual needs.

Summary

BynavListLabels and their built-inIsCurrentThe attribute, AnQi CMS makes the implementation of the highlight function of the navigation bar on the current page very intuitive and efficient.This can not only significantly improve the user experience of the website, allowing visitors to easily understand their position on the website, but also reflects the emphasis of Anqi CMS on practicality and convenience in design.Just a few lines of simple template code and CSS style, your website navigation can be refreshed.


Frequently Asked Questions (FAQ)

1. Why did the navigation items not highlight after I set it according to the steps?

Firstly, please check that your CSS styles are correct and have sufficient priority, and are not overridden by other general styles. You can use the browser developer tools (F12) to check the HTML structure of the navigation items, and seeactiveThe class name has been successfully added. IfactiveThe class name did not appear, it may be that the template code is incorrect orIsCurrentThe judgment did not take effect; if the class name has been added but the style does not take effect, you need to adjust the CSS selector weight or exclude other style interference. In addition, please make sure that you have filled in the correct link address for each navigation item in the background navigation settings.IsCurrentThe attribute is based on matching these links with the current page URL.

2.IsCurrentWhat is used to determine the attribute of the current link? Will dynamic parameters in my URL affect the judgment?

Of Security CMSIsCurrentThe property is mainly based on the complete link of the navigation item(item.Link) Compare with the current page URL. Usually, it will perform an exact match. If your URL structure contains dynamic parameters or aliases (such asexample.com/article?id=123), while the navigation link is only configured toexample.com/articlethenIsCurrentIt may not highlight automatically. In this case, it is recommended that you check the pseudo-static rule settings to ensure that the navigation links are as consistent as possible with the actual access links on the page, or in some special cases, you can consider combining front-end JavaScript for auxiliary judgment to add highlighting.

3. Can I set different highlight styles for different navigation categories (such as top navigation and footer navigation)?

Completely. When you have created different navigation categories in the background (such as top navigation of thetypeId=1, footer navigation of thetypeId=2), you can call them separately in the template:

{# 顶部导航 #}
{% navList topNavs with typeId=1 %} ... {% endnavList %}

{# 页脚导航 #}
{% navList footerNavs with typeId=2 %} ... {% endnavList %}

In CSS, you can define different highlight styles for different navigation containers (such as, for the top navigation's<ul>addclass="top-nav", for the footer navigation's<ul>addclass="footer-nav") and so on:

`css .

Related articles

How to create and manage a website navigation menu and implement the display of secondary navigation?

The website's navigation menu, like the skeleton and landmark of the website, guides visitors to quickly find the information they need, and is the core of user experience and website information architecture.A clear and intuitive navigation system not only enhances user satisfaction, but is also crucial for search engine optimization (SEO).AnQiCMS was designed with this in mind from the outset, providing a flexible and easy-to-operate navigation menu management function, allowing website operators to easily build and maintain the navigation system of the website.

2025-11-07

How to control whether the website name suffix is displayed in the homepage title TDK tag?

If you are using AnQiCMS to build or manage your website, you may encounter a common and important issue: how to finely control whether the website name is displayed as a suffix in the `<title>` tag on the home page.This not only concerns the display effect of the website in search engine results, but also directly affects users' perception of the brand.The AnQi CMS, as a content management system that focuses on SEO optimization and flexibility, provides simple and powerful control capabilities in this aspect.First, let's clarify what is meant by the "website name suffix" mentioned here

2025-11-07

How to dynamically display Title, Keywords, and Description on the homepage to optimize SEO?

It is the first step in building a successful website to make it search engine friendly.And Title (title), Keywords (keywords), and Description (description) - usually abbreviated as TDK - are the core elements of a website's dialogue with search engines.They not only tell search engines about the content of your page, but also directly affect whether users click on your website in the search results.AnQi CMS knows this, and therefore built-in powerful TDK management features, allowing you to easily achieve the dynamic display and optimization of TDK.

2025-11-07

How to customize contact information parameters in AnqiCMS and call them to display in the template?

It is crucial to maintain smooth communication with users in website operation.In addition to traditional contact methods such as phone and email, we often need to display more personalized social media or instant messaging information.AnqiCMS provides a very flexible contact information management module that allows you to easily customize and flexibly display this information at any location on the website.Next, we will divide it into two main steps to explain in detail how to customize contact information parameters in AnqiCMS and call them to display in the template.### One, in AnqiCMS

2025-11-07

How to dynamically display the list of articles or products under a category in the navigation dropdown menu?

AnQi CMS: Build dynamic dropdown navigation menus to make content easily accessible In website operation, the navigation menu is the starting point for users to explore website content, and it is also an important clue for search engines to understand the website structure.The traditional navigation menu is often fixed links, when the website content becomes increasingly rich, especially when there are many articles and product types, manually maintaining the content list in the dropdown menu becomes particularly繁琐 and prone to errors.The AnQi CMS provides us with an elegant and efficient solution with its powerful content management capabilities

2025-11-07

How to manage and display all documents in AnqiCMS and support filtering by title, model, and category?

In the era of explosive digital content, efficiently managing and displaying website documents is a core challenge for every operator.When you have a vast amount of articles, product descriptions, service instructions, or any form of site content, how to ensure that these contents are organized, easy to find, and can be filtered according to different needs, is directly related to user experience and operational efficiency.AnqiCMS, as a system specially designed for enterprise content management, provides a powerful and flexible solution in this aspect.

2025-11-07

How to display thumbnails, descriptions, and custom fields on the document list page?

In website operation, the content list page is a key link for users to obtain information and decide whether to delve deeper into browsing.A richly informative, well-structured list page that can greatly enhance user experience and content appeal.It is particularly important to display thumbnails, descriptions, and custom fields reasonably in the document list page of Anqi CMS to achieve this goal.Through the powerful and flexible template tag system provided by Anqi CMS, you can easily meet this requirement without complex programming knowledge, just need to make simple modifications and configurations to the template file.

2025-11-07

How to accurately retrieve and display the content of a specified ID or URL alias on the document detail page?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides users with a rich set of tools to build and manage websites.During website operations, we often need to precisely display the detailed content of a document on a specific page, whether it is the document being viewed on the current page or specified by a particular identifier (such as ID or URL alias).The template tag system of Anqi CMS, especially the `archiveDetail` tag, is the key to meeting this requirement.

2025-11-07