How to concatenate the area code and phone number in the contact (`contact` tag) using the `add` filter?

Calendar 👁️ 61

It is crucial to clearly and effectively display contact information when operating your website.AnQiCMS (AnQiCMS) with its flexible template engine and powerful tag system, allows you to easily implement various complex display requirements.Today, let's talk about a common but somewhat tricky requirement: how to use the area code and phone number throughaddFilter concatenation display, making your contact phone number clear at a glance.

Why do you need to concatenate the area code and phone number?

Imagine if the contact phone number on the website were the two separate numbers "010" and "12345678", visitors might need to combine themself to understand that this is a complete number.Concatenating it into "010-12345678" or "(010) 12345678" not only allows visitors to see the complete contact number at a glance, avoiding confusion, but also provides a better visual effect in some designs and lays the foundation for creating clickable dialing links in the future.

Anqi CMS contact information label (contact) Basic application

In Anqi CMS, all contact information is processed throughcontactLabel management and calling. You can find the default contact phone field in the background settings -> Contact Settings.Cellphone),can also be used to customize new fields, such as a field specifically for area codes (for example, namedAreaCode) and a field for local phone numbers (for example, namedLocalNumber)

Assuming you have already set up the following two custom contact information fields in the background:

  • Parameter name: AreaCode(Parameter value:)010)
  • Parameter name: LocalNumber(Parameter value:)12345678)

In the template, you can get their values respectively in this way:

