How to display all single page lists using the `pageList` tag in Anqi CMS and exclude specific pages?

AnQi CMS is a flexible and efficient content management system that provides strong support for our website operations, making it easy to publish articles, manage products, or create various single-page applications. Today, let's talk about a very practical skill when using AnQi CMS to manage single-page applications: how topageListIt displays all single pages and can accurately exclude the specific pages we do not want to show.

In daily website operations, we often create single pages such as "About UsMost of the time, we hope to list these pages uniformly in the navigation menu or some area, for the convenience of users.pageListIt can help us easily obtain the list of all single pages on the website.

pageListBasic usage of tags

To get the list of all single-page views of the website,pageListThe usage of tags is very intuitive. We usually write template code like this:

{% pageList pages %}
    <ul class="page-nav">
    {% for item in pages %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
    {% endfor %}
    </ul>
{% endpageList %}

In this code,{% pageList pages %}It will fetch all single-page data from the background database and store it in a variable namedpages. Then, we use{% for item in pages %}Loop through thispageslist,itemwhich represents each single page in the list. Through{{ item.Link }}We can get the link of the page, and{{ item.Title }}then we can display the title of the page. In this way, a navigation list containing all single pages is presented in front of us.

Flexible customization: Exclude specific pages

AlthoughpageListIt is very convenient to list all pages, but in actual operation, we may encounter such needs: some single pages, such as internal drafts, temporary event pages, or some pages that are only accessible through specific links and do not want to appear in the general list, we do not want them to appear in the list bypageListGenerated list.

Anqi CMS'spageListThe label itself does not provide a direct "exclude ID" parameter, but its template engine is very flexible, allowing us to make conditional judgments while iterating through the list, thus achieving customized content display.This is the key to exclude specific pages.

We can achieve this byforadding inside the loopifCondition judgment, to check whether the current page being traversed is the page we want to exclude.If the exclusion conditions are met, we will skip this page and not display it.The most commonly used criterion is the page ID, because the page ID is unique and stable.

Operation Steps:

  1. Get the page ID to exclude:Login to the security CMS backend, enter 'Page Resources' under 'Page Management'.Here, you can see the 'ID' column of each single page.Note down the ID of the page you want to exclude.

  2. Add exclusion logic to the template:Suppose we want to exclude ID as1(such as the early version of “About Us” or an internal test page) and ID as5(for example, “Special Event Login Page”) page. We can modify the template code above by addingifJudgment:

    {% pageList pages %}
        <ul class="page-nav">
        {% for item in pages %}
            {# 判断当前页面的ID是否为1或5。如果是,则不显示它 #}
            {% if not (item.Id == 1 or item.Id == 5) %}
            <li class="page-item">
                <h3 class="page-title"><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
                {# 你还可以根据需要显示更多页面信息,例如缩略图或简介 #}
                {% if item.Thumb %}
                <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="page-thumb">
                {% endif %}
                <p class="page-description">{{ item.Description|truncatechars:100 }}</p>
                <a href="{{ item.Link }}" class="read-more">查看详情</a>
            </li>
            {% endif %}
        {% endfor %}
        </ul>
    {% endpageList %}
    

    In this modified code,{% if not (item.Id == 1 or item.Id == 5) %}This line is core. It first checks the ID of the current page (item.Id) whether it is equal to1or5. If it equals any of them, thenitem.Id == 1 or item.Id == 5this condition will be true, and the previousnotIt will reverse it to false, thereby skipping.<li>...</li>This code, the page will not be rendered. Only when the page's ID is neither.1Nor is it.5whennot (...)The condition must be true for the page content to be displayed normally.

    If you only want to exclude one page, the code will be more concise:{% if item.Id != 1 %}.

Consideration of practical application scenarios

This method of excluding specific pages is very useful in many scenarios:

  • Draft or test pages:The website may have some incomplete or test single pages during development or content update, and we do not want them to be mistakenly clicked by users.
  • Non-public pages:Some single-page applications may be for internal use only or accessed through specific promotional links, without being displayed in public navigation.
  • Redundant or expired pages:If the website has some single pages that have been merged or outdated, but are temporarily not convenient to delete due to historical data or SEO considerations, we can also exclude them from the main list.

Through this approach, we can flexibly control the display of content without modifying the core tag function, makingpageListLabels are more aligned with our refined operational needs while maintaining their convenience.


Common Questions and Answers (FAQ)

1. Besides using ID, can I exclude pages through other means?Of course. Eachitem(Single-page) object in addition toIdalso includesTitle(Title),Link(Link),Description(Description) etc. You can judge according to these fields. For example, if you want to exclude pages with the word "internal" in the title, you can use{% if not (item.Title|contain:"内部") %}.IdIt is usually the most stable and recommended method, as the ID is a unique numeric identifier that is not easily affected by content changes or URL structure adjustments.

2. If I want to exclude a lot of pages, writing them one by oneorwouldn't it be麻烦?when there are many page IDs to exclude, usingorThe connection indeed makes the code verbose. A more concise way is, you can first define a list of IDs to exclude in a variable, and then checkitem.IdDoes it include in this list. For example,{% set exclude_ids = [1, 5, 10, 12] %}Then judge in the loop.{% if not (item.Id in exclude_ids) %}This can greatly improve the readability and maintainability of the code.

3. Is this exclusion logic dynamic? If I change the status of the page in the background (for example, set to draft), will it take effect immediately in the list?Yes, this exclusion logic is dynamic.Each time a user visits the page, the Aiqi CMS will execute the template code in real-time to generate content.pageListGenerated list, as long as they trigger the one you setifNo need to manually clear the cache.