How to determine if a custom field exists or has a value in `archiveParams` before displaying it?

Calendar 👁️ 54

As an experienced website operations expert, I am well aware of the powerful potential of AnQiCMS (AnQi CMS) in content management and website optimization.The flexible content model and customizable fields provide great convenience for us to build highly personalized websites.However, how to elegantly handle these dynamically generated custom fields in template design, ensuring that they are displayed only when they exist or have values, is the key to improving the quality of the template and user experience.archiveParamsIn the tag, cleverly judge the existence and value of custom fields to achieve intelligent display of content.

Flexible application of AnQiCMS custom fields

One of the most prominent features of AnQiCMS is its highly flexible content model.Whether it is an article, product, case or any other type of business content, we can add a series of custom fields for each content model according to actual needs.For example, a "product" model may require fields such as "product model

The introduction of these custom fields greatly enriches the dimensions of the content, but also poses higher requirements for the development of front-end templates.We do not want to have empty titles or placeholders in the template. If a field is not filled in, it should not be displayed. This not only concerns aesthetics but also enhances the professionalism of the website.

archiveParams: The key to unlocking custom fields

In the AnQiCMS template system,archiveParamsIt is a very important tag, which is specifically used to retrieve all the background custom parameters associated with the current document (archive) or the specified document.The strength of this tag lies in its ability to expose various custom fields defined in the content model in a structured way to the front-end template for rendering.

archiveParamsThere are two main ways to obtain labels, which directly affect our strategy for determining whether a field exists or not:

  1. sorted=true(Default):In this mode,archiveParamsIt will return an array of objects arranged in an order defined by the backend. Each element of the array is a containingName(field display name) andValueThe structure of the actual field value. This method is suitable for traversing all custom fields and displaying them uniformly.
  2. sorted=false:When we know the name of the custom field we want to query, we willsortedis set tofalseIt will be more convenient. At this point,archiveParamsIt will return an object similar to Go languagemapWe can directly access it through the "field call" (defined in the background content model settings) of the field,ValueFor example, if the custom field's call field isauthorwe canparams.author.ValueGet its value.

understand the difference between these two ways of acquisition, which is the premise for efficient field judgment.

How to determine if a field exists or has a value?

Now, let's take a specific look at how to make existence or value judgments in the two scenarios mentioned above. AnQiCMS (based on the Pongo2 template engine)ifThe logic judgment label handles empty values very intelligently, which provides us with great convenience. Inifthe condition, empty strings ("falseas well asnilNothing, etc. will be treated asfalse.

Scenario 1: Traverse all custom fields (sorted=true)

When we want to traverse all the custom fields of the document and only display those with content, sorted=trueThe pattern is preferred.

First, througharchiveParamsTag to get all parameters:

