In the world of AnQiCMS, the flexibility and efficient management of content are its core advantages.AnQiCMS can provide powerful and intuitive support for publishing corporate news, product details, or building complex content matrices for multiple sites.forLoop tag, to iterate and displaypageListAll single page content returned by the tag.

AnQiCMS single page: flexible content foundation

Let's take a simple review of the 'single-page' feature in AnQiCMS.In the background, we can easily create and manage various independent pages, such as "About UsThese single pages not only support custom URLs, SEO titles, keywords, and descriptions, but also allow setting independent templates and Banner images, greatly enriching the expression form of the website content.They are an indispensable part of building the basic information and core business introduction of a website.

However, it is not enough to create these pages in the background, we also need to dynamically display them on the front end of the website, such as part of the navigation menu, or in some area to showcase all service pages.This is where AnQiCMS' powerful template tags and loop controls come into play.

Core tags:pageList--Get all single page data

To traverse a single page, we first need to extract the data of these pages. AnQiCMS provides a special template tag for this.pageList。Its usage is very direct and concise.

When you write in the template{% pageList pages %}at that time, the system will automatically retrieve all the single-page data that has been created, and store them in a folder namedpagesThe variable is in.pagesA variable is an array object, where each element represents the complete information of a single page.pageListThe tag itself does not require complex parameter configuration, the default settings can obtain all single-page sites, which lays a solid foundation for our subsequent traversal operations. Of course, if you are in a multi-site environment and need to obtain data from specific sites,pageListAlso supports throughsiteIdspecified by parameters, but for most cases, the default usage is already sufficient.

Each single-page (in the loop, we usually call it)itemAll of them carry rich field information, such asId(Single Page ID),Title(Title),Link(Page Link),Description(description),Content(Page Content),Logo(Thumbnail Large Image) as well asThumb(Thumbnail)et al. These fields enable us to flexibly display various aspects of the page in the front-end template.

Efficient iteration:forto iteratepageListdata

OncepageListFill single-page data intopagesIn the variable, we can bring out another very powerful tag in the AnQiCMS template.forLoop.forThe loop tag is specifically used for iterating over arrays, slices, or other iterable objects, allowing us to access each one individuallypagesEach single page in the variable, and display its content on the web page.

The most basicforThe loop syntax is as follows:

{% pageList pages %}
    {% for item in pages %}
        {# 在这里可以访问item的各种属性 #}
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>{{ item.Description }}</p>
    {% endfor %}
{% endpageList %}

In this code,itemIs the temporary variable representing the current single page in each loop. Byitem.Title/item.Linkand so on, we can easily obtain and display the title and link of each single page.

Advanced Application and Consideration

In actual project development, we often need more fine-grained control and richer display effects. AnQiCMS'sforLoop tags provide various auxiliary functions, making template development more intuitive.

  1. Handling empty list:{% empty %}Elegant fallbackIfpageListNo single page is returned, we do not want the page to be blank or show errors. At this time,forLooping{% empty %}the branch becomes particularly important. It allows us to provide a friendly prompt when the data is empty:

    {% pageList pages %}
        {% for item in pages %}
            {# ... 展示单页面内容 ... #}
        {% empty %}
            <p>目前还没有任何单页面内容发布。</p>
        {% endfor %}
    {% endpageList %}
    
  2. Loop auxiliary variable: master each iterationInforInside the loop, AnQiCMS provides some built-in loop auxiliary variables that can help us better control the display logic of content:

    • forloop.CounterThe current loop index is represented by : and it starts counting from 1. Very suitable for numbering list items.
    • forloop.RevcounterThe value means the remaining loop count.These variables can help us achieve more complex layout or style control, such as adding unique styles to the first, last, or specific position elements.
  3. Conditional judgment:{% if %}Flexible filtering of theSometimes, we may want to exclude certain specific pages while iterating through all single pages, or conditionally render based on a certain attribute of the page. AnQiCMS'sifLogical judgment label can be well integrated withforCombined with loops:

    {% pageList pages %}
        {% for item in pages %}
            {# 假设我们想排除ID为5的特定单页面 #}
            {% if item.Id != 5 %}
                <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
                {# ... 其他内容 ... #}
            {% endif %}
        {% empty %}
            <p>目前还没有任何单页面内容发布。</p>
        {% endfor %}
    {% endpageList %}
    

Complete code example: Dynamically display all service pages

Let us combine the above techniques to build a more comprehensive example, dynamically displaying all service pages of the website in a sidebar:

`twig