As an experienced website operations expert, I know that a flexible and versatile content management system is the cornerstone of website success.AnQiCMS (AnQiCMS) is an excellent tool with its flexibility and powerful customization capabilities, and it has become a rare asset in our hands.guestbook)Form, intelligently renders different input controls, such as text boxes, radio buttons, or checkboxes, based on the field type preset by the background.

This is not just the implementation at the technical level, but also about user experience and the efficiency of data collection.An adaptable form that can flexibly adjust to business needs, not only making it easier for users to fill out, but also helping us collect more accurate and standardized data, laying a solid foundation for subsequent marketing analysis and customer service.

interpretationguestbookLabel: The cornerstone of form customization.

In the AnQi CMS, the construction of the comment form is inseparable from its built-inguestbook

When we use{% guestbook fields %}such a method to call,fieldsThe variable actually carries an array object, which includes the detailed attributes 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 Preferences”, etc., which is presented directly to the user.
  • FieldName: This is the variable name used for backend data processing of the form field, corresponding to the attribute in HTML.nameIt is the key to data submission and recognition.
  • TypeThis is the core topic we are discussing today, which defines how form fields should be displayed in HTML controls, for exampletext(text),number(Number),textarea(Multi-line text),radio(Single choice),checkbox(Multiple choice) orselect(Dropdown selection).
  • Required: A boolean value indicating whether the field is required.
  • Content: For text, numeric, or multi-line text fields, this is typically its default value or placeholder.
  • Items: For single-choice, multiple-choice, and dropdown selection fields, this is a very important array that contains all the available items, each of which is an independent option value.

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

Core implementation: Based onTypeAttribute intelligent rendering control input

To implement based onTypeProperties conditionally render different input controls, we mainly rely on the Anqi CMS template engine.forloop andif/elif/elseConditional judgment tag.

Firstly, we useforto iterateguestbookTag returnsfieldseach element in the arrayitem(Custom field). Inside the loop, we use the property to determine the type of the current field and render the corresponding HTML form control accordingly.item.TypeThe property to determine the type of the current field, and render the corresponding HTML form control based on the type.

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 universal text input box (text,number):For these simple text input types, we can directly utilizeitem.Typeas HTMLinputTagstypeProperty value. Also, don't forget to set.nameproperties (item.FieldName),placeholder(item.Content) as well asrequiredproperties.

    {% 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. Render a multi-line text box (textarea): textareaControl withinputSimilar, but with a different structure.

    {% elif item.Type == "textarea" %}
    <textarea name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" rows="5"></textarea>
    
  4. Render a radio button (radio):The radio button needs to be traversed.item.ItemsAn array to generate each option. Each option'snameproperty is the same, ensuring they belong to the same radio group.valueproperty is set toval,titleproperty is used to display the 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 %}
    

    English hint: here auto was usedloop.indexto generate uniqueid, making it convenientlabelto associate, and addedcheckedlogic to support default values.

  5. Render a checkbox (checkbox):A checkbox also needs to be traverseditem.Items. Unlike a radio button, the checkboxnameproperties usually need to be prefixed with[]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.ContentIn a multiple choice scenario, it may be a string containing multiple default values, so it needs to be judgedval in item.Content.

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

    {% 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 is added)selectedlogic to support default values.

Comprehensive example code: Build a complete dynamic message form

Integrate the above logic with the basic form structure (includinguser_name,contact,contentThese common message fields), a fully functional message form that can dynamically change according to backend configuration is about to emerge:

`twig

{# Typically, the fields of a message form, which can be hardcoded or obtained from other tags according to 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>

{# English 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