How to define a string array directly in the AnQi CMS template?

Calendar 👁️ 68

In AnQi CMS template development, we often encounter the need to display some list data on the page, such as navigation menus, category tags, or some fixed options.When this data is not suitable for dynamic retrieval from the background database, or when we simply need a simple, fixed set of string collections, defining a string array directly in the template is very convenient and efficient.The AnqiCMS based on Django template engine syntax provides a flexible way to handle such requirements.

Core method: UselistFilter defines a string array

We can cleverly utilize the template engine provided to directly define a string array in the AnQi CMS template.setLabel collaborationlistfilter.listA filter is specifically used to convert a string that conforms to a specific JSON format into an iterable array object.

The basic syntax structure is as follows:

{% set 变量名 = '["元素1", "元素2", "元素3", ...]'|list %}

here,{% set ... %}A tag is used to declare and assign a variable in a template.'["元素1", "元素2", "元素3", ...]'is a standard JSON format string, it represents a string array. Next, through the pipe|pass this string tolistFilter,listThe filter will parse and convert it into a Go language string slice that can be directly iterated over in the template ([]string{})

It should be noted that in JSON format strings, each element should be enclosed in double quotes, even if it is a number, after passing throughlistThe filter processed, will also be stored as a string in the array.

Practice example: define and traverse the array.

Assume we want to display several friendship link names at the footer, these link names are fixed and do not need to be managed dynamically from the background. We can define and use this string array in the template:

{% set friend_links = '["安企CMS官网", "AnQiCMS博客", "Go语言教程"]'|list %}

<div class="footer-links">
    <h3>友情链接</h3>
    <ul>
        {% for link_name in friend_links %}
        <li><a href="#">{{ link_name }}</a></li>
        {% empty %}
        <li>暂无友情链接</li>
        {% endfor %}
    </ul>
</div>

In this example,friend_linksThe variable is assigned an array containing three strings. Then, we useforLoop through the array, and create a list item for each element (i.e., each link name).{% empty %}Labels provide a fallback content when the array is empty, making the template more robust.

Application scenarios in practice

  1. Dynamically generate navigation menus or category options:When certain navigation items or filter options are fixed and limited in quantity, they can be directly defined in the template to quickly build an array.For example, the top menu contains 'Home', 'About Us', 'Contact Us', and so on.
    
    {% set main_nav = '["首页", "产品中心", "解决方案", "关于我们", "联系我们"]'|list %}
    <nav>
        <ul>
            {% for item in main_nav %}
            <li><a href="/{{ item|lower }}">{{ item }}</a></li>
            {% endfor %}
        </ul>
    </nav>
    
  2. Auxiliary management of page keywords:Although Anqi CMS provides a powerful keyword library function, for some specific pages or modules, we may need to add some auxiliary keyword prompts, which may not be desired to be globally managed, in which case the array can come in handy.
    
    {% set seo_tips = '["高效", "定制", "易扩展"]'|list %}
    <p>我们提供{% for tip in seo_tips %}{{ tip }} {% if not forloop.Last %}/{% endif %}{% endfor %}的内容管理服务。</p>
    
  3. Conditional judgment and content switching:We can define a string array containing specific states or permissions, thenifDetermine whether a variable is included within a statement to dynamically display content.
    
    {% set vip_levels = '["VIP1", "VIP2", "VIP3"]'|list %}
    {% if user.Group in vip_levels %}
    <p>尊贵的VIP用户,您好!</p>
    {% else %}
    <p>欢迎普通用户,升级VIP享受更多权益!</p>
    {% endif %}
    
    (Note:user.GroupIt should be a string type passed from the backend andfilter-contain.mdis mentioned.inoperator.)

Cautionary notes and **practice