{# 获取区号 #}
{% contact getAreaCode with name="AreaCode" %}
{# 获取本地电话号码 #}
{% contact getLocalNumber with name="LocalNumber" %}

<p>区号:{{ getAreaCode }}</p>
<p>电话号码:{{ getLocalNumber }}</p>

This code will output '010' and '12345678' separately.

Core Tips: UseaddFilter concatenation

The AnQi CMS template engine supports various filters to process variables. To concatenate the area code and phone number obtained above,addThe filter is your helpful assistant. This filter is not only used for adding numbers, but is also good at concatenating strings with a very intuitive effect.

Now, we try to concatenate the area code and the phone number with a hyphen-as a separator:

{% set areaCode = "" %}
{% set localNumber = "" %}

{# 从后台获取区号和电话号码,假设您已在后台联系方式中设置了这两个自定义字段 #}
{% contact fetchedAreaCode with name="AreaCode" %}
{% if fetchedAreaCode %}{% set areaCode = fetchedAreaCode %}{% endif %}

{% contact fetchedLocalNumber with name="LocalNumber" %}
{% if fetchedLocalNumber %}{% set localNumber = fetchedLocalNumber %}{% endif %}

{# 拼接区号和电话号码 #}
{% if areaCode and localNumber %}
    <p>联系电话:{{ areaCode|add:"-"|add:localNumber }}</p>
    {# 另一种可能的需求,带括号并有空格分隔 #}
    <p>联系电话:{{ "(" | add:areaCode | add:") " | add:localNumber }}</p>
{% elif localNumber %}
    {# 如果只有本地电话,则只显示本地电话 #}
    <p>联系电话:{{ localNumber }}</p>
{% endif %}

In the code above:

  1. We first pass through{% set ... %}DeclaredareaCodeandlocalNumbertwo variables, and fromcontactRetrieve their values from the tags. The purpose of doing this is to facilitate subsequent logical judgments and repeated use.
  2. areaCode|add:"-"Will add the area code (for example010) with a delimiter-to010-.
  3. Immediately thereafter,|add:localNumberWill add the result of the previous step010-Concatenate with local phone numbers (for example12345678) to form the final010-12345678.
  4. We also demonstrated how to use multipleaddfilters to convert static strings (such as

Related articles

Can the `add` filter be used to concatenate the website name and footer copyright information obtained from the `system` tag?

In website operation, we often need to finely control the display of page content, especially those global information that needs to be repeatedly displayed throughout the website, such as the website name, footer copyright, and so on.AnQiCMS (AnQiCMS) provides powerful template tags and filters to help us flexibly handle these requirements.Today, let's talk about a very practical scenario: Can the `add` filter of AnQi CMS be used to concatenate the website name and footer copyright information obtained from the `system` tag?The answer is affirmative, and this method is both intuitive and efficient.

2025-11-07

How to use the `add` filter in a `for` loop to dynamically generate a unique `id` or `class` attribute for list items?

In AnQi CMS template development, we often need to handle the display of dynamic lists.Whether it is an article list, product display, or other content block, dynamically generating a unique `id` or `class` attribute for each item in the list is particularly important in order to achieve finer style control, JavaScript interaction, or to ensure the uniqueness of page elements.Today, let's talk about how to cleverly use the `add` filter in the `for` loop of AnQiCMS to achieve this goal.### Understanding the demand for dynamic attribute generation Imagine that

2025-11-07

How to use the `add` filter to build dynamic SEO-friendly URL paths in the Anqi CMS template?

In Anqi CMS template creation, flexible and SEO-friendly URL paths are the key to improving the website's search engine performance.Although AnQi CMS provides powerful pseudo-static rule configuration capabilities in the background, generating static URLs through patterns such as `/module-id.html`, but in certain specific scenarios, we may need to control or combine the local URL path more dynamically and finely within the template to meet unique business needs or marketing strategies.At this point, the `add` filter has become a very practical auxiliary tool.###

2025-11-07

How to use the `add` filter to dynamically add 'Yuan' or a specific currency symbol after the product price?

When managing website content, especially when it involves product display, we often need to present price and other numerical information in a more user-friendly manner.单纯显示一串数字,比如 `199`,往往不够直观,如果能自动加上货币符号,比如 `199元` 或 `$199`,就能大大提升信息的可读性和专业性。AnQiCMS (AnQiCMS) with its flexible template system makes it very simple to meet such requirements, and the key to this is the clever use of the `add` filter.### Understand `add`

2025-11-07

When handling floating-point number addition with the `add` filter, should special attention be paid to its calculation accuracy?

When using AnQi CMS for website content management, template tags and filters are tools we often use, greatly enhancing the flexibility of content presentation.Among them, the `add` filter allows us to perform addition operations of numbers or strings in the template, which is very convenient in many scenarios.However, when it comes to adding floating-point numbers (which are decimal numbers), some users may wonder: Does the `add` filter need to pay special attention to its calculation accuracy when handling such calculations?

2025-11-07

How to use the `add` filter to dynamically add a brand name or fixed suffix to the `Title` of the page `TDK`?

In website content operation, the page's "Title", "Keywords", and "Description", which is what we often call TDK, plays a crucial role in search engine optimization (SEO).A well-structured and brand-recognizable page title that not only helps search engines better understand the page content but also attracts users' attention in search results.AnQiCMS provides a flexible template engine, allowing us to finely control these key information

2025-11-07

The `add` filter performs mathematical calculations in the AnQiCMS template, what is the difference from the arithmetic operation tag in `tag-calc.md`?

During the template development process of AnQiCMS, we often encounter scenarios where numerical calculations or logical judgments are required.The system provides us with various processing methods, among which the `add` filter and the arithmetic operation capabilities introduced in `tag-calc.md` are two commonly used tools.Although they all involve numerical processing, they have significant differences in functional positioning, usage, and complexity.First, let's understand the `add` filter.As the name implies, the `add` filter is mainly used to perform **addition operations or string concatenation**

2025-11-07

How to use the `add` filter in a loop to implement alternating row colors or dynamically concatenate different CSS class names?

In modern web design, the visual presentation of list content has an important impact on user experience.Whether it is an article list, product display, or comment section, by giving list items different styles, such as alternating row colors or applying different class names based on specific conditions, it can significantly improve the readability and aesthetics of the page.AnQiCMS is a powerful template engine that provides flexible tools to meet these dynamic style requirements, where the `for` loop's `cycle` tag and `add` filter are our reliable assistants.###

2025-11-07