How to use `{% if %}` in AnQiCMS template to determine if a contact field exists or is not empty?

Calendar 👁️ 56

In AnQiCMS templates, we often need to decide whether to display a certain area or present information in a different way based on the presence or absence of content.This is especially critical for sensitive and important information such as contact details, we never want to leave a bunch of empty titles or broken links on the page.{% if %}Label to judge whether the contact information field exists or is not empty, making your website interface more intelligent and friendly.

Judgment art in AnQiCMS template.

AnQiCMS is a modern content management system developed based on the Go language, whose template engine adopts a syntax style similar to Django, which provides great convenience for our logical control of content display. Among them,{% if %}Tags are the core tools for implementing conditional judgments.

Its basic syntax is very intuitive:

{% if 条件 %}
    <!-- 条件为真时执行的代码 -->
{% endif %}

You can also introduce{% elif %}(else if) and{% else %}(else) to handle more complex logical branches:

{% if 条件1 %}
    <!-- 条件1为真时执行 -->
{% elif 条件2 %}
    <!-- 条件1为假,条件2为真时执行 -->
{% else %}
    <!-- 所有条件都为假时执行 -->
{% endif %}

Understanding{% if %}The key is, how does AnQiCMS template engine evaluate 'true' and 'false'. Usually, the following values are considered 'false':

  • nil(Empty value)
  • empty string""
  • Number0
  • Booleanfalse
  • Empty array or empty set

This means that when you try to determine whether a variable contains actual content, you can directly use{% if 变量名 %}It usually meets the requirement because it naturally excludes the aforementioned 'false' values.

apply{% if %}Field to judge contact information

AnQiCMS provides convenient{% contact %}Label to obtain contact information configured on the back-end, such as contacts, phone, email, WeChat, etc. The usage of this label is{% contact 变量名称 with name="字段名称" %}. We usually combine to determine whether these fields exist or are not empty{% if %}label and assignment operations

1. The most direct and effective method: assignment and direct judgment of the variable

This is the most commonly used and recommended method. We first take{% contact %}the value obtained from the tag and assign it to a temporary variable, then pass it directly through{% if %}judge this variable.

Example: Check if the Cellphone number exists and is not empty

Assuming you want to display a contact phone number on the page, but only when the phone number has been configured in the background and is not empty:

