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:
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.
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 similar
requiredFields = ["产品编号", "颜色"]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