In the world of AnQiCMS, the flexibility and efficient management of content are its core advantages.Whether it is to publish corporate news, product details, or build a complex content matrix for multiple sites, AnQiCMS can provide powerful and intuitive support.Today, as an expert operator who understands the way, I want to talk to everyone about a very practical skill in AnQiCMS template making: how to use it cleverlyforLoop tag to iterate and displaypageListAll single page content returned by the tag.

AnQiCMS Single Page: Flexible Content Foundation

Let's briefly review the 'single-page' feature in AnQiCMS.In the background, we can easily create and manage various independent pages, such as "About Us", "Contact Information", "Service Details", or any other pages that need to be displayed separately and have relatively fixed content.These single pages not only support custom URLs, SEO titles, keywords, and descriptions, but also allow for independent templates and Banner images, greatly enriching the expression form of the website content.They are an indispensable part of building a website's basic information and core business introduction.

However, it is not enough to have created these pages in the background; we also need to dynamically display them on the website front-end, such as as part of the navigation menu or in a specific area to showcase all service pages.At this moment, the powerful template tags and loop control of AnQiCMS 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 use is very direct and concise.

When you write in the template{% pageList pages %}the system will automatically retrieve all the single page data that has been created and store them in a folder namedpages. ThispagesA variable is an array object, each element representing a complete piece of information for a single page.pageListThe tag itself does not require complex parameter configuration, the default can be used to get all single pages on the entire site, which lays a solid foundation for our subsequent traversal operations. Of course, if you are in a multi-site environment and need to get data for specific sites,pageListit also supports getting throughsiteIdThe parameter can be specified, but the default usage is sufficient for most cases.

Each single page (which we usually call it in a loop)item) carries rich field information, such asId(Single Page ID),Title(Title),Link(Page Link),Description(Description),Content(Page Content),Logo(Thumbnail Full Image) andThumb(Thumbnail) etc. These fields allow us to flexibly display various aspects of the page in the front-end template.

Efficient iteration:forLoop throughpageListdata

OncepageListTo fill the single-page data intopagesIn the variable, we can call out another very powerful tag in the AnQiCMS template——forLoop.forThe loop tag is specifically used to iterate over arrays, slices, or other iterable objects, allowing us to access them one by onepagesEach single page in the variable, and present 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 block,itemIt represents a temporary variable for the current single page in each loop. Throughitem.Title/item.LinkIn this way, 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 a variety of auxiliary functions, making template development more intuitive.

  1. Handle empty list:{% empty %}The elegant fallback.IfpageListNo single page is returned, we do not want the page to be blank or show errors. At this point,forrepeatedly{% 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.Counter: Represents the current loop index, starting from 1. Very suitable for numbering list items.
    • forloop.Revcounter: Indicates the remaining number of loops. 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.Sometimes, we may want to exclude certain pages when iterating through all single pages, or conditionally render based on a page's properties. AnQiCMS'sifLogical judgment labels 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's combine the above techniques to build a more完善的 example, dynamically displaying all service pages of the website in a sidebar:

`twig