How to dynamically generate a guestbook form with custom form field types using the `guestbook` tag?

Calendar 👁️ 71

In modern website operation, providing a convenient user communication channel is crucial, and the message board is one of the efficient ways. AnQiCMS (AnQiCMS) understands this need, through its powerful template tag system, especiallyguestbookTags, allowing you to flexibly generate dynamic comment forms on the website, and easily customize form fields to meet various business scenarios.

The foundation of dynamic comment forms:guestbookTag

In Anqi CMS, to display a message form on the front-end page, the core is to useguestbookTemplate tag. This tag is used to retrieve the form field information you have pre-configured on the backend, and then you can dynamically build out the complete form based on this information.

The usage method is very intuitive:

{% guestbook fields %}
    {# 在这里,您可以使用fields变量来构建您的表单 #}
{% endguestbook %}

here,fieldsis composed byguestbookThe variable provided by the label, it includes all the field information of the comments defined in the "Website Comment Management" under the "Function Management" in the Anqi CMS background.Each field is an independent object, carrying detailed properties such as the name, type, and whether it is required.

Custom form fields on the back-end: the source of flexibility

The strength of AnQi CMS lies in the fact that you do not need to modify the code to add various custom fields to the message board in the backend management interface. These fields will directly affectguestbookTags that can be dynamically generated on the front-end form elements.

In the "Function Management" "Website Message Management", you can add to the message form:

  • Single-line text (text):Applicable for short inputs such as names, email addresses, and phone numbers.
  • Number (number):Ensure that users can only enter numbers, such as quantities and ages.
  • Multi-line text (textarea):Applicable to situations where longer text input is required, such as leaving messages, detailed descriptions, etc.
  • Single choice (radio):Provide a set of options that the user can choose from, such as gender, intended product, etc.
  • Multiple selection (checkbox)Provide a set of options, users can select multiple, such as 'service needs', 'hobbies', etc.
  • Dropdown selection (select): Display a group of options in the form of a dropdown menu, suitable for lists such as countries, provinces, etc., where the user can select a single option.

When configuring these fields, you can specify the display names for them (Name), this is the label seen by the user on the front end; set the calling field(FieldName), this is the field name received by the backend when the form is submitted; select the field type(TypeThis determines what type of HTML input element is generated on the frontend; as well as setting whether it is required or notRequired) and default value or option listContent/Items)

Build the form dynamically in the template

Get itfieldsAfter the variable, you can use aforloop to traverse all fields and generate the corresponding HTML form elements according to each fieldTypeproperty.

This is a typical template code snippet for a dynamically generated form:

<form method="post" action="/guestbook.html">
    {% guestbook fields %}
        {% for item in fields %}
        <div class="form-group">
            <label for="{{ item.FieldName }}">{{ item.Name }}{% if item.Required %}<span class="text-danger">*</span>{% endif %}</label>
            {% if item.Type == "text" or item.Type == "number" %}
                <input type="{{ item.Type }}" name="{{ item.FieldName }}" id="{{ item.FieldName }}"
                       {% if item.Required %}required{% endif %} placeholder="{{ item.Content }}" class="form-control">
            {% elif item.Type == "textarea" %}
                <textarea name="{{ item.FieldName }}" id="{{ item.FieldName }}"
                          {% if item.Required %}required{% endif %} placeholder="{{ item.Content }}" rows="5" class="form-control"></textarea>
            {% elif item.Type == "radio" %}
                <div class="form-check-group">
                {% for val in item.Items %}
                    <div class="form-check form-check-inline">
                        <input type="radio" name="{{ item.FieldName }}" id="{{ item.FieldName }}_{{ forloop.Counter }}" value="{{ val }}" class="form-check-input"
                               {% if forloop.Counter == 1 %}checked{% endif %} {% if item.Required %}required{% endif %}>
                        <label class="form-check-label" for="{{ item.FieldName }}_{{ forloop.Counter }}">{{ val }}</label>
                    </div>
                {% endfor %}
                </div>
            {% elif item.Type == "checkbox" %}
                <div class="form-check-group">
                {% for val in item.Items %}
                    <div class="form-check form-check-inline">
                        <input type="checkbox" name="{{ item.FieldName }}[]" id="{{ item.FieldName }}_{{ forloop.Counter }}" value="{{ val }}" class="form-check-input"
                               {% if forloop.Counter == 1 %}checked{% endif %} {% if item.Required %}required{% endif %}>
                        <label class="form-check-label" for="{{ item.FieldName }}_{{ forloop.Counter }}">{{ val }}</label>
                    </div>
                {% endfor %}
                </div>
            {% elif item.Type == "select" %}
                <select name="{{ item.FieldName }}" id="{{ item.FieldName }}" class="form-control" {% if item.Required %}required{% endif %}>
                    {% for val in item.Items %}
                        <option value="{{ val }}">{{ val }}</option>
                    {% endfor %}
                </select>
            {% endif %}
        </div>
        {% endfor %}

        <div class="form-group text-center mt-4">
            <button type="submit" class="btn btn-primary">提交留言</button>
            <button type="reset" class="btn btn-secondary ml-2">重置</button>
        </div>
    {% endguestbook %}
