Skillfully control content display: AnQi CMStag-pageListSkip a specific page in the loop
As an experienced website operations expert, I fully understand that the flexibility of a content management system is crucial for operational efficiency and user experience.AnQiCMS (AnQiCMS) provides us with powerful content management capabilities with its high-performance architecture based on the Go language.In daily operations, we often encounter scenarios where we need to fine-tune content control, such as skipping or not displaying certain pages in a page list loop.tag-pageListHow to cleverly skip a specific page when displaying single page content in a loopIdto meet the demand of diversified content presentation.
Why do we need to skip a specific page?
In AnQi CMS,pageListLabels can conveniently list the "single-page" content created on our backend (such as "About UsThese single pages are crucial for the basic information construction of the website, but they are not suitable for full display in all scenarios.
For example, you might have a 'All Pages' navigation menu, where you want to list most of the general single-page, but some special, internally used, or pages that you do not want users to access directly through the list (such as a test page, a temporary event page, or a legal statement page that can only be accessed through a specific entry) should not appear in this list. At this point, ifpageListThe tag does not provide a direct 'exclude ID' parameter, so we need to rely on the strong logical control capabilities of the AnQiCMS template engine to achieve this goal.
AnQiCMS's solution: conditional judgment within the loop
AnQiCMS template engine supports Django template syntax, which means we can iterate over the looppageListUtilize the data obtainedifScreen and exclude pages that do not meet our requirements using conditional judgment tags. This method provides an extremely flexible and powerful content control mechanism.
First, let's reviewpageListThe basic usage of the tag. It retrieves all the single page content from the background and encapsulates it into an array object that can be traversed.
<ul>
{% pageList pages %}
{% for item in pages %}
<li>
<a href="{{ item.Link }}">{{ item.Title }}</a>
</li>
{% endfor %}
{% endpageList %}
</ul>
This code will list all single-page titles and links. Now, suppose we want to skipIdWith1the "About Us" page, as well asIdWith5The "Privacy Policy" page, do not let them appear in this list. We can useforinside the loop, usingifconditional judgment tags and pagesitemthe object'sIdattributes to achieve this.
item.IdIs AnQiCMS automatically generated unique identifier for each single page. We can compare thisIdto decide whether to display the current page in the loop.
In order to make the template output cleaner, we can also make good use of{%-and-{%This 'remove logical tag line' format, it will intelligently remove the leading and trailing white spaces before and after the tag, making the generated HTML code more compact.
Example of skipping a single specific page:
If we only want to skipIdWith1the page, the code can be written as:
<ul>
{% pageList pages %}
{% for item in pages %}
{%- if item.Id != 1 %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{%- endif %}
{% endfor %}
{% endpageList %}
</ul>
In this example,{%- if item.Id != 1 %}It will check the current pageitemofIdwhether it is not equal to1. Only when the condition is true,<li>Tags and the page titles and links they contain will be rendered and output.
Example of skipping multiple specific pages:
If you need to skip multiple non-continuous pages, for exampleIdWith1and5The page, where we can use logical operatorsandTo connect multiple conditions:
<ul>
{% pageList pages %}
{% for item in pages %}
{%- if item.Id != 1 and item.Id != 5 %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{%- endif %}
{% endfor %}
{% endpageList %}
</ul>
So, only whenitem.IdNot equal to1Nor equal to5The page will be displayed.
A more flexible exclusion method: usecontainFilter out the IDs from the exclusion list
Use it when there are many page IDs to exclude.and item.Id != XThe way might make the code look long. AnQiCMS providescontainfilter, combinedlistA filter that can handle this situation more elegantly. We can first define a listIdcontaining all the IDs to be excluded, and then judge whether the current page is in this list.
{% set excluded_ids = '[1, 5, 8, 12]'|list %} {# 定义一个包含要排除ID的列表 #}
<ul>
{% pageList pages %}
{% for item in pages %}
{# 检查当前页面ID是否不在排除列表中 #}
{%- if not (excluded_ids|contain:item.Id) %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{%- endif %}
{% endfor %}
{% endpageList %}
</ul>
In this more advanced example, we first use{% set excluded_ids = '[1, 5, 8, 12]'|list %}Defined a namedexcluded_idsan array that contains all the page IDs we want to skip. Then, inside the loop,not (excluded_ids|contain:item.Id)this line of code judgesitem.Idwhether it existsexcluded_idsIf in the list.item.IdNotIn this list, the current page will be displayed normally. This method can significantly improve the clarity and maintainability of the template code when there are a large number of IDs to exclude.
Application scenarios and **practice
This kind ofpageListMethod of skipping specific pages through conditional judgment in a loop, which has a wide application in actual operation:
- Simplify navigation and content aggregation:On the footer, sidebar, or some special topic page of the website, you may only need to display some selected or important single pages, rather than showing all pages, to keep the interface clean and focus on the user.
- A/B testing or temporary activity page:For some single pages that are undergoing A/B testing or temporary promotion, you may not want them to appear in the regular page list to avoid disturbing the test results or prematurely revealing information.
- Internal Management or Draft Page:The website backend may have some draft pages that are not yet completed, or management pages that are only for internal staff viewing, these pages must never appear in any public-facing list.
- SEO optimization:Some single-page content may have a lower value or contain duplicate content, which is not suitable for extensive indexing by search engines. By excluding it from the list, it can be prevented from being exposed in sitemaps or other aggregation pages, thereby more effectively controlling the search engine crawling.
By flexibly using the conditional judgment capability of the AnQiCMS template engine, you can control the website