How to determine if the current navigation item is the current page in the `navList` loop?

Calendar 👁️ 63

In website operation, a clear navigation structure is crucial for improving user experience.A good navigation not only helps visitors quickly find the information they need, but also guides users through visual cues (such as highlighting the current page) so that they always know where they are.In AnQi CMS, implementing this feature is quite intuitive and efficient, thanks to its powerful template tag system.

The core feature revelation:navListlabel'sIsCurrentproperty

AnQi CMS provides a powerful and easy-to-use template tag system for developers and operators to dynamically build website content. Among them,navListTags are the core tools for building website navigation. When we usenavListWhen the template loops to output navigation items, the system automatically attaches some useful attributes to each navigation item, and the key to determining the current page state lies in a name calledIsCurrentThe boolean type attribute.

IsCurrentThe attribute will be when the current navigation item is linked to the page the user is visiting, its value istrue; Otherwise, its value isfalseThis means that we do not need to perform complex URL matching or backend logic judgments, we just need to check the template simplyitem.IsCurrentThe value can easily achieve the highlight effect of the current page of the navigation item.

Practice: How to apply in the templateIsCurrent

In the template files of Anqi CMS, usenavListThe navigation label is usually combinedforLoop to traverse all navigation items. Inside the loop, we can useifcondition judgment label to checkIsCurrentProperty, and based on this, add a specific CSS class to the navigation item of the current page (for exampleactiveorcurrent-page), thereby achieving highlighting.

The following is a typical navigation loop code snippet that demonstrates how to judge and highlight the current navigation item:

