How to use the `guestbook` tag to build and display a dynamic guestbook form on the front page?

Calendar 👁️ 65

In AnQiCMS, building a fully functional message form is an important way to interact effectively with users, collect feedback, or obtain potential customer information. It is pleasing to see that, with its powerful template tag system, especiallyguestbookLabel, we can easily create and display a dynamic, flexible comment form on the front-end page without writing complex backend code.

The core of building a flexible comment form:guestbookTag

guestbookThe tag is a powerful tool designed specifically for the online message function of AnQiCMS.It no longer requires us to manually define each field of the form, but acts like an intelligent assistant, directly reading all the field information from the pre-set form configuration in the background, and then automatically rendering it to the front-end page.This means that any structural adjustments you make to the comment form in the background (such as adding, deleting, or modifying field types) can be reflected in real-time and dynamically on the front-end, greatly improving development and operation efficiency.

In most cases, you will use this tag in the template directoryguestbook.htmlin the file, it is a default template file specifically used to display the online message page

UseguestbookThe basic syntax of the tag is very simple:

{% guestbook fields %}
    {# 在这里循环输出表单字段 #}
{% endguestbook %}

Here, fieldsIt is a custom variable name, which carries all the background configuration data of the message form fields.{% guestbook fields %}and{% endguestbook %}Between them, we can use a loop.fieldsVariables to dynamically generate the various input items of the form.

Understand the composition of form fields in depth.

fieldsA variable is an array that contains multiple form field objects, each representing a form item you have set up in the background. During the loop, we can access it byitem(or any variable name) to access the detailed properties of each field:

  • item.NameThis is the display name of the form field, which is the label text that users see on the form, such as "Your Name", "Contact Information".
  • item.FieldNameThis is the system field name used for data binding when submitting forms, for exampleuser_name/contact.
  • item.TypeIt defines the type of form input box, AnQiCMS supports various types includingtext(Single-line text),number(Number),textarea(Multi-line text),radio(Single selection),checkbox(Multiple choice) andselect(Dropdown select).
  • item.Required: A boolean value indicating whether the field is required. If it istrue, you can add corresponding validation hints on the front end.
  • item.Content: Usually used as a text input box'splaceholder(Placeholder) text, or as the default value of the field.
  • item.Items:Whenitem.TypeWithradio/checkboxorselectthen,ItemsIt will be an array containing all the available option values.

Mastered these properties, we can build a dynamic form that can adapt to changes in background configuration.

Gradually build the dynamic message form code.

Here is a complete example code snippet that shows how to useguestbooktag to iterate over all fields defined in the background and dynamically generate HTML form elements based on field types

<form method="post" action="/guestbook.html">
    {% guestbook fields %}
        {% for item in fields %}
        <div>
            <label for="{{ item.FieldName }}">{{ item.Name }}</label>
            <div>
                {% if item.Type == "text" or item.Type == "number" %}
                <input type="{{ item.Type }}" id="{{ item.FieldName }}" name="{{ item.FieldName }}" 
                       {% if item.Required %}required{% endif %} 
                       placeholder="{{ item.Content }}" autocomplete="off">
                {% elif item.Type == "textarea" %}
                <textarea id="{{ item.FieldName }}" name="{{ item.FieldName }}" 
                          {% if item.Required %}required{% endif %} 
                          placeholder="{{ item.Content }}" rows="5"></textarea>
                {% elif item.Type == "radio" %}
                    {%- for val in item.Items %}
                    <input type="{{ item.Type }}" id="{{ item.FieldName }}_{{ loop.index }}" name="{{ item.FieldName }}" value="{{ val }}" 
                           {% if item.Content == val %}checked{% endif %}>
                    <label for="{{ item.FieldName }}_{{ loop.index }}">{{ val }}</label>
                    {%- endfor %}
                {% elif item.Type == "checkbox" %}
                    {%- for val in item.Items %}
                    <input type="{{ item.Type }}" id="{{ item.FieldName }}_{{ loop.index }}" name="{{ item.FieldName }}[]" value="{{ val }}" 
                           {% if item.Content contains val %}checked{% endif %}>
                    <label for="{{ item.FieldName }}_{{ loop.index }}">{{ val }}</label>
                    {%- endfor %}
                {% elif item.Type == "select" %}
                <select id="{{ item.FieldName }}" name="{{ item.FieldName }}">
                    {%- for val in item.Items %}
                    <option value="{{ val }}" {% if item.Content == val %}selected{% endif %}>{{ val }}</option>
                    {%- endfor %}
                </select>
                {% endif %}
            </div>
        </div>
        {% endfor %}

        {# 验证码区域,如果后台开启了验证码,这里需要添加 #}
        {# 具体的验证码集成代码请参考 FAQ 部分或相关文档 #}

        <div>
            <div>
                <button type="submit">提交留言</button>
                <button type="reset">重置</button>
            </div>
        </div>
    {% endguestbook %}
</form>

This code is first wrapped in aformTagged,actionproperty points to/guestbook.html(This is the default path for AnQiCMS to handle message submission),methodWithpost.

InforIn the loop, we useif...elif...elsestructure judgmentitem.Typeto generate different types of form elements:

  • FortextandnumberType, generateinputTags, and settype/name/id/requiredandplaceholderProperty.
  • FortextareaType, generatetextareaTag, set the corresponding properties accordingly.
  • Forradio/checkboxandselectType, will nest another oneforto loop throughitem.ItemsArray, generate for each optioninputoroptionTag. We also added an extra one here.idandlabelTo improve accessibility.

In this way, regardless of how many fields are configured in the background or how the field types change, the front-end page can intelligently generate the corresponding form, realizing true dynamism.

Attention when submitting the form

When the user fills in and submits the form, the data will be sent toactionspecified by the properties/guestbook.htmlpath. AnQiCMS will automatically receive and process these data.

There are some standard fields, even if you do not explicitly set them in the background, it is recommended to include them in the form, or ensure that your custom fields can cover similar functions:

  • user_name: The name of the poster.
  • contact: Contact information of the leaver (such as mobile phone, email, WeChat, etc.).
  • content: The specific content of the留言.

In addition, you can also add a name forreturnThe hidden field is used to specify the format returned by the backend, for examplehtmlorjsonThis is very useful when you need to submit a form asynchronously through AJAX

Related articles

How to retrieve and display all the detailed content of a single page, including the slide group image, using the `pageDetail` tag?

In Anqi CMS, a single page is an indispensable part of the website structure, commonly used to display 'About Us', 'Contact Information', 'Service Introduction', etc., which are relatively fixed and do not require frequent updates.For these pages, we usually want to be able to flexibly control the way content is displayed, including text, images, and even slide group images.The `pageDetail` tag is born for this, it allows us to easily retrieve and display all the detailed content of a single page.###

2025-11-09

How does the `pageList` tag in Anqi CMS display all single page lists and exclude specific pages?

AnQi CMS is a flexible and efficient content management system that provides strong support for our website operations, whether it is publishing articles, managing products, or creating various single-page applications, it shows great proficiency.Today, let's talk about a very practical skill when using Anqi CMS to manage single-page applications: how to display all single pages through the `pageList` tag while also accurately excluding specific pages that we do not want to display.In daily website operations, we often create some single-page pages such as "About Us", "Contact Information", "Privacy Policy" and so on

2025-11-09

How to effectively display the description (Description) and content (Content) on the front page?

In AnQi CMS, the description (Description) and content (Content) are key elements for website content organization and search engine optimization (SEO).Effectively display this information on the front page, not only providing visitors with clear navigation and in-depth information, but also helping search engines better understand the theme of the page, thereby improving the visibility of the website.Let's explore how to flexibly display the description and content of categories in the Anqi CMS front-end template together.

2025-11-09

How to get and display the custom Banner image group of the `categoryDetail` tag?

In website operation, configuring unique visual elements for different category pages, such as customized banner image groups, is crucial for enhancing user experience and strengthening brand recognition.AnQiCMS (AnQiCMS) leverages its flexible content management capabilities, allowing users to easily set up exclusive Banners for each category and provides intuitive template tags for conveniently displaying these contents on the frontend page.We all know that when users browse a website, the category page is a key entry point for them to explore content.

2025-11-09

How to display the friend link list in the website background configuration of Anqi CMS?

In website operation, friendship links are an important link to enhance website weight, increase traffic, and improve user experience.AnQiCMS (AnQiCMS) provides us with a convenient way to manage and display these links.How can the list of friend links configured on the back-end be displayed on the website front-end?This is actually simpler than imagined, mainly involving two steps: backend configuration and frontend template invocation. ### Step 1: Configure the友情链接 in the background Firstly, we need to add and manage the friendship links in the Anq CMS background management system.According to system design

2025-11-09

How to use the `pagination` tag to generate a page navigation that conforms to user habits for articles or product lists?

In website operation, how to allow users to easily browse a large amount of content and find the information they are interested in is a crucial topic.Especially on pages such as article lists and product displays, reasonable pagination navigation not only improves user experience but is also an indispensable part of search engine optimization (SEO).AnQiCMS (AnQiCMS) understands this point and therefore provides a powerful and flexible `pagination` tag to help us easily generate professional and user-friendly pagination navigation for website content.We will delve deeper into how to utilize Anqi CMS

2025-11-09

What are some practical scenarios for using the `if/else` logical judgment tags in AnQi CMS to control content display conditions?

In AnQi CMS template design, the `if/else` logical judgment tag is an extremely powerful tool that endows the website content with the vitality of dynamic display.By flexibly using this tag, we can intelligently control the display and hiding of elements on the page based on different conditions, thereby providing users with more accurate and personalized browsing experiences, and also greatly improving the operational efficiency of the website.Imagine if your website needs to display different content for different scenarios or if certain information is only visible under specific conditions

2025-11-09

How to implement diverse data layout and display with the `for` loop tag in AnQi CMS?

How to present information in a more rich and attractive way during website content operation is the key to improving user experience and site activity level.AnQiCMS (AnQiCMS) provides us with the tool to achieve this goal with its flexible content model and powerful template engine.Among them, the `for` loop iteration tag is the essential core tool for us to perform diverse formatting and display.Imagine your website is not just a static pile of articles, but can intelligently present lists of articles, product galleries, category navigation, user comments, and even custom parameters based on different scenarios

2025-11-09