In AnQi CMS template design, flexibly handling and displaying data is the key to building a dynamic website.This is a common need in daily development to concatenate numbers or strings.AnQi CMS provides various ways to achieve this goal, from simple direct combinations to powerful filters, which can meet your different content assembly scenarios.
Understanding the basics of concatenation: Direct output and 'plus'
In AnQi CMS template syntax, the most direct way to concatenate strings is to place multiple variables or text side by side. For example, if you want to combine the article title and site name in the webpage title, you can directly write it as<h1>{{ archive.Title }} - {{ system.SiteName }}</h1>. The template engine will convert their respective values to strings and combine them in order.
However, when you need to perform more complex "concatenation" operations, especially involving numerical calculations or intelligent handling of different data types, the Anqi CMS providesaddthe filter becomes very convenient.addThe filter can not only concatenate strings but also perform numeric addition when possible.
addThe flexible application of the filter.
addThe filter is a powerful tool for handling number and string concatenation. Its basic syntax is{{ obj|add:obj2 }}of whichobjis the first value you will operate on,obj2is the second value you will add.
Adding numbers:When
objandobj2when both are numbers,addthe filter will perform mathematical addition.{{ 5|add:2 }} {# 结果:7 #} {{ 10|add:40 }} {# 结果:50 #}This is very useful when it is necessary to dynamically calculate and display the total and statistical information.
String concatenation:When
objorobj2When both (or either) are strings,addThe filter will connect them together. For example:{{ "安企"|add:"CMS" }} {# 结果:安企CMS #}Mixing numbers and strings:
addThe filter can intelligently handle the mixed concatenation of numbers and strings.If one is a number and the other is a string, it usually converts the number to a string before concatenating. For example:{{ 5|add:"CMS" }} {# 结果:5CMS #} {{ "安企"|add:2 }} {# 结果:安企2 #}This flexibility means you don't have to manually perform type conversion, thus simplifying template code.
Handling null or non-numeric valuesIf:
obj2Isnothing(empty value) or cannot be converted to a number/string,addThe filter may return only based on the specific situation,objor ignore the parts that cannot be added. For example:{{ 5|add:nothing }} {# 结果:5 #}
ByaddFilter, you can easily implement dynamic text generation, such as building descriptions with product quantity or status information.
Combination of array content: joinFilter
In some cases, you may need to combine all elements of an array or list into a single string, separated by a specific delimiter. At this point,joinThe filter is **selection.
joinThe syntax of the filter is{{ array|join:"分隔符" }}.
For example, if you have an array storing article tagstags, you can display it as a comma-separated string:
{% tagList tags with itemId=archive.Id limit="10" %}
<p>文章标签:{{ tags|join:", " }}</p>
{% endtagList %}
This will convert the arraytagsConnect the titles (or their default string representation) of each element with commas and spaces to form a complete string.
Fine-grained output control:stringformatFilter
For more advanced formatting and combination scenarios, such as inserting variables into strings and aligning numbers, controlling floating-point precision, etc.,stringformatThe filter provides functionality similar to Go languagefmt.Sprintf()function.
The syntax is{{ obj|stringformat:"格式定义" }}.
For example, you want to format a number into a string with a prefix:
{{ archive.Id|stringformat:"文章ID-%d" }} {# 结果:文章ID-123 #}
Or combine it with other strings for formatting:
{{ system.SiteName|stringformat:"欢迎来到 %s 的网站!" }} {# 结果:欢迎来到 安企CMS 的网站! #}
stringformatThe filter provides a rich set of formatting options (such as%sUsed for strings,%dUsed for integers,%.2fUsed for floating-point numbers and retaining two decimal places, etc.), allowing you to accurately control the structure and content of the output string.
Attention points when concatenating
- HTML content concatenation and safety: If the concatenated string may contain HTML code (for example, from user input rich text content), be sure to use
safea filter such as{{ content|safe }}Otherwise, the template engine will default to HTML escaping, displaying HTML tags as plain text to prevent XSS attacks. - Code readability: You can concatenate multiple variables and strings directly, but when the concatenation logic becomes complex, using
addOr filter.stringformatcan improve the readability and maintainability of the code. - DebugIf the concatenation result is not as expected, you can use
dumpFilter (such as{{ myVariable|dump }}) to view the actual type and value of the variable, which is very helpful for debugging.
By flexibly using these concatenation techniques, you can make the content display of Anqi CMS template more vivid and accurate, better meeting the operation needs of the website.
Frequently Asked Questions (FAQ)
Can I use
addDoes the filter perform pure numerical addition operations? AnswerYes, whenaddBoth operands of the filter are numbers (integers or floating-point numbers), it will perform mathematical addition. For example{{ 10|add:5 }}You will get15If one of the operands is not a number, it will try to convert both of them to strings and then concatenate them.How to concatenate strings while adding a fixed separator between each part, such as a space or a hyphen? AnswerFor concatenating a few strings, you can directly insert the separator as a literal, for example
{{ var1 }} - {{ var2 }}If you need to concatenate multiple elements of an array, separated by a separator,joinThe filter is a better choice, for example{{ tags|join:", " }}.Question: If I need to concatenate a string that contains many variables and fixed text, and need to control the format accurately, which method should I use? AnswerIn this case,
stringformatThe filter will be your best choice. It provides powerful formatting capabilities, allowing you to use Go language as if you were usingfmt.Sprintf()Such an output template is defined, precise control of the insertion position and format of each variable, for example{{ "%s 的产品数量是 %d 个"|stringformat:productName, productCount }}.