How to use the `add` filter to dynamically add 'Yuan' or a specific currency symbol after the product price?

Calendar 👁️ 60

When managing website content, especially when it involves product display, we often need to present price and other numerical information in a more user-friendly way. Simply displaying a string of numbers, such as199It is often not intuitive, if it could automatically add a currency symbol, such as199元or$199,can greatly enhance the readability and professionalism of information. AnQiCMS (AnQiCMS) with its flexible template system makes it very simple to meet such requirements, and the key to this is the clever use ofaddfilter.

Get to knowaddFilter

The Anqi CMS template system, like many modern content management systems, provides a rich set of built-in filters for processing and formatting output data. Among them,addThe filter is a very practical tool, it can concatenate or add two values.

The strength of this filter lies in its ability to not only handle the addition of numbers but also flexibly concatenate strings with numbers. Its basic syntax is{{ 变量 | add: 要添加的内容 }}For example,{{ 5 | add: 2 }}It will output numbers7And if you add a string, like{{ 5 | add: "CMS" }}it will intelligently convert numbers5to strings and then concatenate"CMS"to5CMS. Even if you mix integers, floats, and strings, addThe filter also tries to perform reasonable conversions and operations. If the automatic conversion fails, it will gracefully ignore parts that cannot be added or concatenated, ensuring that the template does not report an error.

Practical Application: Add the "Yuan" symbol to product prices

Now, let's see how to dynamically add the "Yuan" currency unit to product prices on your Anqi CMS website.

Assuming you are building a product detail page and have already passedarchiveDetailtags to get the product details, which include the product price fieldPriceIn the template, you might display the original price like this:

<p>商品价格:{{ archive.Price }}</p>

Ifarchive.Pricehas a value of199Then the page will display 'Product price: 199'. To make it more intuitive, we can introduceaddFilter, simply add 'Yuan' after the price:

<p>商品价格:{{ archive.Price | add: "元" }}</p>

Whenarchive.Pricehas a value of199When, the page now displays as "Product price: 199 yuan". Isn't it clearer all of a sudden? This line of code is usually placed in your product detail template file (for exampleproduct/detail.htmlIn it, or on the product list page'sarchiveListLoop, processing each productitem.Price.

Add more currency symbols or prefixes

addThe flexibility of the filter goes beyond this. If you are targeting international markets and need to display the dollar or euro symbol, the operation is just as simple.You just need to replace the 'Yuan' with the corresponding symbol or abbreviation:

