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

AnQi CMS is a flexible and efficient content management system that provides strong support for our website operations, whether it is publishing articles, managing products, or creating various single pages, it shows great skill. Today, let's talk about a very practical skill when using AnQi CMS to manage single pages: how topageListThe tag displays all single pages and can accurately exclude specific pages that we do not want to display.

In the daily operation of websites, we often create some single-page pages such as "About Us", "Contact Information", "Privacy Policy" and so on.Most of the time, we hope to list these pages uniformly in the navigation menu or some area for easy user access.AnQi CMS provides a convenient template tag for this——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 lists of the website,pageListThe use 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 block,{% pageList pages %}Will fetch all single-page data from the background database and store it in a variable namedpages. Next, we use{% for item in pages %}Loop through thispagesthe list,itemto represent each single page in the list. By{{ 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 appears in front of us.

Flexible customization: Exclude specific pages

AlthoughpageListIt is 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 bypageListThe generated list contains.

Of Security CMSpageListThe tag itself does not provide a direct 'exclude ID' parameter, but its template engine is very flexible, allowing us to make conditional judgments while traversing the list, thus achieving customized content display.This is the key to excluding specific pages.

We can do this byforAdd within the loopifCondition judgment, to check whether the current page being traversed is the page we want to exclude.If it meets the exclusion criteria, we will skip this page and not display it.The most commonly used criterion is the page ID, as the page ID is unique and stable.

Operation steps:

  1. Get the page ID to exclude:Log in to the AnQi CMS backend, go to the 'Page Resources' under 'Page Management'.Here, you can see the 'ID' column for each single page.Write down the ID of the page you want to exclude.

  2. Add the exclusion logic in the template:Assuming we want to exclude ID of1(For example, an early version of "About Us" or an internal test page) and ID is5(For example, the "Special Event Login Page") page. We can modify the above template code to addifJudgment:

    {% 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 the modified code section,{% 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 is equal to any of them, thenitem.Id == 1 or item.Id == 5this condition will be true, the previousnotIt will be converted to false, thus skipping<li>...</li>This code, the page will not be rendered. Only when the ID of the page is not1nor5then,not (...)The condition is true, the page content will 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 way of excluding specific pages is very useful in many scenarios:

  • Draft or test pages:During development or content update, some incomplete or testing single pages may exist, and we do not want them to be mistakenly clicked by users.
  • Non-public page:Some single pages may be for internal use only, or accessible through specific promotional links, without needing to be exposed in public navigation.
  • Redundant or outdated pages: If the website has some merged or outdated single pages that are not convenient to delete temporarily for historical data or SEO considerations, we can also exclude them from the main list.

In this way, we can flexibly control the display of content without modifying the core tag function, allowingpageListLabels maintain their convenience while also fitting our refined operational needs.


Frequently Asked Questions (FAQ)

1. Besides using ID, can I exclude pages in other ways?Of course, each.item(Single-page) The object also excludesIdand includesTitle(Title),Link(Link),Description(Description) and other fields. You can judge according to these fields. For example, if you want to exclude pages with the title containing 'internal', you can use{% if not (item.Title|contain:"内部") %}. However, usingIdIt 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 a lot of page IDs to exclude, usingorIndeed, connecting makes the code verbose. A more concise way is to first define a list of IDs to exclude in a variable, then checkitem.IdIs it included 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, to draft), will it take effect in the list immediately?Yes, this exclusion logic is dynamic. Each time a user visits the page, the Anqi CMS will execute the template code in real-time to generate content.This means that if you modify the page ID in the background or change the content of a field used for judgment (such as the title), these changes will be reflected immediately inpageListGenerated list, as long as they trigger the conditions you setifNo manual cache clearing is needed.