During the process of building a website, we often encounter the need to perform arithmetic operations on numbers in templates, or to concatenate different string content to form new text.For AnQiCMS users, understanding how to efficiently implement these operations in the template engine is the key to enhancing the flexibility of content display and development efficiency.AnQiCMS Powerful template engine, inspired by Django's design philosophy, provides intuitive and feature-rich tags and filters to meet these needs.
AnQiCMS Template Basics: Flexible Calculation and Data Processing
AnQiCMS Template Engine Uses Double Curly Braces{{ 变量 }}To Output Variables, and Use Single Curly Braces and Percent Signs{% 标签 %}Define the control logic. It has built-in basic arithmetic capabilities, such as in{{ 10 + 5 }}In such an expression, you can perform addition, subtraction, multiplication, and division directly.However, when it comes to more complex mixed data type operations, such as the 'addition' (which is actually concatenation) of numbers and strings, or when more flexible data handling is required, the 'filter' provided by AnQiCMS becomes a powerful tool.
The filter is a very practical feature in AnQiCMS template engine, which allows us to convert and process variables or expressions without writing additional logic on the backend. Each filter has its specific purpose, through|The symbol concatenation follows the variable, forming a chained operation.
core tools:addThe filter implements addition and concatenation.
For addition of numbers and concatenation of strings, AnQiCMS template engine provides a function namedaddThe dedicated filter. This filter can intelligently handle different types of data, whether you want to add two numbers, concatenate two strings, or even perform mixed operations involving numbers and strings.addFilters can handle tasks with ease. It works in a way similar to+operators, with certain type conversion capabilities.
addThe basic usage of the filter is very intuitive:
{{ 对象1|add:对象2 }}
Here,对象1It is the initial value you want to operate with对象2It is the value to be added or concatenated
Let us understand its practical application through several specific examples:
Addition operation of numbersSuppose we have a product price of
50Now we want to calculate the tax in the template10The total price.<p>商品价格:{{ product.price }}</p> {# 假设 product.price 的值为 50 #} <p>含税总价:{{ product.price|add:10 }}</p> {# 输出:含税总价:60 #}Pass
addThe filter, we easily completed the addition of numbers without introducing complex programming logic.The concatenation operation of stringsThe text is dynamically generated on the website, such as adding a unified prefix to the article title.
{% set article_title = "AnQiCMS 模板开发指南" %} <p>完整标题:{{ "【精品】"|add:article_title }}</p> {# 输出:完整标题:【精品】AnQiCMS 模板开发指南 #}Here,
addThe filter joins two strings seamlessly.The combination of numbers and stringsSometimes, you may need to combine numbers and strings