In the daily operation of Anqi CMS, we often need to concatenate different field content to form a complete information, such as generating a complete address, or combining an attractive product title.However, a common challenge of dynamic content is that some variables may be empty.If the content is directly concatenated, it not only affects the aesthetics but may also lead to missing information or disordered format.

Fortunately, the powerful Django template engine syntax of AnQiCMS provides us with a flexible solution. By combining cleverlyaddfilters anddefaultFilter, we can set a 'stand-in' for those variables that may be empty before content concatenation, ensuring that the output is always complete and friendly.

Next, let's see how these two filters collaborate to help us build more robust and intelligent template content.

addFilter: The master of content concatenation

Firstly, let's get to know.addFilter. As the name suggests, its most intuitive function is to perform addition operations. When applied to numbers, it will add two values together, just like the plus sign in mathematics.

For example, if you have two numeric variablesnum1andnum2:

{% set num1 = 5 %}
{% set num2 = 2 %}
{{ num1|add:num2 }}  {# 结果是 7 #}

ButaddThe power of the filter goes beyond this. It can also be used to concatenate strings! When you try to add a string with another value (whether it is a string or a number),addThe filter will be very "intelligent" in connecting them.It is not picky, if it encounters types that cannot be added directly (such as adding a number to a non-numeric string), it will try to perform type conversion.If the conversion fails, it will gracefully ignore the portion that cannot be converted, ensuring that the template does not break.

For example, do you want to concatenate “Anqi” and “CMS”:

{% set brand = "安企" %}
{% set product = "CMS" %}
{{ brand|add:product }} {# 结果是 安企CMS #}

Even:

{% set version = 2 %}
{{ "AnQiCMS v"|add:version }} {# 结果是 AnQiCMS v2 #}

defaultFilter: guardian of missing values

UnderstoodaddAfter the concatenation ability of the filter, we still need a partner to deal with the case where the variable is empty, and that isdefaultFilter.In template development, when we obtain data from the background database or API, some fields may not have values for various reasons (such as users not filling in, missing data, etc.).nullsuch as.

defaultThe function of the filter is like a "backup plan": if the value of the variable is empty (includingnilAn empty string""and zero value, etc.), it will use the default value you specify instead.

For example, if you have auserNameA variable, but it may be empty:

{% set userName = "" %} {# 假设 userName 变量为空 #}
{{ userName|default:"匿名用户" }} {# 结果是 匿名用户 #}

{% set userName = "张三" %} {# 假设 userName 有值 #}
{{ userName|default:"匿名用户" }} {# 结果是 张三 #}

Strong combination: solving the splicing pain point

Now, by combining these two filters, we can elegantly solve the problem of 'setting a default value for possibly empty variables before concatenation.'

Imagine that you are creating a contact information display for a product detail page, and you need to combine the phone number and email.If a field is empty, we do not want it to display blank directly, but to give a prompt.

The potential issues that may arise from direct concatenation:Assumearchive.PhoneThe content inside the loop will not be rendered, instead, the content of the block will be displayed.archive.Email有值:

{{ "电话:"|add:archive.Phone|add:",邮箱:"|add:archive.Email }}
{# 结果可能是:电话:,邮箱:[email protected] #}

“电话:”后面直接跟着逗号,显得非常不专业。

Combinedefault过滤器解决:正确的做法是,在每个可能为空的变量被add过滤器处理之前,先用defaultThe filter sets a default value for it. This way, even if the original variable is empty,addThe filter always receives a valid string (i.e., the default value), ensuring the integrity of concatenation.

{% set archive = {Phone: "", Email: "[email protected]"} %} {# 模拟数据,电话为空 #}

{# 优雅拼接:为每个可能为空的变量设置默认值 #}
{{ "电话:"|add:archive.Phone|default:"未提供"|add:",邮箱:"|add:archive.Email|default:"未提供" }}
{# 结果是:电话:未提供,邮箱:[email protected] #}

The working principle here is:

  1. archive.PhoneIt is evaluated first because it is an empty string"".
  2. archive.Phone|default:"未提供"Convert empty string to"未提供".
  3. "电话:"|add:"未提供"Perform concatenation, get"电话:未提供".
  4. Then continue to concatenate",邮箱:"And then concatenatearchive.Email(has value, sodefaultWould not take effect).
  5. Finally, get a complete and friendly string.

By this means, we can ensure that no matter whether the original data is missing, the formatted and complete text will always be output to the user.

Examples of practical application scenarios

This combination is very practical in AnQiCMS template creation, especially when dealing with various dynamic content:

  • Generate structured contact information:When you display company contact information at the bottom of the page, phone, fax, email, and other items may not be complete for everyone.

    {% set contactInfo = {Cellphone: "123-4567-8900", Fax: "", Email: "[email protected]"} %}
    <p>联系电话:{{ contactInfo.Cellphone|default:"暂无" }}</p>
    <p>传真:{{ contactInfo.Fax|default:"暂无" }}</p>
    <p>邮箱:{{ contactInfo.Email|default:"暂无" }}</p>
    {# 如果要拼接成一行: #}
    <p>联系方式:{{ contactInfo.Cellphone|default:"-"|add:" | "|add:contactInfo.Email|default:"-" }}</p>
    {# 结果:联系方式:123-4567-8900 | [email protected] #}
    
  • Build dynamic product descriptions or titles:Product attributes may not be fixed, some may not have a brand or model.

    {% set product = {Brand: "AnQi", Model: "", Color: "Black"} %}
    <h3>{{ product.Brand|default:"未知品牌"|add:" - "|add:product.Model|default:"通用型号"|add:" (颜色:"|add:product.Color|default:"不详"|add:")" }}</h3>
    {# 结果:AnQi - 通用型号 (颜色:Black) #}
    
  • Generate file path or URL:Some images or download links may be missing a specific directory name. “`twig {% set basePath = “https://en.anqicms.com/uploads/English%} {% set year =English