How to dynamically judge whether a custom field of a content model is set as required in the template?

Calendar 👁️ 84

AnQi CMS is an efficient and flexible content management system, and its powerful content model customization features indeed bring great convenience to enterprises and operators.In daily content operations, we often add various custom fields to different content models (such as articles, products, events, etc.) according to business needs.These fields are optional, some are required to be filled in, that is, 'required items'.How can you dynamically determine whether a custom field is set as required in a frontend template and adjust the page display logic accordingly?This is indeed a very practical and worthy of in-depth exploration question.

To answer this question, we first need to understand the working principle of the AnQiCMS template engine and its provided tag features.AnQiCMS uses a syntax similar to the Django template engine, allowing developers to call and render data through specific tags and variables.archiveListUsed to get the document list,archiveDetailUsed to obtain document details and what we need to focus on todayarchiveParamstag, which is specifically used to obtain the custom parameters set for the document by the background.

Understand the content model and custom fields of AnQiCMS

In the AnQiCMS backend, when we enter the 'Content Model' management interface, we will find that the system has built-in 'Article Model' and 'Product Model', and we can also create new custom models according to specific needs.Each model can have its own unique custom fields.For a "product model

When defining these custom fields, AnQiCMS provides a key option - 'whether required'.If we set a field as required, the system will perform a strict check on the backend when the user submits content to ensure that the field is not empty.This is crucial for ensuring the integrity of the data and the correctness of the business logic.

Field data retrieval in templates:archiveParamsThe use of tags

Now, let's turn our attention to the template. AnQiCMS providesarchiveParamsLabel. The main function of this tag is to obtain all the custom parameters configured in the background of the specified document (or the current document). Its basic usage method is:

{% archiveParams params %}
<div>
    {% for item in params %}
    <div>
        <span>{{item.Name}}:</span>
        <span>{{item.Value}}</span>
    </div>
    {% endfor %}
</div>
{% endarchiveParams %}

In this code block,paramsIs an array that contains all custom field information whensorted=trueThis is the default behavior). In the loop, theitemobjects usually containName(the Chinese display name of the field) andValue(The specific field value). In this way, we can flexibly display the name of each custom field and its corresponding content on the front-end page.

However, it is important to note that according to the AnQiCMS template tag design,archiveParamsthe tag returns theitemobject, mainly focusing on the field's "display name"(Name)and actual content(Value),it does not directly contain information about whether the field is a required item(Required)such asmetadatainformation. This means that we cannot directly through{{item.Required}}This way of determining whether a custom field is set as required.

Why it is special to directly judge the 'required' attribute in the template.

This is not a missing feature of AnQiCMS, but rather a common consideration in the design of most template engines. The main responsibility of a template engine isto render dataPresent to the user rather than directly querying and processingSystem configuration or database architecture. The 'required' field belongs to the definition of the content model itself, which is a meta-data at the configuration level, not part of the document content.

In the AnQiCMS architecture, the validation of 'whether it is required' mainly occurs on the backend. When the user submits document content through the backend editor, the system will strictly verify the data according to the rules defined in the content model.If the required field is empty, the backend will prevent submission and prompt the user.This is a standard practice to ensure data integrity and system security through front-end and back-end separation.

Practical strategies for displaying the "required" status on the front-end