{% archiveParams params with sorted=true %}
    {# 接下来,我们将遍历这些参数 #}
    {% for item in params %}
        {# 在这里进行判断,确保字段有实际值才显示 #}
        {% if item.Value %}
            <div class="custom-field-item">
                <span class="field-name">{{ item.Name }}:</span>
                <span class="field-value">{{ item.Value }}</span>
            </div>
        {% endif %}
    {% endfor %}
{% endarchiveParams %}

Parsing:In this example,{% if item.Value %}It is the core of judgment. Ifitem.Valueis an empty string,nilOr if the template engine considers other values as 'empty', thenifThe condition will not be met, the HTML structure of this field will not be rendered, thereby avoiding the display of meaningless blank areas.

Scenario two: Accurately judge specific custom fields (sorted=false)

If your template design requires displaying a custom field with a specific name, such as "author", "product features", or "contact phone number", then usesorted=falseIt will be more direct.

Suppose we define a custom field in the background, whose 'called field' isproductFeaturesAnd its value is a multi-line text, each line representing a feature.

”`twig {% archiveParams customFields with sorted=false %}

{# 假设我们有一个名为 'productFeatures' 的自定义字段 #}
{% if customFields.productFeatures.Value %} {# 检查 'productFeatures' 字段是否有值 #}
    <div class="product-features">
        <h3>产品特点:</h3>
        <ul>
            {# 假设 productFeatures.Value 是一个多行文本,我们可以按行分割并显示 #}
            {%

Related articles

How to render different input controls (such as text, radio, checkbox) in the `guestbook` form based on the `Type` attribute?

As an experienced website operations expert, I know that a flexible and versatile content management system is the foundation of website success.AnQiCMS (AnQiCMS) with its excellent flexibility and powerful customization capabilities has become an invaluable tool in our hands.Today, let's delve deeply into a very practical topic in actual operation: how to intelligently render different input controls, such as text boxes, radio buttons, or checkboxes, in the `guestbook` form of Anqi CMS according to the preset field types in the background.This is not just an implementation at the technical level

2025-11-06

How to dynamically add a required asterisk or hint based on the `Required` property of the `guestbook` form label?

As an experienced website operation expert, I know that every detail can affect user experience and operation efficiency.When building a website, forms are an important part of interacting with users, and clear, friendly form design is the key to improving conversion rates.AnQiCMS (AnQiCMS) with its flexible template engine and powerful backend management features, has provided us with great convenience, especially in handling scenarios such as message forms that require users to fill in information.Today, let's delve deeply into a seemingly simple but extremely practical skill: how to use the `Required` field of the back-end field

2025-11-06

How to determine if a comment in `commentList` has a parent comment (`Parent`) and display the reply structure?

As an experienced website operations expert, I fully understand how to effectively display user comments in content management, especially comments with reply structures, which are crucial for enhancing user interaction and website activity.AnQiCMS (AnQiCMS) relies on its powerful template engine and flexible data tags to make this requirement easily achievable.Today, let's delve into how to cleverly judge whether a comment has a parent comment in the AnQiCMS `commentList` tag and elegantly present a clear reply structure.

2025-11-06

How to judge whether the comment (`Status`) in the AnQiCMS template has passed the review and be displayed differently?

As an experienced website operation expert, I know the importance of the comment system for website user interaction and content ecology.In such a powerful content management system as AnQiCMS, flexibly controlling the display of comments, especially the differentiated display based on the review status, is a key factor in improving user experience and maintaining the quality of website content.Today, let's delve into how to use the `commentList` tag in the AnQiCMS template to judge the `Status` field of comments and implement intelligent differentiated display.

2025-11-06

How to use the `IsCurrent` property to mark the currently selected filter condition in the `archiveFilters` filter tag?

As an experienced website operation expert, I deeply understand the critical role of user experience (UX) in the success of a website.A user-friendly, responsive interface can effectively guide users to discover content, and the content filtering feature is an important aspect of improving user experience.In AnQiCMS (AnQiCMS) this efficient and customizable content management system, the `archiveFilters` tag provides strong support for building flexible filtering functions, and its internal `IsCurrent` property is the 'magic wand' that lights up the user experience. Today

2025-11-06

How to elegantly handle empty list situations in AnQiCMS template using the `{% for ... empty ... %}` syntax?

In AnQiCMS template development, we often need to display a series of content lists, such as article lists, product lists, navigation menus, or friend links.However, these lists are not always filled with data. When the list is empty, how to elegantly inform the user that "there is no content here" instead of displaying a blank page or an error message has become a detail consideration in template design.AnQi CMS is well-versed in this, drawing on the excellent design of Django templates in its powerful template engine developed in Go language, providing us with `{% for ...

2025-11-06

How to use the `if` tag in combination with `forloop.Counter` to alternate the odd and even row styles of list items?

As an experienced website operation expert, I fully understand how to enhance user experience through detailed interface design in content presentation.AnQiCMS, with its high-performance architecture based on Go language and flexible template engine, provides us with great freedom to easily implement all kinds of creativity and features.

2025-11-06

How to use the `filter-contain` filter in AnQiCMS to check string or array containment in the `if` statement?

As an experienced website operations expert, I fully understand that in an increasingly complex network environment, efficient content management and flexible page display are crucial for improving user experience and SEO effectiveness.AnQiCMS (AnQiCMS) provides a series of practical tools with its high-performance architecture based on the Go language and the powerful features of the Django template engine.

2025-11-06