How to split and render multi-level navigation path strings when creating a dynamic navigation menu using the `split` filter?

Calendar 👁️ 69

In the daily operation of AnQi CMS, we often need to build flexible and diverse navigation menus to adapt to the changing content structure and user needs. Although AnQi CMS provides powerfulnavListLabels are used to manage the navigation of the background configuration, but in certain specific scenarios, such as when we need to dynamically generate multi-level navigation based on a string storing complete path information, or render a breadcrumb navigation with a depth far exceeding two levels, the built-in labels may not fully meet our refined needs.

This is the template engine built-in of Anqi CMSsplitThe filter comes in handy. It helps us to cleverly 'cut' the seemingly ordinary path string into individual navigation elements, thus allowing for flexible rendering.

Get to knowsplitFilter

splitThe filter is a very practical string processing tool.The core function is to split a string into an array (or slice) according to the specified delimiter.splitThey can be broken down one by one, allowing you to handle each word separately.

Its basic usage is very simple:{{ 你的字符串变量|split:"你的分隔符" }}.

For example, if you have a string"Apple,Banana,Orange"comma)

{% set fruits_string = "Apple,Banana,Orange" %}
{% set fruits_array = fruits_string|split:"," %}
{# 此时 fruits_array 将是一个包含 ["Apple", "Banana", "Orange"] 的数组 #}

BysplitAfter processing,fruits_arrayit becomes an array that can beforlooped through, with each element being a part of the original string.

Scenario setup: Manage multi-level navigation path strings

Assuming our website has a complex classification hierarchy, such as "Home > Products > Electronic Equipment > Camera", we hope to dynamically display such a complete navigation path at the top of each product detail page or category page, and each part should be a clickable link.

We can use the "Content ManagementFullNavPathThe custom field. In this field, we will store the navigation path in a specific format, for example:

首页|/ > 产品|/products/ > 电子设备|/products/electronics/ > 相机|/products/electronics/cameras/

This string contains navigation level information, display text, and corresponding links. Here we use “> as the level separator, as well as “| as the separator between the title and the link.}

Practice: Cut and render dynamic navigation

Now that we have the stored path string, the next step is how to use it in the templatesplitFilter it and render it out.

Step 1: Get the navigation path string

Firstly, we need to retrieve the custom fields stored in the background templateFullNavPathString. If it is on the category detail page, we can obtain it throughcategoryDetailtags:

{# 假设我们正在一个分类详情页,并已在该分类的自定义字段中设置了 FullNavPath #}
{% categoryDetail currentCategory with name="FullNavPath" %}
{% set fullPathString = currentCategory %} {# currentCategory现在就是我们的路径字符串 #}

Step two: First cut - split the navigation items

With the complete path string, we first use “>Separate the entire path into individual navigation items using the delimiter.

{% set pathSegments = fullPathString|split:" > " %}
{# 此时 pathSegments 会是类似这样的数组:["首页|/", "产品|/products/", "电子设备|/products/electronics/", "相机|/products/electronics/cameras/"] #}

Step three: Loop rendering and cut again - extract titles and links.

pathSegmentsNow is an array, each element is a标题|链接formatted string. We need to traverse the array and use each element againsplitFilter, extracts the title and link and renders them as HTML links.

`twig

Related articles

The `split` filter combined with `archiveDetail` or `categoryDetail` tags, what are some advanced usages to extract and process fields?

In AnQi CMS template development, the combination of the `split` filter with `archiveDetail` or `categoryDetail` tags and others provides powerful flexibility for us to extract and process data from fields.This not only makes the display of website content more refined and dynamic, but also better meets the specific content operation needs.

2025-11-08

What error message or default behavior will occur if the input received by the `split` filter is not a string type?

Anqi CMS is an efficient enterprise-level content management system that provides a rich set of tags and filters for template creation, helping us to flexibly display content.Among them, the `split` filter is a very practical tool that can split a string into an array according to a specified delimiter, which is particularly convenient in handling scenarios such as keyword lists, multi-value fields, etc. ### `split` filter's working principle and expected input We all know that the main function of the `split` filter is to "split strings".Imagine that

2025-11-08

Does the `split` filter apply to extracting specific attribute values from HTML content, such as `data-items="item1|item2"`?

In AnQi CMS template development, we often encounter situations where we need to handle different types of data.When it comes to extracting specific attribute values from HTML content, such as `data-items="item1|item2"`, and you want to further process these values, the `split` filter is a very useful tool.However, its applicability is not directly aimed at HTML parsing, but rather at the **string data already obtained**.###

2025-11-08

How to sort the array split by the `split` filter according to custom rules?

In website operations, we often need to handle various data, sometimes these data are stored in a string in a specific format.AnQiCMS (AnQiCMS) provides powerful template tags and filters, making content display flexible and efficient.Among them, the `split` filter is a very practical tool that can split a string into an array according to a specified delimiter, making it convenient for us to traverse and display the data further.However, when we split the string into an array, we sometimes encounter the need to sort these array elements.

2025-11-08

When using the `split` filter to process a large amount of data, are there any recommended practices to optimize performance?

When managing website content in Anqi CMS, the `split` filter is undoubtedly a very practical tool, which can help us easily split strings according to the specified delimiter into an array, thereby flexibly displaying the data.However, when the amount of data being processed is very large, or the page calls the `split` filter very frequently, we may start to pay attention to its performance.In the end, a smooth user experience and efficient server response speed is a goal pursued by any website operator.So, when processing a large amount of data with the `split` filter

2025-11-08

How to safely access array indices after splitting with the `split` filter to avoid 'out of range' errors?

In Anqi CMS template development, the `split` filter is undoubtedly a very practical tool.It can help us easily split a string containing a specific delimiter (such as multiple keywords or tags) into an array that can be traversed and accessed.

2025-11-08

How to combine the `split` filter with the background "keyword library management" function to automatically extract and process keywords?

In the daily operation of Anqi CMS, keywords are undoubtedly the core of content strategy.No matter whether it is to help users find your website through a search engine or to improve the relevance and user experience of on-site content, 'keywords' play a vital role.AnQi CMS provides a powerful "Keyword Library Management" function, helping us to centrally manage and optimize these valuable words.How can the keywords input by the background be implemented in the front-end template to achieve more flexible, intelligent, automated processing and display?This requires us to cleverly combine the `split` filter in the template engine.###

2025-11-08

How to use the `split` filter in data validation scenarios, such as checking if user input contains a specific number of elements?

In AnQiCMS's content operation practice, we often need to handle various data submitted by users, which may not be simple text or numbers, but structured information containing multiple elements, such as tags of an article, multiple features of a product page, or multiple choices in a questionnaire.Validate such inputs to ensure they meet our expected quantity requirements is the key to improving data quality and user experience.AnQiCMS template engine provides a very practical `split` filter

2025-11-08