{# 1. 使用 {% contact %} 标签将电话号码赋给变量 `cellphone` #}
{% contact cellphone with name="Cellphone" %}

{# 2. 使用 {% if %} 判断 `cellphone` 变量是否包含内容 #}
{% if cellphone %}
    <p>联系电话: <a href="tel:{{ cellphone }}" rel="nofollow">{{ cellphone }}</a></p>
{% endif %}

Explanation:If the background "Contact Phone" field is not filled in, or only an empty string is filled in, thencellphonethe variable will be empty,{% if cellphone %}the judgment result will befalseThis code will not be rendered. On the contrary, if a valid phone number is entered,cellphoneThe variable is not empty, the judgment result istrueThe phone information will be displayed normally.

Example: Check if WeChat QR code (Qrcode) exists

For the image or URL field, the judgment logic is the same. If the QR code URL does not exist or is empty, the image will not be displayed.

{# 1. 将微信二维码URL赋给变量 `wechatQrcode` #}
{% contact wechatQrcode with name="Qrcode" %}

{# 2. 判断 `wechatQrcode` 是否存在 #}
{% if wechatQrcode %}
    <div class="wechat-qrcode">
        <img src="{{ wechatQrcode }}" alt="微信二维码" />
        <p>扫码添加微信</p>
    </div>
{% endif %}

2. Slightly clearer but usually unnecessary: combinelengthFilter to determine string length

Although directly checking the variable is sufficient, sometimes you may want to express the intent of "string length greater than 0" more explicitly, you can uselengthfilter.

Example: Check the length of the contact email (Email) field

{# 1. 将联系邮箱赋给变量 `contactEmail` #}
{% contact contactEmail with name="Email" %}

{# 2. 使用 length 过滤器判断邮箱字符串的长度是否大于0 #}
{% if contactEmail|length > 0 %}
    <p>邮箱: <a href="mailto:{{ contactEmail }}">{{ contactEmail }}</a></p>
{% endif %}

Explanation: contactEmail|lengthIt will return the number of characters in the string (Chinese and other UTF-8 characters are also counted as single characters). IfcontactEmailIs an empty string, its length is0,0 > 0Withfalse. This method is consistent with direct{% if contactEmail %} effects, but it adds an additional filter, which is a bit redundant in terms of code. In most cases, directly judging the variable is sufficient.

3. Determine the custom contact information field

The strength of AnQiCMS lies in its high customizability. If you add custom fields in the background "Contact Information Settings" (for example,WhatsApp),You can also use the above method to judge them.

Example: judge custom WhatsApp field

{# 假设后台配置了一个名为“WhatsApp”的自定义联系方式字段 #}
{% contact whatsappNumber with name="WhatsApp" %}

{% if whatsappNumber %}
    <p>WhatsApp: <a href="https://wa.me/{{ whatsappNumber }}" target="_blank" rel="nofollow">{{ whatsappNumber }}</a></p>
{% endif %}

Actual case: contact information display at the bottom of the website

Let's integrate these skills to build a contact information module at the bottom of the website, ensuring that only filled contact information is displayed.

<div class="footer-contact-info">
    <h4>联系我们</h4>
    <ul>
        {# 联系人 #}
        {% contact contactPerson with name="UserName" %}
        {% if contactPerson %}
            <li><strong>联系人:</strong> {{ contactPerson }}</li>
        {% endif %}

        {# 联系电话 #}
        {% contact phoneNumber with name="Cellphone" %}
        {% if phoneNumber %}
            <li><strong>电话:</strong> <a href="tel:{{ phoneNumber }}" rel="nofollow">{{ phoneNumber }}</a></li>
        {% endif %}

        {# 联系地址 #}
        {% contact contactAddress with name="Address" %}
        {% if contactAddress %}
            <li><strong>地址:</strong> {{ contactAddress }}</li>
        {% endif %}

        {# 联系邮箱 #}
        {% contact contactEmail with name="Email" %}
        {% if contactEmail %}
            <li><strong>邮箱:</strong> <a href="mailto:{{ contactEmail }}">{{ contactEmail }}</a></li>
        {% endif %}

        {# 微信 #}
        {% contact wechatAccount with name="Wechat" %}
        {% if wechatAccount %}
            <li><strong>微信:</strong> {{ wechatAccount }}</li>
        {% endif %}

        {# 微信二维码 #}
        {% contact wechatQrcode with name="Qrcode" %}
        {% if wechatQrcode %}
            <li class="qrcode-item">
                <img src="{{ wechatQrcode }}" alt="微信二维码" style="width: 100px; height: 100px;">
                <p>扫码添加微信</p>
            </li>
        {% endif %}

        {# 自定义WhatsApp #}
        {% contact whatsapp with name="WhatsApp" %}
        {% if whatsapp %}
            <li><strong>WhatsApp:</strong> <a href="https://wa.me/{{ whatsapp }}" target="_blank" rel="nofollow">{{ whatsapp }}</a></li>
        {% endif %}
    </ul>
</div>

In this way of writing, the contact information area at the bottom of your website will always remain tidy and organized, presenting only the information that users truly need, greatly enhancing the user experience and the professionalism of the website.

Cautionary notes and **practice

  • Keep it concise.In AnQiCMS template,{% if 变量名 %}is the simplest and most efficient way to determine if a variable exists and is not empty. Unless there is a special requirement, there is no need to uselengthetc. filters for additional judgment.
  • Semantic variable namingPasses through:{% contact %}Assign meaningful names to the variables obtained (such ascellphone/wechatQrcode), which helps improve the readability and maintainability of the template.
  • Make good use ofrel="nofollow"andtarget="_blank": Recommend adding for external links or phone, email linksrel="nofollow"properties to optimize SEO, avoid passing weight; addtarget="_blank"to open in a new tab, enhancing user experience.
  • Pre-configure and testWhen developing templates, it is recommended to configure several contact methods (including empty values and valid values) on the AnQiCMS backend to fully test the robustness of the template.

Frequently Asked Questions (FAQ)

Q1:{% if variable %}and{% if variable|length > 0 %}What is the difference?

A1:In the AnQiCMS template engine, both have almost the same effect for string variables.{% if variable %}Will judgevariableWhether it is a 'false' value (empty string, nil/0/

Related articles

Whether the `Qrcode` image output by the contact information label supports custom `alt` text attribute?

As an experienced website operations expert, I know that every detail in a content management system can affect the overall performance of the website, whether it's user experience or search engine optimization (SEO).Today, let's delve deeper into the QR code image output of the contact information label in AnQiCMS (AnQiCMS), whether the `alt` text attribute supports customization, and how we can use it flexibly.### Security CMS QR code image `alt` text attribute customization: Insightful functions and practical strategies In modern website operations

2025-11-06

How to make all external links output by AnQiCMS contact information tags automatically add the `rel="nofollow"` attribute?

As an experienced website operations expert, I fully understand your attention to the details of website SEO, especially the handling of external links.In AnQiCMS, to automatically add the `rel="nofollow"` attribute to all external links output by the contact information label, this involves understanding the core function mechanism of the CMS and also requires some flexible template application skills.First, let's delve into the meaning of the `rel="nofollow"` attribute.This attribute tells the search engine not to consider this link as an 'endorsement' or 'vote' for the target website

2025-11-06

Do I support automatically adding `tel:` and `mailto:` links to contact numbers and email addresses, AnQiCMS?

In today's website operations, improving user experience and convenience is one of the core competitive advantages.When a user sees a phone number or email address on a website and can directly click to dial or send an email, it undoubtedly greatly improves efficiency and satisfaction.So, does AnQiCMS (AnQi Content Management System) support automatically adding `tel:` and `mailto:` links to the contact phone numbers and email addresses on the website?As an experienced website operations expert, I can confidently tell you that AnQiCMS provides a flexible mechanism to meet this requirement

2025-11-06

How to implement responsive layout and optimize loading speed for WeChat QR code images in AnQiCMS template?

As an experienced website operation expert, I am well aware that in today's multi-device environment, the display effect and loading speed of website content are key factors determining user experience and search engine performance.The WeChat QR code is an important tool for connecting online and offline and promoting user conversion, and its presentation in the AnQiCMS template also requires fine-tuning.Today, let's delve into how to achieve a perfect responsive layout and optimize loading speed for your WeChat QR code image in AnQiCMS templates, providing a smooth user experience.

2025-11-06

If an AnQiCMS contact field is empty, how can you display default text in the template instead of leaving it blank?

## AnQiCMS template optimization: How to cleverly display default text when the contact information field is empty?In website operation, maintaining the integrity of website content and user experience is crucial.AnQiCMS (AnQiCMS) provides a solid foundation for building various websites with its flexible content management and powerful template functions.

2025-11-06

The `{% contact %}` tag supports which filters to format the output content?

As an experienced website operation expert, I know that how to flexibly display and process information in a content management system is the key to improving website user experience and operational efficiency.AnQiCMS (AnQiCMS) provides us with great convenience with its powerful template engine and rich tag system.Today, let's delve into the `{% contact %}` tag and how to make your website's contact information display more exquisite and practical through its output content filters.

2025-11-06

How to use AnQiCMS's `{% system %}` tag and `{% contact %}` tag to combine and display the global contact information of the website?

As an experienced website operations expert, I know that effectively and uniformly managing website content is the foundation of success in an increasingly complex network environment.AnQiCMS (AnQiCMS) leverages the efficient and flexible customization capabilities of the Go language to provide us with many conveniences.Today, let's delve deeply into two seemingly simple but extremely powerful tags in Anqi CMS - `{% system %}` and `{% contact %}`, as well as how to skillfully combine them to build a unified and easy-to-maintain website global contact information display system

2025-11-06

Why did I use the `{% contact %}` tag in the AnQiCMS template but the content did not display on the front end?

As an experienced website operations expert, I know that even a small template tag problem may confuse and anxiety the operator while managing and maintaining a website.Today, let's delve into a problem that AnQiCMS (AnQi CMS) users occasionally encounter: when the `{% contact %}` tag is used in the template, no content is displayed on the front page.Don't worry, this is usually not a big problem. Just follow the steps to investigate, and we can find the cause and solve it soon.

2025-11-06