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

Calendar 👁️ 56

As an experienced website operation expert, I know that a flexible and variable content management system is the foundation of a successful website.AnQiCMS (AnQiCMS) with its excellent flexibility and powerful customization capabilities has become an indispensable tool in our hands.Today, let's delve deeply into a very practical topic in actual operation: how to leave messages in Anqi CMS (guestbookIn the form, it intelligently renders different input controls based on the field type preset on the backend, such as text boxes, radio buttons, or checkboxes.

This is not just a technical implementation, it also concerns user experience and the efficiency of data collection.A form that can be flexibly adjusted to meet business needs, not only makes it easier for users to fill out, but also helps us collect more accurate and standardized data, laying a solid foundation for subsequent marketing analysis and customer service.

InterpretguestbookLabel: The cornerstone of form customization.

In AnQi CMS, the construction of the comment form cannot do without its built-inguestbookThe tag's strength lies in the fact that it does not simply generate a fixed form, but can dynamically read the custom field information you configure in the background 'website message' feature.

When we use{% guestbook fields %}This way to call,fieldsThe variable actually carries an array object that contains the detailed properties of each custom field you define for the comment form. Each field (i.e.),item) has the following key information:

  • Name: This is the display name of the form field, such as "Your name", "Your preference", etc., which is displayed directly to the user.
  • FieldName: This is the variable name used for form field processing on the backend, corresponding to the attribute in HTMLnameAttribute, it is the key to data submission and identification.
  • Type: This is the core of what we discussed today, defining how form fields should be displayed in HTML controls, for exampletext(Text),number(Number),textarea(Multi-line text),radio(Single selection),checkbox(Select multiple) orselect(Dropdown select).
  • Required: A boolean value indicating whether the field is required.
  • ContentFor text, numeric, or multiline text fields, this is typically the default value or placeholder.
  • Items: For single-choice, multiple-choice, and dropdown fields, this is a very important array that contains all the optional items, each of which is an independent option value.

UnderstoodfieldsThe structure of the array, we can start building dynamic rendering logic.

Core implementation: based onTypeAttribute intelligent rendering input control

To implement based onTypeRendering different input controls based on attribute conditions, we mainly rely on the Anqi CMS template engine'sforloop andif/elif/elsecondition judgment tags.

First, we useforLoop throughguestbookthe tags returned byfieldsarrayitem(custom fields). Inside the loop, we useitem.TypeAn attribute to determine the type of the current field and render the corresponding HTML form control.

Let's break down this process step by step:

  1. Traverse all custom fields:

    {% guestbook fields %}
        {% for item in fields %}
            {# 在这里根据 item.Type 渲染不同控件 #}
        {% endfor %}
    {% endguestbook %}
    
  2. Render a universal text input box (text,number):For these simple text input types, we can directly utilizeitem.Typeas HTMLinputlabel'stypeattribute values. Also, don't forget to setnameproperties(item.FieldName)、placeholder(item.Content) andrequiredProperty.

    {% if item.Type == "text" or item.Type == "number" %}
    <input type="{{item.Type}}" name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" autocomplete="off">
    {% endif %}
    
  3. to render a multiline text box (textarea): textareacontrol andinputSimilar, but the structure is different.

    {% elif item.Type == "textarea" %}
    <textarea name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" rows="5"></textarea>
    
  4. Render radio button (radio):The radio button needs to traverseitem.ItemsThe array to generate each option. Each option'snameproperties are the same, ensuring that they belong to the same radio group.valueset the attribute toval,titleProperties are used to display option text.

    {% elif item.Type == "radio" %}
        {%- for val in item.Items %}
        <input type="{{item.Type}}" name="{{item.FieldName}}" value="{{val}}" id="{{item.FieldName}}-{{loop.index}}" {% if val == item.Content %}checked{% endif %}>
        <label for="{{item.FieldName}}-{{loop.index}}">{{val}}</label>
        {%- endfor %}
    

    (Hint: This is used)loop.indexto generate uniqueid, convenientlabelto associate and addcheckedLogic to support default values.)

  5. Render checkbox (checkbox):The checkbox also needs to be traverseditem.Items. Unlike the radio button, the properties of the checkbox usually need to be addedname属性通常需要加上[]suffix so that the backend can receive an array of values (for examplename="interests[]")

    {% elif item.Type == "checkbox" %}
        {%- for val in item.Items %}
        <input type="{{item.Type}}" name="{{item.FieldName}}[]" value="{{val}}" id="{{item.FieldName}}-{{loop.index}}" {% if val in item.Content %}checked{% endif %}>
        <label for="{{item.FieldName}}-{{loop.index}}">{{val}}</label>
        {%- endfor %}
    

    (Hint:item.ContentIt may be a string containing multiple default values in a multi-select scenario, so it needs to be judgedval in item.Content.)

  6. Render dropdown selection box (select):The dropdown selection box also needs to be traverseditem.Itemsto generate<option>.

    {% elif item.Type == "select" %}
    <select name="{{item.FieldName}}" {% if item.Required %}required{% endif %}>
        {%- for val in item.Items %}
        <option value="{{val}}" {% if val == item.Content %}selected{% endif %}>{{val}}</option>
        {%- endfor %}
    </select>
    

    (Hint: Here you added)selectedLogic to support default values.)

Comprehensive example code: Build a complete dynamic message form

Integrate the above logic, combining the basic form structure (includinguser_name,contact,contentThese common comment fields, a fully functional comment form that can dynamically change according to backend configuration has emerged:

`twig

{# Typically, the form fields for comments, which can be hardcoded or obtained from other tags based on your template design #}

<label for="user_name">您的姓名:</label>
<input type="text" name="user_name" id="user_name" required placeholder="请输入您的姓名" autocomplete="off" class="form-control">

<label for="contact">联系方式:</label>
<input type="text" name="contact" id="contact" required placeholder="手机、邮箱或微信" autocomplete="off" class="form-control">

<label for="content">留言内容:</label>
<textarea name="content" id="content" required placeholder="请留下您的宝贵意见或问题" rows="5" class="form-control"></textarea>

{# Dynamic rendering of custom field area #} {% guestbook fields %}

  {% for item in fields %}
  <div class="form-group dynamic-field-{{item.FieldName}}">
      <label for="{{item.FieldName}}">{{item.Name}}:</label>
      <div>
          {% 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}}" autocomplete="off" 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" %}
              {%- for val in item.Items %}
              <label class="radio-inline">
                <input type="{{item.Type}}" name="{{item.FieldName}}" value="{{val}}" id="{{item.FieldName}}-{{loop.index}}" {% if val == item.Content %}checked{% endif %}> {{val}}
              </label>
              {%- endfor %}
          {% elif

Related articles

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 conditionally display the 'Previous' and 'Next' buttons in the `pagination`?

As an experienced website operation expert, I am willing to analyze the conditional display techniques of the pagination navigation buttons in AnQi CMS in detail.AnQi CMS is known for its efficiency and flexibility, its template engine provides powerful functions, allowing us to easily achieve both beautiful and practical page interactions.In website content management, pagination is an indispensable element, and how to intelligently display the "Previous Page" and "Next Page" buttons is directly related to the smoothness of the user's browsing experience.### SecureCMS

2025-11-06

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

As an experienced website operation expert, I deeply know 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 a value, is the key to improving template quality and user experience.

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