As an experienced website operations expert, I am well aware of the convenience and power that AnQiCMS (AnQi CMS) brings to content management and website operations for us.Its flexible template engine and rich tag features allow us to build and maintain websites efficiently.{% diy %}Can the custom parameter value obtained by the tag be directly used in the conditional judgment of the template?

This question seems simple, but it actually contains an understanding of the working principle of the AnQiCMS template engine. Let's uncover it step by step.

{% diy %}Tag: The powerful assistant for custom content

Firstly, we need to clarify{% diy %}The role of tags. In AnQiCMS,{% diy %}(Custom content tag) is used to retrieve the parameter values you have customized in the "Global Function Settings" or "Contact Information Settings" modules and other modules in the background.These parameters can be additional help links for your website, WhatsApp contact information, or any key-value pairs you set according to your operational needs.

For example, you added a custom parameter namedHelpUrlin the background "Global Function Settings" and set its value tohttps://en.anqicms.com/help. You can pass through it in the template{% diy with name="HelpUrl" %}To get this value and display it. This flexibility allows AnQiCMS to handle personalized content display with ease

Conditional judgment:{% if %}The expectation of tags

Let's take a look at the conditional judgment in the template, we mainly rely on{% if %}tag. This tag allows us to decide whether to render a specific content block based on the result of the expression (true or false). For example,{% if archive.Id == 10 %}It will check if the current document's ID is equal to 10.

{% if %}The tag expects to receive one in its condition part.VariableOr one.An expression that can be evaluated as a boolean value (true or false)It is not designed to directly handle another template tagOutput result.

Core issue: The challenge of direct embedding

Then, when we try to directly embed{% diy %}the output of the tag into{% if %}What will happen in the condition?

For example, we might write it intuitively like this:

{% if {% diy with name="ShowAnnouncement" %} == "true" %}
    <div class="announcement">网站重要通知</div>
{% endif %}

Unfortunately, this format does not workdirectly in the AnQiCMS template engineThe reason is that when the template engine parses{% if %}the tag, it will attempt to evaluateifthe conditional expression following it. And{% diy with name="ShowAnnouncement" %}IfNonespecifying a variable name to receive its value, it is considered a directoutput contentthe operation.The template engine cannot use the result of a direct output operation as part of a conditional expression evaluation.ifThe condition in the statement is the same.

Solution: Assign first, then judge.

In order to use in conditional judgment.{% diy %}The value obtained from the tag, we need to follow the principle of 'assign first, then judge'. Fortunately,{% diy %}The tag itself supports assigning the obtained value to a variable, or we can also use{% set %}Assign the label.

Method one: in{% diy %}Directly assign to the variable in the label (recommended)

This is the most commonly used and recommended method. In{% diy %}In the label, by inwith name="字段名称"Before specifying a variable name, AnQiCMS will assign the obtained value to this variable.

For example, we added a named in the background "Global Function Settings".ShowHeroSectionThe custom parameter, if its value istruethen the hero area of the website will be displayed:

{# 假设后台设置了一个名为 "ShowHeroSection" 的自定义参数,值为 "true" 或 "false" #}
{% diy showHero with name="ShowHeroSection" %} {# 将 "ShowHeroSection" 的值赋给变量 showHero #}

{% if showHero == "true" %}
    <section class="hero-banner">
        <h2>欢迎来到我们的精彩世界!</h2>
        <p>探索更多,发现无限可能。</p>
    </section>
{% endif %}

Thus,showHerothe variable is storedShowHeroSectionThe value (such as “true” or “false”), then{% if %}The tag can be used normally toshowHero == "true"Make a judgment on this expression.

Method two: use{% set %}Tag assignment (suitable for some complex scenarios or coding style preferences)

Although{% diy %}The object itself has the ability to assign values, but in certain specific scenarios, or if you are more accustomed to using{% set %}to explicitly declare and assign values, you can also do this. However,{% diy %}Tags that do not specify a variable name will output the content directly, so this method is usually combined withcaptureor ensurediyLabels are used in contexts that do not render directly (such as within another macro or block). But to avoid unnecessary complexity, for{% diy %}Label, method one is usually more direct and concise.

So, we mainly focus on and recommend method one.

Actual application example

Let's look at several specific scenarios to see how we can flexibly use this combination of assignment and judgment:

  1. Determine the website theme color based on custom parameters:Assuming you have set up a custom parameter namedThemeColorwith a value ofblue/greenorred. You can dynamically load different CSS or styles based on this value:

    {# 后台设置名为 "ThemeColor" 的参数,如 "blue" #}
    {% diy siteThemeColor with name="ThemeColor" %}
    
    
    <style>
        body {
            background-color: {% if siteThemeColor == "blue" %} #e0f2f7; /* 浅蓝色 */
            {% elif siteThemeColor == "green" %} #e6fae6; /* 浅绿色 */
            {% elif siteThemeColor == "red" %} #ffe6e6; /* 浅红色 */
            {% else %} #f7f7f7; /* 默认灰色 */
            {% endif %}
        }
    </style>
    
  2. Control the display switch for specific features:If you have an 'Online Customer Service' module and you want to control its display on the backend through a custom parameter:

    {# 后台设置名为 "EnableLiveChat" 的参数,值为 "true" 或 "false" #}
    {% diy enableChat with name="EnableLiveChat" %}
    
    
    {% if enableChat == "true" %}
        <div class="live-chat-widget">
            <p>在线客服为您服务!</p>
            <button>立即咨询</button>
        </div>
    {% endif %}
    
  3. Combine with numeric custom parameters for logical judgment:If your custom parameter is a number (such as limiting the maximum number of articles displayed per page), don't forget to use a filter to convert it to a numeric type, such as|integeror|float。“`twig{# Assume there is a custom parameter named “MaxArticlesPerPage” with a value of “15” #}{% diy maxArticles with name=“MaxArticlesPerPage” %}

    {% if maxArticles|integer > 10 %}

    <p>当前