It is crucial to clearly and effectively display contact information when operating your website.AutoCMS (AutoCMS) is equipped with a flexible template engine and a powerful tag system, allowing you to easily implement various complex display requirements.addFilter concatenation display, making your contact phone number clear at a glance.
Why do you need to concatenate the area code and phone number?
Contact information label of Anqi CMS (contact) Basic application
In the Auto CMS, all contact information is processed throughcontactLabel management and invocation. You can find the default contact phone field in the back-end's →.Cellphone),can also be customized as needed, for example, a field specifically for area codes (for instance, namedAreaCode)and a field for local phone numbers (for instance, namedLocalNumber).
Assume 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 as follows:
{# 获取区号 #}
{% contact getAreaCode with name="AreaCode" %}
{# 获取本地电话号码 #}
{% contact getLocalNumber with name="LocalNumber" %}
<p>区号:{{ getAreaCode }}</p>
<p>电话号码:{{ getLocalNumber }}</p>
This code will output '010' and '12345678'.
Core Skill: Usingaddthe filter is used for concatenation
The template engine of AnQi CMS supports multiple filters to process variables. To concatenate the area code and phone number obtained above,addThe filter is your helpful assistant. This filter can be used not only for adding numbers but also excels at concatenating strings, with a very intuitive effect.
Now, we try to concatenate the area code and phone number with a hyphen-as the 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 above code:
- We first go through
{% set ... %}DeclaredareaCodeandlocalNumberTwo variables, and fromcontactLabel the values. The purpose of doing this is to facilitate subsequent logical judgments and repeated use. areaCode|add:"-"It will take the area code (for example010) and delimiter-to form010-.- Immediately following,
|add:localNumberIt will take the result from the previous step010-Concatenate with the local phone number (such as12345678) to form the final010-12345678. - We also demonstrated how to do it by multiple
addFilters, such as static strings (such as