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.