{% navList navs %}
    <ul class="main-nav">
    {%- for item in navs %}
        <li {% if item.IsCurrent %}class="active-nav"{% endif %}>
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            {%- if item.NavList %} {# 如果有子导航 #}
            <dl class="sub-nav">
                {%- for subItem in item.NavList %}
                    <dd {% if subItem.IsCurrent %}class="active-sub-nav"{% endif %}>
                        <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                    </dd>
                {%- endfor %}
            </dl>
            {%- endif %}
        </li>
    {%- endfor %}
    </ul>
{% endnavList %}

In this code block:

  1. {% navList navs %}: Declare a variable namednavsvariable, which contains all the navigation data.
  2. {%- for item in navs %}: Loop through.navsEach navigation item. Here,{%-and-%}The usage helps to remove extra blank lines caused by HTML tags, making the final rendered HTML code cleaner.
  3. {% if item.IsCurrent %}class="active-nav"{% endif %}: This is the core logic. If the current loop reachesitem(Navigation item) is the page the user is visiting,item.IsCurrentit will betrueat this time, it will be set for the<li>tagsactive-navthis CSS class. You can define your own CSS styles, and replace it withactive-navany class name you want.
  4. {% if item.NavList %}This internal loop shows how to handle multi-level navigation. Anqi CMS'snavListtags also support nested structures,item.NavListwill contain sub-navigation items, and each sub-navigation item also hasIsCurrentProperty, making the highlight judgment of sub-navigation equally convenient.

After completing the writing of the template code, you just need to define it in the CSS style sheet of the website..active-navor.active-sub-navThe style of the class, such as changing the background color, text color, or adding underlines, can achieve a visual highlight effect.

Deep understanding:navListMore details about the tag

navListThe tag supports not onlyIsCurrentProperty to determine the current page, it also has more flexible navigation organization capabilities:

  • Support for multiple navigation sets:Websites often have more than one set of navigation, such as top main navigation, bottom footer navigation, and so on.navListTags can be used totypeIdParameters to specify the different navigation categories configured on the backend. For example,{% navList footerNavs with typeId=2 %}You can call the navigation category with ID 2, each set of navigation can independently judge itsIsCurrentstatus。“
  • Intelligent recognition of the current page:Of Security CMSIsCurrentThe logic is sufficiently intelligent. It can recognize the current page's URL and match it with the navigation item's link.Even if the navigation item links to a category page, and the user is browsing the detail page of an article under that category, AnQi CMS will intelligently mark the category navigation item and its parent navigation items asIsCurrentEnsure the correct highlighting of navigation paths, providing a coherent user experience.

By using correctlyIsCurrentThe attribute not only enhances the visual beauty of the website, but also greatly improves the navigation experience within the website.Visitors can clearly know where they are, which is very beneficial for improving the usability of the website and the user's stay time.

Frequently Asked Questions (FAQ)

1.IsCurrentHow to determine whether an external link is the current page? IsCurrentThe attribute is mainly used to match the URL paths within the AnQi CMS system. For navigation items linking to completely external websites, they are usually not registered internally unless redirected through specific settings or custom logic.IsCurrentIt is directly recognized as the current page. If you need to provide similar highlighting effects for external links, you may need to combine JavaScript or other frontend logic for the judgment.

2. If my navigation item links to a category page, and I am browsing a specific article detail page under that category,IsCurrentwill it take effect?Yes. Anqi CMS'sIsCurrentThe logic is sufficiently intelligent, it will recognize the category of the current page and mark the parent navigation item (if the navigation item links to the category) asIsCurrentThis ensures that when users browse category content, the category entry in the navigation bar also remains highlighted, providing a good user path indication.

3. How do I highlight the parent navigation item of the current navigation item?As mentioned in the previous question, Anqi CMS handles this situation by default. When you are browsing a deep page (such as an article detail page), all parent navigation items related to it (such as the category it belongs to, the main navigation) ofIsCurrentThe properties will be set totrueMake sure your CSS styles respond correctly to these marked items..active-navNested navigation elements of the class can achieve hierarchical highlighting effects.

Related articles

How to use the `navList` tag to create a navigation bar with submenus in the Anqi CMS template?

In AnQi CMS template development, building a functional and user-friendly navigation bar is one of the core tasks of website frontend development.Especially when navigation needs to include multi-level sub-menus, the `navList` tag provided by Anqi CMS can help us efficiently meet this requirement.Understanding the core role of the `navList` tag The `navList` tag is a tool specifically used in the Anqi CMS template to retrieve website navigation data and display it on the page.

2025-11-08

How to display documents under the current category using the `categoryList` tag without including documents from subcategories?

When building a website with Anq CMS, we often encounter such a need: on a category page, we want to display only the documents that belong to the current category, and not mix in the document content of its subcategories.This is particularly important in scenarios where the content structure is clear and avoids redundancy.Today, let's discuss in detail how to accurately achieve this goal through the template tags of Anqi CMS. ### Understanding AnQiCMS Classification and Document Relationship AnQiCMS has always fully considered the flexibility of content hierarchy from the beginning of its design.A category can have subcategories

2025-11-08

How to build a multi-level category navigation in Anqi CMS template and judge whether each category has subcategories?

In today's increasingly rich website content, a clear and efficient navigation system is crucial for improving user experience and search engine optimization (SEO).Especially for websites with a large amount of content or multiple product categories, multi-level category navigation can help users quickly find the information they need while also showing the structural hierarchy of the website to search engines.Anqi CMS with its flexible and powerful template engine makes it simple and intuitive to build such a navigation.This article will deeply explore how to cleverly construct a multi-level classification navigation in Anqi CMS template, and intelligently judge whether each category contains subcategories

2025-11-08

How to retrieve and display custom document parameters for a specific named using the `archiveParams` tag?

In AnQi CMS, content management is not limited to preset fields such as titles, content, etc., but can also be enriched and expanded by custom parameters.When you set unique custom fields for articles, products, or other content models, how to accurately obtain and display these specific parameter names in the front-end template is a common requirement in template creation.This article will deeply explore how to flexibly obtain and display the custom parameters of the `archiveParams` tag.### Custom parameter setting

2025-11-08

How to dynamically generate a custom field comment form with `guestbook` tag and adapt to different input types?

Managing website comments in Anqi CMS is a common requirement in daily operations, especially when we need to collect specific information, a flexible and customizable comment form becomes particularly important.Aqie CMS fully considers this requirement and provides a powerful `guestbook` tag, allowing us to easily dynamically generate a feedback form with custom fields and adapt to various input types.The power of the `guestbook` form label The core lies in the `guestbook` tag

2025-11-08

How to display a document's comment list in Anqi CMS template, including reply and pagination features?

In Anqi CMS, add a comment list to the document content and enable it to have reply and pagination functions, which can greatly enhance the user interaction of the website.Users can express their opinions and discuss specific documents, which not only enriches the content ecosystem but also brings more vitality to the website. ### One: Displaying Comment Lists: The Foundation of Interaction To display comments on a document page, it mainly relies on the `commentList` tag built into Anqin CMS.This tag helps us easily get the comment data associated with the current document. First

2025-11-08

How to judge whether the comment is under review and display it conditionally using the `commentList` tag?

In website operation, comments are an important element in enhancing user interaction and community vitality.However, it is an indispensable link to review the comments submitted by users to ensure the health and quality of the website content.How can we flexibly display these comments in AnQi CMS, especially when they are still in the review stage, which has become a concern for many operators.It's good that AnQi CMS template tag system provides us with strong control, especially the `commentList` tag, which can help us easily implement conditional display of comments.

2025-11-08

How to create a customizable pagination component for the document list in Anqi CMS template?

It is crucial to display and manage document lists efficiently when building a content-rich website.Especially for sites with a large amount of content, a well-designed, feature-complete pagination component can not only greatly improve user experience but also optimize the website's SEO performance, helping search engines to better crawl and index content.AnQiCMS (AnQiCMS) with its flexible template engine and powerful tag features, allows us to easily create highly customizable pagination components for document lists.

2025-11-08