How to implement dynamic adjustment of the output format of calculation results in a template, for example, displaying different currency formats based on countries or regions?

Calendar 👁️ 62

In website operation, especially when facing global users, the localization experience of content is crucial.Among them, dynamically adjusting the output format of the calculated results, such as displaying different currency formats based on the user's country or region, is a common requirement.AnQiCMS (AnQiCMS) is a flexible content management system that provides a powerful template engine and rich tags / filters, allowing us to beautifully implement this function.

To achieve this goal, we mainly rely on several core capabilities of the Anqi CMS template engine:Get the current site or user's language/region information/Conditional judgmentandNumber formatting.

Get the current site or user's language/region information

The AnQi CMS excels in multilingual support, which provides the foundation for us to dynamically adjust content.The system will automatically identify the language settings of the current visited site or the language selected by the user through the language switcher.In the template, we can go throughsystemLabel to get the current site's language code.

For example, to get the current site's language settings, you can do it like this:

{% set current_lang = "" %}
{% system current_lang with name="Language" %}
{# 此时,current_lang 变量将包含当前站点的语言代码,例如 "en-US", "zh-CN", "ja-JP" 等 #}

Here, we first define an empty variablecurrent_langThen usesystemLabel it as the language code of the current site. This language code will be the key basis for our subsequent judgment.

The core idea of dynamically adjusting the output format of the calculation results.

With the current language/region information, the next step is to apply this information to the output of the calculation result (such as product prices). The core idea is:

  1. Get the original value:For example, get the product price from the document details.
  2. Determine the current language/region:UseifLogic judgment label, according tocurrent_langto branch based on the value.
  3. Apply formatting:For different languages/regions, combine currency symbols and number formatting filters to output a currency format that conforms to local customs.

Practice: Formatting product prices in currency format

Assuming we have a product detail page where we need to display the product price. The product price is stored inarchivethe object'sPricein the field.

1. Basic price display

The simplest way to display the price is to output it directly, but usually without the currency symbol and proper format:

产品价格:{{ archive.Price }}

2. Format dynamically according to language/region

Now, we combine the language information we have obtained to add dynamic currency formatting to product prices. We will usefloatformata filter to control the number of decimal places and manually add the currency symbol.

{% set current_lang = "" %}
{% system current_lang with name="Language" %}

<div>
    产品价格:
    {% if current_lang == "en-US" %}
        $ {{ archive.Price|floatformat:2 }} {# 美国,显示美元符号,保留两位小数 #}
    {% elif current_lang == "fr-FR" or current_lang == "de-DE" %}
        {{ archive.Price|floatformat:2 }} € {# 欧洲,显示欧元符号,保留两位小数,符号在数字后 #}
    {% elif current_lang == "ja-JP" %}
        ¥ {{ archive.Price|floatformat:0 }} {# 日本,显示日元符号,不保留小数 #}
    {% elif current_lang == "zh-CN" %}
        ¥ {{ archive.Price|floatformat:2 }} {# 中国大陆,显示人民币符号,保留两位小数 #}
    {% else %}
        $ {{ archive.Price|floatformat:2 }} {# 默认格式,例如国际通用美元格式 #}
    {% endif %}
</div>

In the above code:

  • We first obtained the language code of the current site and stored it incurrent_langthe variable.
  • Next, useif...elif...elsestructure based oncurrent_langand judged its value.
  • For different languages, we adjust the position, type, and display of currency symbols. For example, Japan usually does not display decimal places, while the Western world tends to keep two decimal places.
  • floatformat:2Represents formatting a floating-point number to two decimal places.floatformat:0Means not to retain decimals.
  • We have also set aelsebranch as the default display format for all languages that are not explicitly specified.

3. Wrap the formatting logic into a helper macro (Macro)

If a website needs to use currency formatting in many places, we can encapsulate the above logic into a macro (macro) to improve code reusability and maintainability.

First, define a macro in a separate template file (for examplepartial/currency_macros.htmlThen, import and call this macro where needed:

{# partial/currency_macros.html #}
{% macro format_price(price_value, lang_code) %}
    {% if lang_code == "en-US" %}
        $ {{ price_value|floatformat:2 }}
    {% elif lang_code == "fr-FR" or lang_code == "de-DE" %}
        {{ price_value|floatformat:2 }} €
    {% elif lang_code == "ja-JP" %}
        ¥ {{ price_value|floatformat:0 }}
    {% elif lang_code == "zh-CN" %}
        ¥ {{ price_value|floatformat:2 }}
    {% else %}
        $ {{ price_value|floatformat:2 }} {# 默认格式 #}
    {% endif %}
{% endmacro %}

Then, in the place where needed, import and call this macro:

{% import "partial/currency_macros.html" as currency_helpers %}

{% set current_lang = "" %}
{% system current_lang with name="Language" %}

<div>
    产品价格:{{ currency_helpers.format_price(archive.Price, current_lang) }}
</div>

This, when we need to display the price elsewhere, just callcurrency_helpers.format_pricemacro, the code will

Related articles

How `get_digit` filter extracts the year part from a longer number?

In daily website content operation, we often encounter the need to extract specific information from data, such as extracting the year from a long number.AnQi CMS provides rich template filters and tags to help us flexibly handle these data.Today, let's talk about the `get_digit` filter and discuss its application in the specific scenario of extracting the year, as well as other methods that are more suitable for extracting the year.First, let's understand what the `get_digit` filter is used for.This filter is designed very cleverly

2025-11-08

How to convert a user's input currency string (such as "123.45") to a numeric type in a template?

When building websites on AnQiCMS, we often encounter scenarios where we need to handle data type conversions, especially when data from the backend is stored as strings but needs to be performed numerical calculations or comparisons on the frontend.For example, product prices, quantities, and other information, although they may be entered in the background as strings such as '123.45', in the template, we may need to treat them as numbers for arithmetic operations such as addition, subtraction, multiplication, or make conditional judgments based on the magnitude of the values.Luckyly, AnQiCMS's powerful template engine provides simple and efficient filters (filters)

2025-11-08

How to ensure consistent display format when the `floatformat` filter handles 0 values?

In Anqi CMS template development, the `floatformat` filter is undoubtedly an important tool for handling floating-point number display, which can help us standardize the display of numbers on the page.However, when encountering zero values or decimal points followed by all zeros, the default behavior may lead to inconsistent display formats, which is particularly prominent in scenarios requiring strict uniformity (such as financial data, product prices).### The basic usage of `floatformat` filter First, let's review the basic function of the `floatformat` filter

2025-11-08

How do I calculate the sum of two product prices in a template and ensure the result is an accurate floating-point number?

In the operation of e-commerce websites, the calculation and display of product prices are often one of the core links.AnQi CMS as an efficient content management system, although it mainly focuses on content management and presentation, its powerful template engine also supports us to perform some basic arithmetic operations, such as calculating the total price of two or more products, and ensuring that the final result is presented in an accurate floating-point form.This article will discuss in detail how to implement this requirement in Anqi CMS templates, covering how to obtain product prices, perform addition operations, and how to control floating-point precision.###

2025-11-08

How does the `stringformat` filter convert a floating-point number to a string with thousand separators?

In Anqi CMS template development, the `stringformat` filter is a very practical tool that allows us to flexibly format various data types, especially numbers and strings, to meet the needs of page display.When dealing with floating-point numbers, we often encounter the need to convert them to strings in a specific format, such as retaining decimal places.However, if large numbers are formatted with thousands separators (e.g., 1,234,567.89), it can significantly enhance the readability of the numbers and make the data presentation more professional.So, in AnQi CMS

2025-11-08

How to convert a numeric field in a template to a string without affecting other logic?

In AnQiCMS template development, we often encounter scenarios where it is necessary to display numeric data combined with text content.For example, you may need to add the product ID at the end of a product title, or add “times read” after the number of reads in an article.How to flexibly convert numeric fields in templates to strings and concatenate them without affecting other logical judgments or calculations?It's lucky that

2025-11-08

What happens when one of the operands is `nil` or empty while using the `add` filter?

In AnQiCMS template development, the `add` filter is a very practical tool that allows us to perform addition operations on numbers or strings, providing convenience for template logic processing.However, when an empty value (empty value) or `nil` appears in the operands of addition, the result may puzzle some users who are new to the subject.Today, let's delve into the behavior of the `add` filter in this specific case.Firstly, the `add` filter is designed to be very flexible, it can not only handle the addition of pure numbers, but also intelligently concatenate strings

2025-11-08

How to ensure that a number converted to `float` or `integer` is correctly identified as `true`/`false` in an `if` condition?

When using AnQiCMS for website content management, we often encounter scenarios where we need to make judgments and process data.Especially when data comes from user input or external interfaces and the type is uncertain, how to correctly identify the number converted through `float` or `integer` filters in the `if` condition judgment in the template is the key to ensuring logical accuracy. The AnQiCMS built-in template engine is powerful, supporting `float` and `integer` type conversions in GoLang.Understand the behavior of these transformation filters in different situations

2025-11-08