In AnQiCMS templates, we often encounter scenarios where we need to display some small, relatively fixed list data, such as the featured functions of a page, the advantages of a product series, or auxiliary navigation links, etc.The traditional method may require creating a special content model on the back end, then publishing several pieces of data, and finally calling it through template tags.But this seems somewhat cumbersome for data that does not change frequently and is limited in quantity.
In order to handle such requirements more efficiently and flexibly, AnQiCMS provides a way to define arrays directly in the template (listA convenient way, it can help us simplify the development process, making the template more expressive.
What are the benefits of defining an array directly in the template?
Define arrays directly in the AnQiCMS template, mainly bringing the following conveniences:
first, Improve development efficiency and iteration speed.For some page-specific, small content lists that do not involve database storage, we can quickly define and render them directly in the template file.This means there is no need to log in to the backend to create content models, publish content, which greatly shortens the development cycle, especially when the front-end page needs to frequently adjust these small lists, you can directly modify the template file, take effect immediately, greatly improving work efficiency.
secondly,Enhance the flexibility and self-sufficiency of the template.In some cases, we may need to temporarily define a set of options or data based on the requirements of a specific page or component, such as filter conditions for a module, a set of fixed icon class names, etc.Embed these data directly into the template, which can help the component better encapsulate its logic and data, reduce dependence on global configuration or backend data, and make the template itself more modular and portable.
Moreover,Simplify data management and reduce maintenance costs.If all list data is managed through the background content model, even a small amount of fixed and unchanging data will consume database resources and may increase the complexity of background management.By defining arrays in the template, we can differentiate this "lightweight" data from "heavyweight" content (such as articles, products), making the backend focus on core content management, and also making the template structure clearer, easier to understand, and maintain.
Finally,Ease rapid prototyping and testing.At the early stage of template development, real data may not be ready.Define the mock data array directly in the template, which can quickly build the page skeleton and functions, conduct style and interaction testing, and then replace it with dynamic data after the backend data interface is perfected for a smooth transition.
How to define an array in AnQiCMS template?
In AnQiCMS, with its powerful filter function, we can easily define an array in the template. This is mainly throughsetLabel collaborationlistTo implement a filter.
Define the basic syntax of the arrayThat's it:
{% set 变量名称 = '["元素1", "元素2", "元素3", ...]'|list %}
Let's take a detailed look at this syntax:
{% set 变量名称 = ... %}This is a tag used in AnQiCMS templates to define variables. You can变量名称Replace it with any meaningful name you want, such asmyFeatures/navItemsetc.'["元素1", "元素2", "元素3", ...]'This is the string format of defining array content. It needs to be enclosed in single or double quotes to indicate that it is a string. The elements of the array are enclosed in square brackets inside the string.[]Enclosed, elements separated by English comma,Separate. Each element itself can also be enclosed in quotes (especially when the element is a string), but quotes are optional for types such as numbers.|listThis is the key.listA filter. Its purpose is to parse and convert the previously defined string into an array (under the hood in Go, it is handled as a[]string{}type, that is a string slice).
Define an array exampleYou can try it like this:
{# 定义一个包含特色功能的数组 #}
{% set myFeatures = '["高效管理", "灵活定制", "安全稳定", "易于扩展"]'|list %}
{# 定义一个包含图片URL的数组 #}
{% set imageGallery = '["https://en.anqicms.com/uploads/img1.jpg", "https://en.anqicms.com/uploads/img2.jpg", "https://en.anqicms.com/uploads/img3.jpg"]'|list %}
{# 定义一个包含混合类型(但最终都会被视为字符串)的数组 #}
{% set mixedData = '["AnQiCMS", 2023, true, "GoLang"]'|list %}
After defining the array, we can use it flexibly in the template.
The most common usage is to cooperate withforLoop to iterate over the array, display each element:
<div class="features-list">
<h3>我们的核心优势:</h3>
<ul>
{% for feature in myFeatures %}
<li>{{ feature }}</li>
{% endfor %}
</ul>
</div>
If you need to access a specific element in the array, you can use an index (starting from 0):
<p>第一个特色功能是:{{ myFeatures[0] }}</p>
<img src="{{ imageGallery[1] }}" alt="第二张图片">
It is worth noting that, due tolistThe filter will convert all elements to string type, even if you define numbers or boolean values, they are still strings in the template. For most display needs, this will not cause any trouble, but if you want to perform arithmetic operations on numbers, you may need to use other filters such as|integeror|floatPerform an explicit conversion.
In general, the AnQiCMS feature of directly defining arrays in templates provides us with a lightweight, efficient data processing method, making template development more convenient, and also allowing for more flexible and diverse presentation of website content.
Frequently Asked Questions (FAQ)
1. What are the data types of array elements defined in the template?
When you use AnQiCMS template,|listWhen defining an array filter, regardless of whether you write numbers, boolean values, or other data that looks like different types in the string, they will ultimately be processed by the system as strings([]string{}This means that even if you defined'["123", 456]'|listto accessmyArray[1]the value obtained at the time"456"It is still a string, not an integer. For most display needs, this is usually fine, but if you need to perform mathematical calculations, you may need to use it first.|integeror|floatConvert using filters.
Can the array defined in this way be queried or filtered complexly like background data?
Not allowed.The array defined directly in the template is static, its values are determined at the time of template parsing, and it does not support complex dynamic queries, sorting, or filtering, nor does it support advanced operations such as pagination.This method is more suitable for displaying fixed, finite list data.archiveList/categoryListto call.
3. Can I define multiple arrays in a template?
Of course you can.You can define as many arrays as needed within the same template file, just make sure each array uses a different variable name.myFeatures/imageGalleryandsocialLinksMultiple arrays are used, and they are called and displayed in different parts of the template. This flexible definition method allows you to better organize and manage various small static content blocks on the page.