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

Core Method: UselistFilter definition of string array

To define a string array directly in the Anqi CMS template, we can cleverly utilize the template engine providedsetlabel combined withlistFilter.listThe 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 ... %}The tag is used to declare and assign a variable in the template.'["元素1", "元素2", "元素3", ...]'is a standard JSON format string, it represents an array of strings. Then, pass this string to|this string tolistthe already escaped string,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, each element should be enclosed in double quotes, even if it is a number, after passing throughlistThe filtered data will also be stored in the array as a string.

Practical example: Define and traverse the array

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

{% 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 this 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.

Actual application scenarios

  1. Dynamically generate navigation menus or category options:When some navigation items or filter options are fixed and limited in number, arrays can be defined directly in the template to quickly build.For example, the top menu includes "Home
    
    {% 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 feature, for certain specific pages or modules, we may need to add some additional auxiliary keyword prompts. These keywords may not be desired to be globally managed, and that's when the array comes in handy.
    
    {% set seo_tips = '["高效", "定制", "易扩展"]'|list %}
    <p>我们提供{% for tip in seo_tips %}{{ tip }} {% if not forloop.Last %}/{% endif %}{% endfor %}的内容管理服务。</p>
    
  3. Condition judgment and content switching:We can define a string array containing specific states or permissions, thenifThe statement determines whether a variable is included in it to achieve dynamic display of 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 needs to be a string type passed from the backend, andfilter-contain.mdis mentioned in itinoperator.)

Attention Points and **Practice

Define string arrays directly 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 background business logic, we still recommend using the various tags provided by Anqi CMS (such ascategoryList/archiveList/tagListThe content is dynamically retrieved from the database, which can better utilize the advantages of CMS and maintain the maintainability and scalability of the content.

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


Common Questions (FAQ)

Q1: Why can't we use it directly?{% set my_array = ["item1", "item2"] %}This syntax to define an array?A1: The template engine of AnQi CMS (Django-like based on Go language) needs to perform explicit type conversion when assigning template variables. Use directly[...]Syntax is parsed as a plain string in the template, not as an array. Therefore, we need to use|listThe filter to explicitly tell the template engine to parse the JSON format string within 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,listThe filter will parse all elements of the JSON format string and store them as strings in the final[]string{}In array. Even if you write numbers or boolean values in the JSON string, such123ortrue, they are treated as strings in the array."123"and"true"If you need to perform arithmetic operations on these 'numeric' elements, you may need to perform additional type conversions when accessing (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/archiveListThe list returned by tags contains a list of complex objects (for example, each element is a category object containing properties such as ID, title, link, etc.), not simple strings. If you want to extract specific properties (such as all category titles) from these complex objects to form a string array, you need toforIterate over these objects and manually construct a new array of strings.In most cases, it is more convenient to iterate and render over the list of objects provided by the backend data directly, rather than converting it into a plain string array.The definition of string arrays directly in the template is suitable for small lists that are fixed and do not depend on backend data updates.