</form>

In this code block,item.NameUsed to display the label for form fields,item.FieldNameUsed as an input box,nameProperty,item.TypeGenerate different HTML input elements based on their value ("text", "number", "textarea", "radio", "checkbox", "select")item.RequiredThe property will be added as a required field.requiredFor radio, checkbox, and dropdown selections,item.Itemsit will be traversed to generate each option.

Message form submission and security

The submission address of the message form is fixed to/guestbook.htmlThe submission method isPOST. Anqi CMS will automatically process the submitted data based on the fields you configure in the background. In addition to custom fields, the system will automatically receiveuser_name(Username),contact(Contact information) andcontentthese core fields of (留言内容).

To enhance the security of the website and prevent spam comments, it is strongly recommended that you enable the captcha function.In the AnQi CMS backend, under "Backend Settings" -> "Content Settings", you can enable comment captcha.After enabling, you need to add the following code to your comment form to dynamically retrieve and display

Related articles

How to display article comments using the `commentList` tag and integrate reply and like functions?

When building a vibrant website, the article comment feature is undoubtedly a core element to enhance user interaction and promote the development of a content community.AnQiCMS as an efficient and flexible content management system, provides us with powerful template tags, making it simple and intuitive to integrate and display comments.Today, let's delve into how the `commentList` tag in AnQiCMS helps us display article comments and cleverly integrate reply and like functions.

2025-11-07

How to build a complex document parameter filtering interface using the `archiveFilters` tag, such as real estate, product filtering?

In website operation, providing users with an efficient and accurate content filtering function is the key to improving user experience and content discoverability.Whether it is complex real estate information, massive product SKUs, or professional industry documents, a well-designed filtering interface can allow users to quickly locate the information they need.In Anqi CMS, the `archiveFilters` tag is a powerful tool for building such complex filtering interfaces.### Understand the core of the filter interface: content model and custom parameters Build a flexible filtering function

2025-11-07

How to implement the switch of multilingual sites and the setting of default language package in AnQiCMS?

A deep analysis of AnQiCMS multi-language site switching and default language package settings In today's global internet environment, website support for multiple languages has become a key factor in expanding the market and improving user experience.AnQiCMS as a system dedicated to providing an efficient and customizable content management solution also offers flexible and powerful multilingual support.This article will elaborate on how to implement multi-language site switching in AnQiCMS, as well as the setup of the default language package.

2025-11-07

How to add custom parameters to meet the specific information display requirements in the contact information settings?

During the process of building and operating a website, displaying contact information is often a crucial link.It is not only a bridge for visitors to communicate with you, but also a key element for building trust and improving conversion rates.AnQiCMS (AnQiCMS) understands this need, providing basic contact information settings while also giving users great flexibility, allowing you to customize and display various contact information according to your unique business needs.This article will guide you to a deep understanding of how to set up custom contact information parameters in Anqi CMS to meet the specific information display needs of your website template

2025-11-07

How to effectively use `if` and `for` tags for conditional judgment and data looping in AnQiCMS templates?

In AnQiCMS template development, `if` and `for` tags are undoubtedly the core tools for building dynamic content and flexible layouts.They allow us to display content based on specific conditions or efficiently loop through data, thereby transforming static templates into rich, user-responsive pages.AnQiCMS uses syntax similar to the Django template engine, making these operations intuitive and powerful.

2025-11-07

How `list` and `split` filters convert a string to an array and how to process it in a template?

In the powerful template system of Anqi CMS, flexible data processing is the key to building dynamic websites.At times, the data we retrieve from the backend, such as tags, keywords, or custom field values, may be stored as comma-separated strings, but we want to treat them as individual items in the frontend template.At this point, the `list` and `split` filters provided by Anq CMS are particularly important, as they help us convert strings into arrays, thereby enabling more refined control and display in templates.Why do we need to convert a string to an array

2025-11-07

How does the `slice` filter accurately extract the specified part of a string or array for display?

In the daily content management of Anqi CMS, we often need to accurately trim the text or data list displayed on the website to better adapt to different layouts, provide content previews, or optimize the user reading experience.At this point, the `slice` filter has become a very practical tool, which can help us flexibly extract the specified part of a string or array.### Core Function: Basic Usage of `slice` Filter The `slice` filter is like a tailor, capable of cutting according to the "scissors" position you provide

2025-11-07

How do the `truncatechars` and `truncatewords` filters control the truncation display of long text and add an ellipsis?

In website content operation, we often encounter such situations: In order to maintain the neatness and consistency of the page layout, we need to truncate the long text, such as in article lists or product summaries.If cut off simply and brutally, it may not only result in incomplete meaning of the text, but may also destroy the text structure containing HTML tags, affecting the aesthetics and functionality of the page.AnQi CMS, with its flexible template engine, provides us with an elegant solution to this problem.Through built-in text filters, we can easily control the display length of long texts and add ellipses at appropriate positions

2025-11-07