As an experienced website operations expert, I know that every detail can affect user experience and the overall performance of the website.AnQiCMS, this is an enterprise-level content management system developed based on the Go language, with its high performance, modular design, and friendly support for SEO, it provides us with powerful content management capabilities.The syntax adopted by the Django template engine also allows front-end developers to flexibly and efficiently implement various design requirements.

However, during the template development process, we often encounter a seemingly minor problem that may trigger a chain reaction: when a parent menu item does not have any submenu content, the template may still render an empty<ul>Tags. This may not only lead to a disordered front-end style and affect the aesthetics of the page, but may also slightly affect the semanticization of the page due to the generation of meaningless HTML structures, and even in extreme cases, interfere with some front-end scripts.

Today, let's delve deeply into how to avoid these empty sub-menus in the AnQiCMS GoLang template in an elegant and efficient manner<ul>The rendering of tags.


Understanding the navigation structure of AnQiCMS

In AnQiCMS, the website's navigation menu is usually through built-innavListThe tag is used to retrieve and render. This tag is very flexible and can help us easily build multi-level navigation. When we use{% navList navs %}such a label gets navigation data, it will return an array containing multiple navigation itemsnavs)

each navigation item (for example, we name ititem) has such asTitle(Title),Link(Link) properties. Moreover, if there is a submenu under a navigation item,itemit will contain a name calledNavList. ThisNavListis also an array storing all the currentitemsub-level navigation item.

The root cause of the problem is that if we hardcode the rendering of the submenu in the template<ul>the logic of the tag, evenitem.NavListThis array is empty,<ul>The tag itself will still be generated.

Core strategy: Use conditional judgment to avoid empty tags.

To solve this problem, we do not need complex logic, just skillfully use the conditional judgment feature provided by the AnQiCMS template engine -{% if ... %}Label. This label can help us check before rendering the submenu containeritem.NavListwhether it actually contains any sub-items.

Let's illustrate a common navigation menu code snippet:

Suppose you are building a two-level navigation menu. A preliminary template structure might look like this:

`twig