{# 显示美元价格,例如:$99.00 #}
<p>价格:{{ "$" | add: archive.Price }}</p>

{# 显示欧元价格,例如:99.00 € #}
<p>价格:{{ archive.Price | add: " €" }}</p>

{# 显示人民币符号,例如:¥199.00 #}
<p>价格:{{ "¥" | add: archive.Price }}</p>

{# 添加更具体的货币单位,例如:199.00 RMB #}
<p>价格:{{ archive.Price | add: " RMB" }}</p>

You can even combine multipleaddFilter, or add a fixed text before the variable to build a more complex display format. For example, if you want to follow the text "Price:" directly with the currency symbol and price, you can write it like this:

<p>{{ "价格:" | add: "¥" | add: archive.Price }}</p>

This will output an effect similar to "Price: ¥199."

Advanced usage and注意事项

Handling null or zero values:In actual operation, the price of some products may be null or zero. We do not want to display it directly0元Or it might just be a lone currency symbol. At this point, combined with the Anqi CMS templateifLogical judgment can make your price display more intelligent:

<p>
{% if archive.Price > 0 %}
    商品价格:{{ archive.Price | add: "元" }}
{% else %}
    商品价格:面议
{% endif %}
</p>

This code will checkarchive.PriceIs it greater than zero? If it is greater than zero, display the price plus ' Yuan'; otherwise, display 'Discussable'.

Maintain consistency:To maintain the consistency and professionalism of the website's overall style, it is recommended to use a unified filtering method for displaying prices throughout the site.Whether in the product list, detail page, or other modules, the same currency symbol and format should be used.

Multi-language and global currency settings: If your website needs to support multiple currencies and you want the currency symbol to be dynamically switched based on user selection or system settings, then you may need to combine the multi-language function of Anqi CMS or customize a global variable to achieve this, rather than simply hardcoding it inaddIn the filter. For example, you can define a global currency symbol variable{{ system.CurrencySymbol }}, then combineaddThe filter is used:{{ system.CurrencySymbol | add: archive.Price }}This requires you to add the corresponding custom parameters in the background "Global Function Settings".

ByaddFilter, you can easily beautify the display of product prices, making your website look more professional and user-friendly.This small change can significantly improve user experience, bringing your content operation to a new level.


Frequently Asked Questions (FAQ)

1.addDoes the filter only apply to prices?No, it's not.addThe filter is very versatile and can be used for concatenating or adding any numbers or strings. For example, you can add the reading count of the article.{{ archive.Views | add: " 次阅读" }}Or concatenate the product name and model{{ product.Name | add: " - " | add: product.Model }}.

2. How do I add the currency symbol in front of the price instead of at the end?This is also very simple. You just need to adjustaddThe order of the filters is. For example, if you want to add the symbol '¥' before the price, you can write it like this: `

Related articles

What is the practical difference in applying the `add` filter and the `stringformat` filter in template string concatenation?

In the AnQi CMS template world, flexibly handling strings is an indispensable skill for building dynamic web pages.Among them, the `add` filter and the `stringformat` filter can help us concatenate or combine strings, but their design philosophy and application scenarios are obviously different.Understanding these differences allows us to make more informed choices when developing templates, resulting in more efficient and readable code.### `add` filter: A concise and intuitive concatenation tool `add`

2025-11-07

How to dynamically add 'Read More' link text to each title on the AnQiCMS article list page?

When managing and operating a website, the article list page is an important entry for users to browse content and discover information.To enhance user experience, clearly guiding visitors to enter the article details, it is a common practice to dynamically add a "Read More" or "View Details" link text to each title in the list.AnQiCMS (AnQiCMS) provides a powerful and flexible template system, allowing us to easily achieve this feature.

2025-11-07

How do you handle the case when one of the variables is `nothing` or `nil` while using the `add` filter, and what will be the output result?

In AnQiCMS template development, we often need to perform simple addition operations or string concatenation, the `add` filter is created for this purpose.It is very convenient to use, and can intelligently handle addition of numbers and concatenation of strings.If you give it two numbers, it will perform mathematical addition;If you give it two strings, it will concatenate them.Even if it is a mixture of numbers and strings, AnQiCMS will try to make a reasonable conversion and output the result.For example, add numbers: twig {{ 5|add:2 }} {#

2025-11-07

In AnQiCMS template, why does `{{ 5|add:"CMS" }}` output `5CMS`? What are the rules for mixing numbers and strings in concatenation?

In AnQiCMS template development, we sometimes encounter some seemingly simple expressions that hide clever logic.Among the phenomenon that `{{ 5|add:"CMS" }}` finally outputs `5CMS`, it often makes friends who are new to it feel curious.This involves the unique rule of mixed number and string splicing in AnQiCMS template engine.Today, let's delve deeper into this interesting mechanism.### The "filter" in AnQiCMS template and the magic of `add` In AnQiCMS

2025-11-07

How to use the `add` filter to build dynamic SEO-friendly URL paths in the Anqi CMS template?

In Anqi CMS template creation, flexible and SEO-friendly URL paths are the key to improving the website's search engine performance.Although AnQi CMS provides powerful pseudo-static rule configuration capabilities in the background, generating static URLs through patterns such as `/module-id.html`, but in certain specific scenarios, we may need to control or combine the local URL path more dynamically and finely within the template to meet unique business needs or marketing strategies.At this point, the `add` filter has become a very practical auxiliary tool.###

2025-11-07

How to use the `add` filter in a `for` loop to dynamically generate a unique `id` or `class` attribute for list items?

In AnQi CMS template development, we often need to handle the display of dynamic lists.Whether it is an article list, product display, or other content block, dynamically generating a unique `id` or `class` attribute for each item in the list is particularly important in order to achieve finer style control, JavaScript interaction, or to ensure the uniqueness of page elements.Today, let's talk about how to cleverly use the `add` filter in the `for` loop of AnQiCMS to achieve this goal.### Understanding the demand for dynamic attribute generation Imagine that

2025-11-07

Can the `add` filter be used to concatenate the website name and footer copyright information obtained from the `system` tag?

In website operation, we often need to finely control the display of page content, especially those global information that needs to be repeatedly displayed throughout the website, such as the website name, footer copyright, and so on.AnQiCMS (AnQiCMS) provides powerful template tags and filters to help us flexibly handle these requirements.Today, let's talk about a very practical scenario: Can the `add` filter of AnQi CMS be used to concatenate the website name and footer copyright information obtained from the `system` tag?The answer is affirmative, and this method is both intuitive and efficient.

2025-11-07

How to concatenate the area code and phone number in the contact (`contact` tag) using the `add` filter?

It is crucial to clearly and effectively display contact information when operating your website.AnQiCMS (AnQiCMS) with its flexible template engine and powerful tag system, allows you to easily achieve various complex display needs.Today, let's talk about a common but somewhat tricky requirement: how to concatenate the area code and phone number in the contact information using the `add` filter to make your phone number clear at a glance.Why do you need to concatenate the area code and phone number?

2025-11-07