How to judge whether a contact field (such as Qrcode) in the AnQiCMS template has a value?

Calendar 👁️ 63

In website operation and template development, we often need to dynamically adjust the display of the front-end page based on the setting of the background content.Especially for contact information and other important information, ensure that it is displayed after it exists to effectively avoid broken links, blank areas, or incomplete information on the page, thereby improving the user experience.

Today, let's delve deeply into how to make elegant and accurate judgments in the AnQiCMS template.contactThe contact information field obtained by the tag (for example)Qrcodehas been set.

Know AnQiCMS'scontactTag

Built-in in AnQiCMScontactTags are a very practical feature that allows us to directly access various contact information from the preset "Contact Settings" in the background, such as contacts, phone numbers, addresses, emails, as well as the WeChat QR code we are focusing on today (Qrcode)et al. This tag is usually used in the following way{% contact 变量名称 with name="字段名称" %}.

For example, if you want to get the address of the WeChat QR code image set in the background, you can write it like this:

{% contact with name="Qrcode" %}

This simple tag will directly output the backgroundQrcodeThe field value.But what if the backend does not upload the QR code image, what will this label output?It usually outputs an empty string.<img>label'ssrcThe attribute, will cause the page to display a broken image icon, which is obviously not what we want.

Skillfully use{% if %}The tag determines whether the field has a value

AnQiCMS's template engine supports syntax similar to Django templates, where{% if %}Tags are the core tools for conditional judgment.It can determine whether a variable has a 'truthy' value.{% if 变量 %}the judgment result will befalseThis means that we do not need to worry about the specific 'empty' type returned when the background field is not set, as the template engine will intelligently judge it as having no value.

To judgeQrcodeIf the field has a value, we can take the following steps:

  1. Assign the field value to a variable:To facilitate subsequent judgments and uses, it is recommended to first assigncontactTags retrieved from comments.QrcodeThe value to a temporary variable, such asqrcodeUrl.

    {% contact qrcodeUrl with name="Qrcode" %}
    

    This statement will attempt to retrieve the contact information namedQrcodeand store the result in a variable. If the background is not set,qrcodeUrlit will be an empty string. Now, we can directly accessqrcodeUrl.

  2. Use{% if %}Label for judgment:the variable.qrcodeUrlThe variable makes a conditional judgment.

    {% if qrcodeUrl %}
        <!-- 如果 qrcodeUrl 有值,则显示二维码 -->
    {% else %}
        <!-- 如果 qrcodeUrl 没有值,则显示替代内容或不显示 -->
    {% endif %}
    

Combining these two steps, we can build a robust and user-friendly QR code display logic:

{# 1. 从后台获取 Qrcode 字段的值,并赋给 qrcodeUrl 变量 #}
{% contact qrcodeUrl with name="Qrcode" %}

{# 2. 判断 qrcodeUrl 变量是否有值 #}
{% if qrcodeUrl %}
    <div class="contact-item contact-qrcode">
        <p class="title">扫描微信二维码添加好友:</p>
        <img src="{{ qrcodeUrl }}" alt="微信二维码" class="qrcode-image">
    </div>
{% else %}
    {# 当后台没有设置微信二维码时,我们可以选择显示一段提示文本,或者干脆什么都不显示 #}
    <div class="contact-item no-qrcode">
        <p class="title">微信二维码暂未提供。</p>
        <!-- 或者您也可以留空,不显示任何内容 -->
    </div>
{% endif %}

In the above code, only whenqrcodeUrlWhen there is a real value,<img>the tag will be rendered, thus avoiding the problem of broken images. IfqrcodeUrlIf empty, the page will display a prompt saying 'WeChat QR code is not provided yet', instead of an eye-sores placeholder.

Generalization: Applies to all contact information fields

This judgment logic is not only applicable toQrcodefields, but also to all fields obtained throughcontacttags in AnQiCMS. Whether it is to judge contactCellphonephone, contact emailEmailOr it is any contact information field that you customize on the backend, and its judgment method is the same.

For example, judgment of contact phone number:

{% contact phoneNumber with name="Cellphone" %}
{% if phoneNumber %}
    <p>联系电话:<a href="tel:{{ phoneNumber }}">{{ phoneNumber }}</a></p>
{% else %}
    <p>暂无联系电话。</p>
{% endif %}

By first assigning the field value to a variable, then{% if %}The pattern of judgment, you can ensure that the website can display contact information beautifully in different configuration states, enhance the user experience and the professionalism of the website.This flexible conditional control capability is a manifestation of the strength of AnQiCMS templates, and it is also an important guarantee for our refined website operation.


Frequently Asked Questions (FAQ)

  1. Q: Why can't we just{% if contact with name="Qrcode" %}judge like this?A:{% contact ... %}The tag itself is used to output content, not to directly return a boolean value forifJudgment. In the template syntax of AnQiCMS, a condition judgment usually requires a variable that has already been assigned. Therefore, first through{% contact variableName with name="FieldName" %}Assign a value tovariableName, and then use{% if variableName %}Make a judgment, it is a more standardized and feasible approach.

  2. Q: How can I judge if any of the contact methods have a value and display a common 'Contact Us' block?A: You can first get the values of these fields into different variables, then use{% if %}the logical OR (or) operator for combination judgment.

    {% contact phone with name="Cellphone" %}
    {% contact email with name="Email" %}
    {% contact qrcode with name="Qrcode" %}
    
    {% if phone or email or qrcode %}
        <div class="contact-us-block">
            <h3>联系我们</h3>
            {% if phone %}<p>电话:{{ phone }}</p>{% endif %}
            {% if email %}<p>邮箱:{{ email }}</p>{% endif %}
            {% if qrcode %}<p>扫描二维码:<img src="{{ qrcode }}" alt="联系二维码"></p>{% endif %}
        </div>
    {% else %}
        <p>暂未提供任何联系方式。</p>
    {% endif %}
    
  3. Q: Besides checking if a field has a value,{% if %}can you check other conditions?A: Of course you can.{% if %}The tag supports multiple condition checks, such as:

    • Comparison operation: {% if count > 0 %}/{% if price == 100 %}
    • String contains: {% if "keyword" in item.Title %}
    • Boolean value: {% if isActive %}
    • Logical combination: {% if user.IsAdmin and user.IsLoggedIn %}This makes AnQiCMS templates highly flexible, capable of meeting various complex page logic requirements.

Related articles

How to judge whether the website is closed and display a prompt based on the `SiteCloseTips` of the system settings (`{% system %}`)?

As an experienced website operations expert, I know that the stable operation of the website is of great importance, but sometimes, due to reasons such as maintenance, upgrades, or data migration, the website has to temporarily shut its doors.How to communicate with visitors elegantly during a closure period, ensuring user experience while effectively managing expectations, is a detail that every operator needs to pay attention to.AnQiCMS (AnQiCMS) provides a flexible mechanism in this regard, allowing us to deal with it easily.Today, let's delve deeply into how to set up tags in Anqi CMS through system settings `{% system

2025-11-06

How to determine if there is a previous or next document on the document detail page and display navigation links?

As an experienced website operations expert, I know that a smooth reading experience is crucial when users browse content.A good document detail page should not only display the core content but also guide users to the next step, such as reading related content or easily switching to the previous or next page.This not only increases the user's stay time on the website and reduces the bounce rate, but it is also SEO-friendly, helping search engines to better crawl and understand the website structure.In AnQiCMS (AnQiCMS), implement the previous and next navigation links on the document detail page

2025-11-06

In AnQiCMS template, how to determine if a certain `flag` attribute (such as `recommended` or `headline`) is set?

As an experienced website operations expert, I am well aware in my daily work that the flexibility of content display is crucial for user experience and operational efficiency.AnQiCMS (AnQi Content Management System) is the choice of many enterprises and operators due to its powerful customizability and simple and efficient template engine.Today, let's delve into a very practical skill in AnQiCMS template making: how to determine if the specific `flag` attribute (such as "Recommended" or "Top") of a document is set, and adjust the content display accordingly.

2025-11-06

How to display a specific content block based on the `archiveDetail` or `categoryDetail` Id value?

As an experienced website operations expert, I am well aware that how to flexibly display specific content in content management is crucial for improving user experience and achieving operational goals.AnQiCMS (AnQiCMS) takes advantage of its powerful template engine and rich tag system, providing us with great convenience.Today, let's delve into how to cleverly use the `archiveDetail` and `categoryDetail` tag's `Id` value to accurately control the display of content blocks on the website.

2025-11-06

How to highlight the current page in the `navList` navigation menu based on the `IsCurrent` property?

In the fast-paced digital world, a clear and intuitive website navigation menu is the foundation of a good user experience.As an experienced website operations expert, I am well aware of AnQiCMS's powerful capabilities in providing efficient and customizable content management solutions.It not only performs excellently but also considers the operator's needs in detail, for example, the built-in `navList` tag combined with the `IsCurrent` attribute can easily highlight the current page, greatly enhancing the professionalism and user-friendliness of the website.

2025-11-06

How to determine if the `navList` menu item has child navigation (`item.NavList`) and perform nested rendering?

## AnQi CMS navigation menu: skillfully use `navList` to implement multi-level nesting and child navigation judgment As an experienced website operations expert, I fully understand the importance of a clear and flexible navigation system for user experience and the overall architecture of the website.In AnQiCMS (AnQiCMS), the `navList` tag is the core tool we use to build a powerful navigation system.

2025-11-06

How to determine if the `linkList` friendship link tag list is empty to avoid displaying an empty tag?

As an experienced website operations expert, I have accumulated rich experience in content management and template optimization in AnQiCMS.I know well that a smooth, beautiful and quick-loading website cannot be separated from the meticulous refinement of every detail.Today, let's delve deeply into an issue that is often overlooked in template development but is crucial for user experience and page neatness: how to avoid displaying redundant empty tags when the `linkList` friendship link tag list is empty.Avoid displaying empty tags: AnQi CMS

2025-11-06

How to conditionally output the standard link according to the `CanonicalUrl` tag in AnQiCMS templates?

Hello! As an experienced website operations expert, I fully understand that in daily work, we not only need to pay attention to the quality of the website content, but also to the impact of technical details on SEO.The canonical URL is one of the crucial details that can effectively solve the problem of duplicate website content, concentrate page authority, and thus improve search engine rankings.Today, let's delve deeply into how to intelligently and elegantly output standard links based on the existence or absence of the `CanonicalUrl` field within the `tdk` tag in AnQiCMS templates

2025-11-06