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
"" - Number
0 - Boolean
false - 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 of
rel="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/