Since we cannot directly pass through the templatearchiveParamsHow can we effectively display which fields are required to users on the front-end page when we need to obtain the 'required' status of a field? There are several practical and commonly used strategies:

  1. Based on known information, a hard-coded tag:This is the most common and simplest method.In most website designs, which fields are required items are usually determined in the product planning and interface design stage.As a template developer, you will learn this information from the design drafts or backend configuration.*Or apply a specific style to visually inform the user.

    {% archiveParams params %}
    <div>
        {% for item in params %}
        <div>
            <span>{{item.Name}}
            {%- if item.Name == "产品编号" or item.Name == "颜色" -%}
                <span style="color: red;">*</span>
            {%- endif -%}
            :</span>
            <span>{{item.Value}}</span>
        </div>
        {% endfor %}
    </div>
    {% endarchiveParams %}
    

    This method is not enough "dynamic", but for websites with relatively stable field structures and infrequent changes, it is very efficient and easy to maintain.

  2. Backend controller preprocessing, passing the 'required' status to the template context:For those who need high dynamism, the status of required fields may change frequently, and you want the front-end display to be fully synchronized with the back-end configuration, the most elegant solution is to handle the data processing in the back-end controller layer of AnQiCMS.This means that when the backend prepares data and passes it to the template, it can also query the definition of the content model to get the 'required' status of each custom field, and inject this metadata into the template context.

    Although the AnQiCMS template tag document does not directly show how to query model definitions from templates, the backend developed in Go language is fully capable of obtaining this information. Once the backend retrieves similarrequiredFields = ["产品编号", "颜色"]The list or a more detailed field definition object is passed to the template, and the front-end template can render a required marker through a simpleifjudgment.

    ”`twig {# Assuming the backend has passed an array named ‘customFieldDefinitions’ to the template, which includes the Name and Required status of each field #} {% if customFieldDefinitions %}

    {% for fieldDef in customFieldDefinitions %}
        {% if fieldDef.Required %}
            {# 渲染这个必填字段的输入框或显示区域,并加上星号 #}
            <div>
                <span>{{fieldDef.Name}}<span style="color: red;">*</span>:</span>
                {# 这里还需要对应地找到该字段的实际值来显示 #}
                {% archive
    

Related articles

Can I define different types of 'recommendation attributes' for different content models (such as 'top news' for article models and 'new product' for product models)?

As an experienced website operations expert, I know that accurately recommending different types of content in content management is the key to improving user experience and operational efficiency.AnQi CMS, with its flexible content model design, indeed provides us with powerful content organization capabilities.And regarding the differentiated definition of 'recommended attributes', this embodies some exquisite operational strategies and technical implementation methods, which are worthy of in-depth exploration.### AnQi CMS: The Flexible Application of Content Model and "Recommended Attributes" Many operators will have a question when they first contact AnQi CMS

2025-11-07

What is the collaboration or difference between the `archiveParams` tag and the `moduleDetail` tag in obtaining custom field information of the content model?

Today, with the increasing diversity of digital content, a flexible content management system (CMS) is undoubtedly the key to improving efficiency and achieving personalized display for enterprises and self-media operators.AnQiCMS (AnQiCMS) with its powerful content model function enables users to deeply customize the content structure according to their own business needs.However, when we delve into the template level, trying to retrieve this customized data, we may encounter some questions: `archiveParams` tag and `moduleDetail` tag

2025-11-07

When a content model has custom fields (such as `author`), how can these model-specific custom fields be called in the `archiveDetail` tag?

Unlock the charm of AnQi CMS content model custom fields: advanced application of `archiveDetail` tag As a senior website operations expert, I fully understand the importance of a flexible content management system in improving operational efficiency and achieving personalized content display.AnQiCMS (AnQiCMS) excels in this aspect with its efficient architecture based on the Go language and excellent customizability.

2025-11-07

How to combine the `archiveList` tag to filter and display the document list of a specific content model based on `moduleId`?

## AnQiCMS Content Model Deep Guide: How to Use `archiveList` to Precisely Filter Document Lists As a veteran in website operation for many years, I deeply understand the importance of a flexible and efficient content management system for a corporate website.AnQiCMS with its efficient architecture based on the Go language and highly customizable features, provides us with strong support in the content management field.

2025-11-07

Does the `moduleDetail` tag provide fields to retrieve the model creation time or update time to achieve more refined template display?

As an experienced website operations expert, I fully understand that the precise control and display of time dimension information in content management systems is crucial for website operations and user experience.Whether it is the publication time of the article or the last update time of the content, it can convey important information to the reader and also have a positive impact on search engine optimization (SEO).

2025-11-07

Why must the `model table name` of the content model be in English lowercase letters? What is the relationship with the underlying Go language development of Anqi CMS?

AnQi CMS, as an enterprise-level content management system meticulously crafted based on the Go language, has always adhered to a rigorous design philosophy on the path of pursuing ultimate performance, stability, reliability, and flexibility.Today, let's delve into a seemingly simple yet deeply technical consideration: why must the table name of the content model be in lowercase English letters?How closely is this related to the underlying development of the Go language?### The foundation of AnQi CMS: Go language and efficient design Firstly

2025-11-07

How to get the default category ID of the current content model through the `moduleDetail` tag, so as to build category navigation in the template?

HelloAs an experienced website operation expert, I am more than happy to share with you how to use the `moduleDetail` tag巧妙地 in AnQiCMS templates to obtain the ID of the current content model, and build a dynamic and accurate classification navigation system based on this.The AnQi CMS, with its efficient and flexible content model, provides great convenience for content operators, and a deep understanding of its template tags is the key to unleashing these advantages.

2025-11-07

If I want to restrict certain user groups to only edit specific content models, how can I configure this in the backend, and what is the association with the `moduleDetail` tag?

As an experienced website operations expert, I deeply understand the importance of fine-grained permission management in content operations.An excellent content management system that not only supports a variety of content formats but also provides flexible permission control, ensuring that each team member can work efficiently within their scope of responsibilities while avoiding misoperations and potential security risks.AnQiCMS (AnQiCMS) performs well in this regard, with its powerful user group management and flexible content model mechanism, which is the key to achieving this goal.

2025-11-07