During the process of building a website, we often encounter the need to perform addition 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 is a powerful template engine, drawing inspiration from Django's design philosophy, providing intuitive and feature-rich tags and filters to meet these needs.
AnQiCMS Template Basics: Flexible Operations and Data Processing
AnQiCMS Template Engine Using Double Curly Braces{{ 变量 }}To output variables, use single curly braces and percent signs{% 标签 %}Define control logic. It has built-in basic arithmetic capabilities, such as in{{ 10 + 5 }}In this 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 needed, the 'filter' provided by AnQiCMS becomes a powerful tool.
The filter is a very practical feature of the AnQiCMS template engine, which allows us to transform and process variables or expressions without writing additional logic on the backend. Each filter has its specific purpose, through|The symbol is connected after the variable, forming a chained operation.
Core tool:addThe filter implements addition and concatenation.
For addition of numbers and concatenation of strings, the AnQiCMS template engine provides a namedaddThe dedicated filter. This filter can intelligently handle different types of data, whether you want to add two numbers together, concatenate two strings, or even perform mixed operations involving numbers and strings.addThe filter can complete tasks with ease. Its working method is similar to that of the operator in Go language.+It has certain type conversion capabilities.
addThe basic usage of the filter is very intuitive:
{{ 对象1|add:对象2 }}
here,对象1Is the initial value you want to operate on,对象2Then is the value to be added or concatenated.
Let us understand its practical application through a few specific examples:
Arithmetic addition of numbersSuppose we have the price of a product is
50Now want to calculate in the template by adding taxes10The total price after.<p>商品价格:{{ product.price }}</p> {# 假设 product.price 的值为 50 #} <p>含税总价:{{ product.price|add:10 }}</p> {# 输出:含税总价:60 #}By
addWith the filter, we easily completed the addition of numbers without introducing complex programming logic.String concatenation operationText needs to be dynamically generated on websites, such as adding a unified prefix to article titles.
{% set article_title = "AnQiCMS 模板开发指南" %} <p>完整标题:{{ "【精品】"|add:article_title }}</p> {# 输出:完整标题:【精品】AnQiCMS 模板开发指南 #}here,
addThe filter seamlessly connects two strings together.The number and string are mixed togetherSometimes, you may need to combine numbers and strings