How to display different text or icons in the AnQiCMS template based on the size of a number (such as order amount)?

Calendar 👁️ 63

In website content management, we often need to dynamically display different information based on the specific numerical value of the data, which can not only improve user experience but also make the content more expressive.For example, display different discount tips, VIP level icons, or "In stock" or "Out of stock" based on inventory quantity, according to the amount of the order.AnQiCMS provides flexible and powerful template functions, allowing easy implementation of conditional judgments and content display based on numerical values.

The template engine syntax of AnQiCMS is very similar to the popular Django template engine, it allows us to control the output of the page through concise tags and variables. The core idea is to use{% if 条件 %}Such logic judgment tags, combined with the comparison of numbers, to determine which content to display.

Preparation: Set the numeric field in the content model.

To judge based on the value size, first ensure that your content model (such as articles, products, or other custom models) contains a field that stores values.AnQiCMS allows users to customize content models and add various fields, which provides us with great flexibility.

Assuming we need to display different information based on the order amount of the product, we can add a custom field to the corresponding model (such as the 'Product Model') in the 'Content Model' management.For example, create a field named "Order Amount" and select its field type as "Number".Such, when publishing or editing a product, we can enter specific values for this field.

In the template, we can go througharchiveDetailtags orarchiveParamsTag to get the value of this custom field. If your value field is directly defined in the content model, you can use it directly{{ archive.你的字段名 }}Get it. For example, if your custom field is set in the "Call Field" in the backgroundorderAmount, then you can access it in the template like this:{{ archive.orderAmount }}.

To implement conditional judgment: display different text

Once we can get the numeric field, we can useifLogic judgment tags to display different text according to the size of the number.

For example, we want to display 'Large Order', 'Normal Order', or 'Small Order' based on the order amount:

