In the operation of the AnQiCMS website, maintaining the standardization of product units and currency symbols is a key factor in improving user experience and professionalism.We often need to unify the formats of data from different sources and different input habits.Then, AnQiCMS providesreplaceCan the filter effectively be used to achieve this goal? The answer is yes, by using it cleverlyreplaceThe filter allows us to standardize the displayed content at the template level without modifying the original data in the database.
UnderstandingreplaceThe principle of the filter.
AnQiCMS uses a syntax similar to the Django template engine, which includes rich filters (filters) to handle data display on the front end.replaceThe filter is one of the very practical tools that allows us to replace a specific "old" word with a "new" word in a string and return the new string. Its basic usage is very intuitive:
{{ obj|replace:"旧词,新词" }}
Hereobjis the variable to be processed, such as product price or unit field, and旧词and新词are separated by commas,to separate. It should be noted thatreplacethe filter is for precise string matching and replacement.
Actual application scenario one: Standardized product units
Assuming our product content model has a "weight" field, but when entered, there may be various unit representation methods, such as "500g", "1 kilogram", "1000 grams". In order to display them uniformly as "grams" or "g" on the website frontend,replaceThe filter can be used.
For example, we want to display all "g" as "gram":
{{ product.Weight|replace:"g, 克" }}
If the original data contains "kilogram", we can also perform multiple replacements, or first unify it to an intermediate format:
{# 假设原始数据是 "1公斤" 或 "500g" #}
{{ product.Weight|replace:"公斤,000克"|replace:"g,克" }}
{# "1公斤" -> "1000克";"500g" -> "500克" #}
In this way, we can convert various irregular unit representations into the unified format we expect when rendering to the page, greatly improving the standardization of the content.
The practical application scenario two: Uniform currency symbol display
When displaying product prices, the standardization of currency symbols is also important.For example, the backend may store "USD 100", "¥50" or "20 euros", and we want to display them uniformly as "$100", "¥50" and "€20"。
UtilizereplaceFilter, we can operate like this:
{# 将 "USD" 替换为 "$" #}
{{ product.Price|replace:"USD, $" }}
{# 将 "¥" 替换为 "¥"(如果输入的是全角或不标准符号) #}
{{ product.Price|replace:"¥, ¥" }}
{# 处理“欧元”的替换 #}
{{ product.Price|replace:"欧元, €" }}
It should be noted that if the price field is a pure number and we want to add a currency symbol in front of it,replaceThe filter itself is not suitable for direct addition. It is better at replacing existing strings. In such cases, we may need to first format the number as a string (for example, usingstringformatorfloatformatFilter, then perform the replacement operation:
{# 假设 product.RawPrice 是一个数字,我们希望显示为 "$100.00" #}
{{ product.RawPrice|floatformat:2|add:"^"|replace:"^, $" }}
{# 这里巧妙地利用 add 添加一个特殊字符,再替换为 $。不过更推荐直接字符串拼接。 #}
{# 更直接的拼接方式(非replace功能): "$"|add:product.RawPrice|floatformat:2 #}
{# 但如果原始价格是 "100 USD",要换成 "$100": #}
{{ product.PriceWithCurrency|replace:"USD, $" }}
Consider multi-language support
AnQiCMS provides powerful multilingual support