In AnQiCMS template, how to determine if a certain `flag` attribute (such as `recommended` or `headline`) is set?

Calendar 👁️ 88

As an experienced website operations expert, I am well aware that the flexibility of content display is crucial for user experience and operational efficiency in my daily work.AnQiCMS (AnQi Content Management System) is chosen by many enterprises and operators largely due to its powerful customizability and simple and efficient template engine.flagWhether the attribute (such as "recommended" or "headline") is set and adjust the content display accordingly.

Use AnQiCMS's 'Flag' attribute to make the content more expressive

In the AnQiCMS backend, when we edit documents, we will see an option for "recommended properties". Here, such as头条[h]/推荐[c]/幻灯[f]/特荐[a]Multiple identifiers.These "Flag" properties are powerful tools provided by AnQiCMS for content operators, allowing them to classify and tag documents in a fine-grained manner, and then dynamically change the style, layout, or even display additional functional modules in the front-end template.For example, an article marked as "top news" may want to have a more prominent title color or a special corner mark on the homepage list; while a product marked as "recommended" may need to be placed in the "hot recommendations" area of the sidebar.

Understanding the principle of these Flag attributes is the first step. According to the AnQiCMS documentation, these attributes are marked by corresponding letters, such ashrepresenting the headline,cRepresents a recommendation. When a document has multiple Flags set, itsFlagThe value will be a string containing all these letters (for example, if it is both headline and recommendation, thenFlagthe value may be "hc").

Get the document's properties in the templateFlagProperty value

To judge the Flag attribute of the document in the template, we first need to correctly obtain the value of this attribute. AnQiCMS provides two main scenarios to obtain the document'sFlagattribute:

  1. In the document detail page:When you are on the detail page of a document, you can usually directly go througharchiveVariables (or usearchiveDetailTag) to access various properties of the current document. At this point, obtaining the Flag attribute is very direct, just by using{{ archive.Flag }}.

    <p>当前文档的 Flag 属性值:{{ archive.Flag }}</p>
    
  2. In the document list loop:When you usearchiveListWhen looping through multiple documents, if you want to access the properties of each document within the loop,Flagyou need to pay special attention to one parameter of thearchiveListtag:showFlag="true"only when this parameter is set totruewhen, each document'sitem.Flagproperty will be loaded and accessible in the loop. For example:

    {% archiveList articles with type="list" limit="10" showFlag=true %}
        {% for item in articles %}
            <li>{{ item.Title }} - Flag: {{ item.Flag }}</li>
        {% endfor %}
    {% endarchiveList %}
    

    here,itemrepresents each document in the loop,item.Flagwill return the Flag attribute string of the document.

Precise judgment: usecontainFilter

obtaining toFlagAfter the attribute value, the key is how to determine whether the string contains the specific Flag letter we want. The AnQiCMS template engine has built-in powerful filter mechanisms, whichcontainThe filter is exactly the tool to solve this problem.

containA filter used to determine if a string (or array) contains a specific keyword (substring). It returns a boolean value (trueorfalse) which makes it very suitable for use withifLogical judgment tags are used together.

Here are some specific usage examples:

1. Determine if the document is set as "Top News":

Suppose you want to check the current document (or the document in the list ofitem) Does it have the 'Top News' (h) attribute. You can write it like this:

{% if archive.Flag|contain:"h" %}
    <span class="badge headline-icon">头条</span>
{% else %}
    <span class="badge normal-item">普通文章</span>
{% endif %}

Or in a list loop:

{% archiveList articles with type="list" limit="10" showFlag=true %}
    {% for item in articles %}
        <li {% if item.Flag|contain:"h" %}class="highlight-headline"{% endif %}>
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            {% if item.Flag|contain:"h" %}
                <i class="icon-headline"></i>
            {% endif %}
        </li>
    {% endfor %}
{% endarchiveList %}

2. Determine if the document is set to 'Recommended' or 'Slideshow':

If you want to determine whether the document is a "recommended" (c) or a "slide" (f) among any of them, you can use the logicaloroperator:

{% if archive.Flag|contain:"c" or archive.Flag|contain:"f" %}
    <span class="badge featured-slideshow">推荐/幻灯内容</span>
{% endif %}

3. Store the judgment result in a variable for better readability (use)setTag):

When your judgment logic is complex or you need to use the same judgment result multiple times, you can store it in a variablecontainAssign the result of the filter to a new variable, which makes the template code clearer:

{% set isRecommended = archive.Flag|contain:"c" %}
{% set isHeadline = archive.Flag|contain:"h" %}