{% set amount = archive.orderAmount|integer %} {# 获取订单金额,并确保转换为整数类型 #}

{% if amount > 1000 %}
    <span class="order-status large">大额订单</span>
{% elif amount > 500 %}
    <span class="order-status normal">普通订单</span>
{% else %}
    <span class="order-status small">小额订单</span>
{% endif %}

In the code above, we first use{% set %}Labels willarchive.orderAmountAssign a value to a variable namedamountThe variable.It should be noted that the values obtained from custom fields may sometimes be treated as string types in templates.|integerThe filter explicitly converts it to an integer. If your value may contain decimals, you can use|floatfilter.

Next, we use{% if ... %}/{% elif ... %}and{% else %}structure based onamountDifferent intervals to output different text content. This structure is very intuitive and can clearly express conditional logic.

Advanced application: Display different icons

In addition to displaying text, we can also display different icons based on the size of the number. This is usually achieved by inserting<img>tags or adding different CSS classes to elements.

For example, we can display different VIP level icons based on the order amount:

{% set amount = archive.orderAmount|integer %} {# 获取订单金额,并转换为整数类型 #}

<div class="vip-level-display">
    {% if amount >= 5000 %}
        <img src="/public/static/images/vip-diamond.png" alt="钻石VIP" title="订单金额达到5000元以上">
        <span class="vip-text">钻石VIP客户</span>
    {% elif amount >= 2000 %}
        <img src="/public/static/images/vip-gold.png" alt="黄金VIP" title="订单金额达到2000元以上">
        <span class="vip-text">黄金VIP客户</span>
    {% elif amount >= 500 %}
        <img src="/public/static/images/vip-silver.png" alt="白银VIP" title="订单金额达到500元以上">
        <span class="vip-text">白银VIP客户</span>
    {% else %}
        <img src="/public/static/images/vip-bronze.png" alt="普通会员" title="订单金额不足500元">
        <span class="vip-text">普通会员</span>
    {% endif %}
</div>

In this example, based on different amount ranges, we not only displayed different icons through<img>Label introduction, make sure that these icon files have been uploaded topublic/static/images/The directory, with corresponding text descriptions, can greatly enrich the visual effects and information delivery efficiency of the page.

Practical skills and precautions

  • Type conversion is the key:As mentioned before, be sure to use|integeror|floatThe filter converts the variable to the correct numeric type. This is a critical step to avoid conditional judgment errors.
  • Handle null or default values:If your numeric field may be empty, and you want to display a default value or avoid errors when it is empty, you can use|defaulta filter. For example:{% set amount = archive.orderAmount|integer|default:0 %}This will ensure that whenorderAmountis empty,amountThe value is0.
  • Make good use of{% set %}Tags:Use{% set yourVariable = yourValue %}You can temporarily create variables to make the template logic clearer and more readable, avoiding repeated retrieval or processing of data.
  • Static resource management:If you want to display an icon, make sure the icon file has been uploaded to the static resource directory of the website (usuallypublic/static/in a subdirectory), and refer to the correct path in the template.
  • Code simplicity and maintainability:Even though the template function is powerful, it is still recommended to keep the logic concise.If the condition judgment becomes too complex, consider whether it can be simplified by adjusting the data structure or processing partially on the backend.

By using this method, AnQiCMS users can easily display different text or icons in templates based on the numerical size, thereby adding more interactivity and dynamic effects to the website content.


Frequently Asked Questions (FAQ)

  1. If my value field content is text type (such as "one thousand yuan"), rather than pure numbers, can the template directly compare?Answer: AnQiCMS's template engine cannot directly recognize non-numeric text (such as "one thousand yuan") as comparable values.Ensure that the type of the custom field is "number" and the content entered is purely numeric.If indeed it is necessary to handle such text, it may be necessary to preprocess the text into numbers before it is stored in the database, or to use a custom filter function for more complex text parsing at the template level (but this usually requires secondary development).

  2. I want to dynamically modify the CSS style of elements (such as background color, font size) based on the amount range, rather than simple text or icons, can AnQiCMS do that?Of course you can. You can output different CSS classes through conditional judgment, or output inline styles directly. For example:

    {% set amount = archive.orderAmount|integer|default:0 %}
    <span class="order-price {% if amount > 1000 %}highlight-red{% elif amount > 500 %}highlight-orange{% else %}highlight-green{% endif %}">
        {{ archive.orderAmount }} 元
    </span>
    

    Or output inline styles directly:

    {% set amount = archive.orderAmount|integer|default:0 %}
    <span style="color: {% if amount > 1000 %}red{% elif amount > 500 %}orange{% else %}green{% endif %}; font-weight: bold;">
        {{ archive.orderAmount }} 元
    </span>
    
  3. If the numeric field may be empty (i.e., the user has not filled in), how can you avoid template errors or poor display results?Answer: You can combine using|defaultFilter. For example,{% set amount = archive.orderAmount|integer|default:0 %}. This will ensure that whenarchive.orderAmountIf empty or cannot be converted to a number,amountThe value of the variable

Related articles

How to sum up sales data that may be a string read from the database in the template?

In website operation, we often need to handle various data, among which the sales data of products or articles is particularly common.To better analyze and display, we may need to perform cumulative calculations on the sales data in the front-end template of the website.However, the data type read from the database is not always purely numerical, and it may sometimes exist in the form of a string, which presents a challenge for direct mathematical operations.The Anqi CMS, with its flexible template engine and powerful data processing capabilities, provides us with an elegant solution.

2025-11-08

How can I convert and validate the age input (string) in the template?

In website operation, we often need to process user input data, especially some key numerical information, such as the age of the user.Although the final processing and strict verification of data are usually performed on the backend, preliminary conversion and verification on the frontend template level can effectively improve user experience and ensure the accuracy and rationality of page display.AnQiCMS (AnQiCMS) with its flexible template engine provides powerful tools to help us achieve this goal.

2025-11-08

Does the `integer` filter round up or down during conversion?

In AnQi CMS template development, data transformation is a common operation, especially when dealing with numbers.We often encounter situations where we need to convert a number that may have decimals into an integer, at which point the `integer` filter comes into play.However, a common question is: when using the `integer` filter for conversion, does it perform ceiling or truncation (floor)?Understanding the working principle of the `integer` filter is crucial for ensuring the accurate display of website data.in short

2025-11-08

How can I avoid scientific notation when displaying floating-point numbers directly in a template?

In the AnQiCMS template, when we need to display floating-point numbers, we sometimes encounter them appearing in scientific notation, such as `1.23E+07` instead of `12,300,000`.This display method is useful in some specific scenarios, but it is often difficult for ordinary users to understand intuitively, affecting the aesthetics and readability of the page, especially in situations where the accuracy is not high but the intuitiveness is required, such as displaying product prices, statistics, etc.AnQiCMS's template engine design borrowed from Django syntax

2025-11-08

Does the `floatformat` filter support negative number of decimal places, to discard decimal places from right to left?

In AnQi CMS, the details of content display often determine the quality of user experience.How to present numbers commonly seen on websites, especially floating-point numbers, in a clear, accurate, and expected format is a concern for content operators.The Anqi CMS template engine provides a variety of powerful filters to assist us in processing data, among which the `floatformat` filter is a powerful tool for displaying floating-point numbers.

2025-11-08

I have a numeric string that needs to be converted to an integer and used as the `limit` parameter, can I do that?

In Anqi CMS template development, we often need to handle various types of data.Sometimes, data obtained from external interfaces or user input exists in string form, but we need to use it as a number, for example, when displaying the number of items in the control list with the `limit` parameter.Then, can a numeric string be converted to an integer in the template and used as a `limit` parameter?The answer is affirmative, and Anqicms provides a simple and powerful way to achieve this.

2025-11-08

How to display formatted numbers without changing the original data type?

In the presentation of website content, the way numbers are displayed often directly affects users' perception and understanding of information.At times, we want to format numbers, such as displaying prices with two decimal places, adding a percentage sign when showing percentages, or formatting large numbers for easier readability.AnQi CMS provides us with a powerful and flexible template engine, allowing us to easily achieve these refined numerical display requirements without modifying the original data type.The template engine of Anqi CMS adopts a syntax style similar to Django or Blade, it uses double curly braces

2025-11-08

How does the `get_digit` filter perform on Chinese string input?

During the template development process of AnQiCMS, we often use various filters to process and format data.The `get_digit` filter is one of them, its main function is to extract the specified digit from a number.However, when we pass Chinese string as input to the `get_digit` filter, its behavior may be different from what we intuitively expect.### `get_digit` filter's basic function First, let's review the `get_digit`

2025-11-08