Directly define string arrays in the template, which is most suitable for handling data that is relatively fixed, has a low frequency of change, and a small number of items. For data that needs frequent updates, has a large number of items, or is closely related to backend business logic, we still recommend using the various tags provided by Anqi CMS (such ascategoryList/archiveList/tagListDynamically retrieved from the database, which can better leverage the advantages of CMS, and maintain the maintainability and scalability of content.

BysetTags andlistThe combination of filters, we can easily define and operate string arrays in the Anqi CMS template, bringing more flexibility and convenience to the front-end display.


Frequently Asked Questions (FAQ)

Q1: Why can't we use{% set my_array = ["item1", "item2"] %}this syntax to define an array?A1: The Anq CMS template engine (based on Django-like templates in Go language) needs to perform explicit type conversion when handling template variable assignments. Directly using[...]Syntax is parsed as a plain string in the template instead of an array. Therefore, we need to use|listThe filter explicitly tells the template engine to parse the JSON format string inside the parentheses as a string array (slice).

Q2: Can I include numbers or other types of data in this string array? For example{% set mixed_data = '["文本", 123, true]'|list %}?A2: Yes, but it is important to note that,listThe filter will parse all elements of the JSON format string and store them as strings in the final[]string{}in the array. Even if you write numbers or boolean values in the JSON string, such as123ortruethey will be treated as strings in the array."123"and"true". If you need to perform arithmetic operations on these 'number' elements, you may need to perform additional type conversion when accessing them (if the template engine supports it).

Q3: Can I directly assign the data obtained from the Anqi CMS backend (such as the category list) to a custom string array?A3:categoryList/archiveListThis tag returns a list containing complex objects (for example, each element is a category object containing ID, title, link, and other attributes), not simple strings. If you want to extract a specific attribute (such as all category titles) to form a string array, you need to useforLoop through these objects and manually construct a new string array.Generally, it is more convenient to directly traverse and render the object list provided by the background data than to convert it into an array of plain strings.Directly define a string array in the template is suitable for small lists that are fixed and do not depend on backend data updates.

Related articles

How to display the name of a specified category

Flexible display of category names in AnQiCMS: A comprehensive template call guide Content categories are the core of the website structure, not only helping to organize and manage massive information, but also guiding users to browse the website and improve the user experience.Whether it is an article list, product details, or side navigation, clearly displaying category names can make the content of your website more organized.

2025-11-08

How to set pagination for a document list and display page navigation?

In modern website operations, setting up pagination for content lists is almost indispensable.It can not only significantly improve the user's browsing experience, avoid the slow loading caused by the long content of a single page, but also help search engines better crawl and index the website content.For friends using AnQi CMS, implementing pagination for the document list and displaying page navigation is a very intuitive and powerful feature.Our AnQi CMS provides flexible template tags, allowing full control over content display.To set pagination for the document list and display page navigation in a friendly manner

2025-11-08

How to implement multi-condition (such as custom parameters) filtering function on the document list page?

In AnQi CMS, implementing multi-condition filtering functionality on the document list page, especially filtering based on custom parameters, is crucial for improving user experience and helping users quickly find the content they need.AnQi CMS with its flexible content model and powerful template tag system makes this feature highly efficient and intuitive.The entire process can be divided into several core steps: First, define your content model and custom filter fields in the background;Secondly, build the display interface for the filtering conditions in the template; finally, present the filtering results in the document list using list tags.--- ### One

2025-11-08

How to implement keyword search and display results on the document list page?

How to help visitors quickly and accurately find the information they need in the face of increasingly large amounts of website content is the key to improving user experience and website value.AnQiCMS (AnQiCMS) is deeply proficient in content management, providing us with a powerful and flexible keyword search mechanism that allows the document list page to easily implement keyword search and elegantly present the results.

2025-11-08

Can 'list' filter be used to define an array that includes numeric types in the AnQi CMS template?

During the development of AnQi CMS templates, we often need to handle various data, among which arrays (or lists) are commonly used to organize data.When it comes to defining an array containing different data types directly in a template, many friends may be curious whether types like numbers can also be processed and included in the `list` filter?The answer is affirmative.The `list` filter in Anqi CMS template is designed to be very flexible, it fully supports including numeric data in the defined array.

2025-11-08

How to iterate over an array defined in an AnQi CMS template using the `list` filter?

In Anqi CMS template design, we often encounter situations where we need to define and iterate over some fixed or small array data.Although most dynamic content is retrieved through built-in tags such as `archiveList`, `categoryList`, etc., it is more convenient to directly process arrays in the template for some localization configurations, custom options, or static lists.AnQi CMS provides the `list` filter, combined with the `for` loop tag, it can easily meet this requirement.

2025-11-08

How would AnQi CMS handle if the string format is incorrect when using the `list` filter to define an array?

In website content operation, we often need to display various list data in templates, such as a set of keywords, product features, or navigation links.AnQiCMS provides a powerful template engine, where the `list` filter is a very practical feature, allowing us to directly convert string-formatted data into an iterable array object in the template, greatly enhancing the flexibility of the template.

2025-11-08

How to split a string into an array using a specific delimiter in Anqi CMS template?

In the template development of AnQi CMS, we often encounter situations where we need to process specific strings.For example, a keyword of an article may be stored as a comma-separated string, and we want to display them as separate tags on the front end.At this point, splitting a string into an array with a specific delimiter becomes the key step in handling such issues.The AnQi CMS template engine supports syntax similar to Django, and it provides a rich set of filters (filters) to help us process data.

2025-11-08