{% if isRecommended and not isHeadline %}
    <p>这是一篇推荐但非头条的文章。</p>
{% elif isHeadline %}
    <p>这是一篇醒目的头条文章!</p>
{% endif %}

This method is particularly useful when the template code is long, as it helps you better manage and understand the business logic.

Application scenarios and **practice

Mastering the method of judging the Flag attribute, you can achieve various dynamic content display effects in the AnQiCMS template:

  • Distinguishable display of icons or text:Show labels such as 'Hot', 'Recommended', 'Original', etc. next to the article title based on the Flag.
  • Adjust the layout and style:With

Related articles

How to display a specific content block based on the `archiveDetail` or `categoryDetail` Id value?

As an experienced website operations expert, I am well aware that how to flexibly display specific content in content management is crucial for improving user experience and achieving operational goals.AnQiCMS (AnQiCMS) takes advantage of its powerful template engine and rich tag system, providing us with great convenience.Today, let's delve into how to cleverly use the `archiveDetail` and `categoryDetail` tag's `Id` value to accurately control the display of content blocks on the website.

2025-11-06

How to judge whether the current item in the list loop is the first or last in the AnQiCMS template?

## Mastering the List Loop in AnQiCMS Templates: A Clever Trick to Identify Head and Tail Items Displaying data lists dynamically is a common requirement in website content operation.Whether it is an article list, product display, or image gallery, we hope to make the information more attractive through unique visual effects or logical processing.

2025-11-06

How to use the `if` tag to check if `archiveList`, `categoryList`, etc., are empty?

As an experienced website operations expert, I know that paying attention to the user experience in content management and website presentation is crucial.A meticulously designed website that not only provides rich content but also maintains elegance and usability even when content is missing.How can one effectively judge whether the list data is empty and flexibly display content accordingly, which is a basic and key skill in the design of front-end templates.In AnQiCMS (AnQiCMS), we take advantage of its powerful and easy-to-use Django template engine syntax, which can very elegantly achieve this.

2025-11-06

How to check for content inclusion using the `{% if "keyword" in variable %}` condition in AnQiCMS templates?

As an experienced website operations expert, I know that the flexible use of templates is the key to improving website efficiency and user experience in daily content management.AnQiCMS (AnQiCMS) provides us with powerful content customization capabilities with its efficient architecture based on the Go language and support for Django template engine syntax.

2025-11-06

How to determine if there is a previous or next document on the document detail page and display navigation links?

As an experienced website operations expert, I know that a smooth reading experience is crucial when users browse content.A good document detail page should not only display the core content but also guide users to the next step, such as reading related content or easily switching to the previous or next page.This not only increases the user's stay time on the website and reduces the bounce rate, but it is also SEO-friendly, helping search engines to better crawl and understand the website structure.In AnQiCMS (AnQiCMS), implement the previous and next navigation links on the document detail page

2025-11-06

How to judge whether the website is closed and display a prompt based on the `SiteCloseTips` of the system settings (`{% system %}`)?

As an experienced website operations expert, I know that the stable operation of the website is of great importance, but sometimes, due to reasons such as maintenance, upgrades, or data migration, the website has to temporarily shut its doors.How to communicate with visitors elegantly during a closure period, ensuring user experience while effectively managing expectations, is a detail that every operator needs to pay attention to.AnQiCMS (AnQiCMS) provides a flexible mechanism in this regard, allowing us to deal with it easily.Today, let's delve deeply into how to set up tags in Anqi CMS through system settings `{% system

2025-11-06

How to judge whether a contact field (such as Qrcode) in the AnQiCMS template has a value?

In website operation and template development, we often need to dynamically adjust the display of the front-end page based on the setting of the background content.Especially for important information such as contact information, ensure that it is displayed after it exists, which can effectively avoid broken links, blank areas, or incomplete information on the page, thereby improving the user experience.Today, let's delve into how to elegantly and accurately determine whether a contact field (such as `Qrcode`) obtained by the `contact` tag in the AnQiCMS template has been set.

2025-11-06

How to highlight the current page in the `navList` navigation menu based on the `IsCurrent` property?

In the fast-paced digital world, a clear and intuitive website navigation menu is the foundation of a good user experience.As an experienced website operations expert, I am well aware of AnQiCMS's powerful capabilities in providing efficient and customizable content management solutions.It not only performs excellently but also considers the operator's needs in detail, for example, the built-in `navList` tag combined with the `IsCurrent` attribute can easily highlight the current page, greatly enhancing the professionalism and user-friendliness of the website.

2025-11-06