In AnQiCMS template, how to add a recommended icon to the article title based on the boolean state of the `item.Flag` attribute?

Calendar 👁️ 77

When managing website content in AnQiCMS, we often hope to highlight important articles with some intuitive visual elements, such as adding a prominent small icon to recommended content.This not only attracts visitors' attention but also helps them quickly find high-quality information on the website.Luckyly, AnQiCMS is built-initem.FlagProperties and flexible template engines make it very simple to meet this requirement.

This article will discuss in detail how to implement in AnQiCMS templates based on the article'sitem.FlagProperty status, ingeniously adds a recommendation icon to the article title.

Understandingitem.FlagProperty: recommendation indicator for content

In the AnQiCMS content management system, each article has a feature called 'Recommendation Attribute', which corresponds toitem.FlagThis property. This property is very flexible, it can be used to mark various states of articles, such as:

  • Headline[h]: Indicates that the article is the key news or the most important content on the website.
  • Recommended[c]This indicates that the article is a selected recommended content.
  • Slide[f]This is commonly used for carousel or homepage large image display.
  • Featured[a]Content specially recommended.
  • Image[p]This indicates that the article content includes important images.
  • Jump[j]Clicking the title will jump to an external link.

When editing articles in the background, you can select one or more options in the "Recommended Properties" area. For example, if you select "Recommended", then in the template, this article'sitem.FlagAttributes may contain charactersc. It is worth noting that,item.FlagIt is not a simple boolean value (true/false), but a string that may contain one or more recommended identifier characters, such as "chf" indicating that it has the headline, recommendation, and slide properties.Understanding this is crucial for our subsequent conditional judgments.

Obtained in the templateFlagproperty

To determine and display the recommended icon in the front-end template, first make sure that you have correctly obtained the article list byFlagThe property is also obtained. AnQiCMS providesarchiveLista tag to traverse the article list. When you use this tag, you need to add an extrashowFlag=trueparameter so that each article'sitem.FlagThe property will be available in the loop.

Suppose you are on a list page of articles (such as a category list page or home page) and you want to display the article title and its recommended icon, the basicarchiveListLabel calling may be as follows:

{% archiveList archives with type="list" categoryId="1" limit="10" showFlag=true %}
    {% for item in archives %}
        <li>
            <a href="{{ item.Link }}">
                {# 这里是放置图标的位置 #}
                <h5 class="article-title">{{ item.Title }}</h5>
            </a>
            <p class="article-description">{{ item.Description }}</p>
            {# ... 其他文章信息 ... #}
        </li>
    {% empty %}
        <li>暂无文章可供展示。</li>
    {% endfor %}
{% endarchiveList %}

Note the code in the above mentioned:showFlag=trueIt ensures that:item.FlagIt is crucial to access:

To implement icon display: utilizingcontainThe filter makes conditional judgment

Sinceitem.FlagIs a string, we need to determine whether this string contains a specific recommendation identifier. The AnQiCMS template engine has built-in powerful filters, among whichcontainThe filter is very suitable for this scenario.containThe filter can determine whether one string contains another substring and returns a boolean value (True or False).

For example, to determine if an article has beenitem.Flagwhether it includescrecommended (i.e.,{% for item in archives %}we can use the following conditional

{% if item.Flag|contain:'c' %}
    <i class="fas fa-star recommend-icon"></i> {# 假设使用FontAwesome的星星图标 #}
{% endif %}

here,item.Flag|contain:'c'Will checkitem.Flagto check if a string contains lettersc. If it exists, it will render one out<i> and add a tag for itfas fa-starandrecommend-icon these two CSS classes.fas fa-starIt is usually the star icon represented in the FontAwesome icon library, andrecommend-iconIt is the style class you can customize, which can be used to control the color, size, and position of icons. Of course, you can also replace it with the icon library you are actually using.<i>The class name of the label.

Practice exercise: Add recommendation icons to the article list

Now, let's integrate the knowledge mentioned above and add icons to the titles of recommended articles in an actual article list.

Firstly, make sure that your template HTML file has already included the required icon library (such as FontAwesome) CSS file, and that you have defined it in your custom CSS.recommend-iconStyle.

/* public/static/css/custom.css 或您的主题CSS文件 */
.recommend-icon {
    color: #ffc107; /* 黄色星星 */
    font-size: 14px;
    margin-right: 5px;
}

/* 如果您想让推荐文章标题有额外样式 */
.article-title.is-recommended {
    color: #007bff; /* 蓝色标题 */
    font-weight: bold;
}

Then, in your AnQiCMS template file (for examplearchive/list.htmlyou can write like this:

{# 假设这是文章列表页,通过archiveList标签获取文章数据 #}
{% archiveList archives with type="page" categoryId="1" limit="10" showFlag=true %}
    {% for item in archives %}
    <article class="article-item">
        <a href="{{ item.Link }}" class="article-link">
            {# 根据item.Flag状态添加推荐图标 #}
            {% if item.Flag|contain:'c' %}
                <i class="fas fa-star recommend-icon" title="推荐文章"></i>
            {% endif %}
            {# 同时可以根据flag添加不同的CSS类来改变标题样式 #}
            <h5 class="article-title {% if item.Flag|contain:'c' %}is-recommended{% endif %}">{{ item.Title }}</h5>
        </a>
        <p class="article-description">{{ item.Description|truncatechars:100 }}</p> {# 截取前100字作为简介 #}
        <div class="article-meta">
            <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
            <span>浏览量:{{ item.Views }}</span>
            {# ... 更多文章元数据 ... #}
        </div>
    </article>
    {% empty %}
        <p>此分类下暂无文章。</p>
    {% endfor %}

    {# 如果是分页列表,别忘了添加分页导航 #}
    <div>
        {% pagination pages with show="5" %}
            {# 分页代码请参考AnQiCMS文档中的pagination标签用法 #}
        {% endpagination %}
    </div>
{% endarchiveList %}

By such settings, when the 'recommended attribute' of the article is checked, a yellow star icon will automatically appear before the title, and the title text will also become blue and bold, thus visually distinguishing it from other articles.

Advanced Application and Thinking

  • Multiple icons and stylesIf:item.FlagContains multiple identifiers, you can add different icons or styles to each identifier. For example,{% if item.Flag|contain:'h' %}<i class="fas fa-fire hot-icon"></i>{% endif %}You can add a flame icon to the headline article.
  • Flexible backend management:Due to the complete display of these icons being based on the background's “recommendation attribute” settings, operation personnel can conveniently control which articles need to be highlighted through simple checkmarks without having to modify any code.
  • Unified styleIt is recommended to plan the differences from the beginning of the designFlagThe corresponding icons and styles to ensure the consistency of the overall visual style of the website

Byitem.FlagAttribute, AnQiCMS provides powerful tools for content operation, allowing website administrators to easily manage the visual priority of content through simple backend operations, enhancing user experience and content marketing effectiveness.


Frequently Asked Questions (FAQ)

1.

Related articles

How to determine if the boolean value of a custom field in the AnQiCMS template is true and dynamically add a CSS class?

In AnQiCMS template development, we often need to adjust the display style of the page based on the specific attributes of the content.This includes dynamically adding CSS classes based on the boolean value of custom fields (i.e., true or false status), thus achieving a more flexible and expressive interface.AnQiCMS with its flexible content model design allows users to create various custom fields for different content types (such as articles, products).These custom fields greatly enrich the expression of content, enabling templates to be highly customized for different business needs.###

2025-11-09

How to combine `if` and `yesno` filters in AnQiCMS templates to display different prompts based on VIP status and expiration time?

In website operations, providing differentiated services and content prompts to different user groups is a key link in improving user experience and achieving business goals.Especially on websites with a VIP membership system, displaying personalized information based on the user's VIP status and even the expiration time of the membership can greatly enhance the user's sense of belonging and willingness to interact.AnQiCMS (AnQiCMS) relies on its flexible template engine and rich tags, filters, making everything simple and efficient.Today, let's discuss how to use the AnQiCMS template

2025-11-09

How to ensure that the AnQiCMS template does not cause page rendering errors when the variable is `nil`?

When building a website with AnQiCMS, we often encounter the situation where template variables may be empty (`nil`).For example, a document may not have a thumbnail set or a custom field may not be filled in.If the template code does not properly handle these potential null values, errors may occur during page rendering, affecting user experience and even causing some website functions to be unavailable.Fortunately, the AnQiCMS template engine provides various mechanisms to help us elegantly handle the cases where variables are `nil`, ensuring the stability and smoothness of the page

2025-11-09

In AnQiCMS template, which filters or tags behave in three states of 'or and not' logic for `nil` values?

In AnQiCMS template development, efficiently and robustly handling data is the key to building dynamic websites.Especially when the data obtained from the backend may contain `nil` (empty) values, it is particularly important to elegantly display content, provide default values, or execute specific logic in the template.AnQiCMS provides various filters and tags that can help us display three states of existence, non-existence, or undefined for `nil` values, thereby enhancing the flexibility and user experience of templates.### One, directly judge whether the data exists: `if`

2025-11-09

How to calculate the number of times a specific keyword appears in a string or array using the `count` filter in AnQiCMS templates?

During the template development process of AnQiCMS, we often need to perform dynamic data analysis or display of page content, such as counting the frequency of a keyword or checking the number of specific elements in a list.At this time, the `count` filter has become a very practical tool.It can help you quickly and efficiently calculate the number of times a specific keyword appears in a string or array, providing the possibility of flexible template presentation.--- ### `count` filter: Accurately count your content In short, `count`

2025-11-09

How can you get the document details of Anqi CMS by document ID?

When using AnQi CMS to manage website content, we often need to retrieve detailed information about specific documents from the backend, whether it is to display content on custom pages or integrate data into other systems (such as mini-programs, mobile applications, etc.).AnQi CMS provides a powerful and flexible API interface, one of the core functions of which is to obtain specific document details through the document ID.Today, let's dive deep into how to use the document ID to easily obtain the details of Anqi CMS documents, making your content operations more efficient and automated.### Core Steps

2025-11-09

How to get the details of a document through the document URL alias (`filename`)?

In AnQi CMS, the document URL alias (`filename`) is a very practical feature, which not only makes your website URL more friendly and improves search engine optimization (SEO) effect, but also provides great convenience in content acquisition.When you need to obtain detailed information about a specific document through programming, AnQiCMS provides a simple and intuitive API interface.### Core Feature Revelation: Get Details Using Document URL Alias To get all details of the document through its URL alias

2025-11-09

If both `id` and `filename` parameters are provided, which one will the AnQi CMS document detail interface prioritize?

In AnQi CMS, getting document details is a very frequent operation in our daily operation.We usually request document data through the unique numeric identifier `id` or the user-friendly URL alias `filename`.

2025-11-09