In website operation, collecting user feedback and inquiries is an important link to improve service quality and understand user needs.AnQiCMS provides a flexible and powerful message management function, allowing you to easily display the message form in the frontend template and support custom fields to meet the information collection needs of different business scenarios.
To implement dynamic display of user submitted comment forms and custom fields in AnQiCMS templates, we mainly use a core tag: guestbook. This tag can fetch all the comment field information you configure in the background, and then render it to the front-end page through a loop to build a completely dynamic comment form.
UnderstandguestbookTag
guestbookThe label is designed specifically for the留言表单in Anqi CMS, its basic usage is very intuitive:
{% guestbook fields %}
{# 在这里循环渲染表单字段 #}
{% endguestbook %}
When you use{% guestbook fields %}It will return a namedfieldsto the template. ThisfieldsThe variable is an array that contains all the form field information you have configured under "Function Management" in the Anqi CMS backend, including default system fields and custom fields you have created.
Moreover, if you are managing multiple sites and want to call form field of other sites' comment forms in the current site's template, you can do so bysiteIdparameter to specify the site ID, for example{% guestbook fields with siteId="2" %}.
Dynamically render the message form field
fieldsEach element in the variable represents a message form field. In the loop traversalfieldsAn array, we can get detailed properties of each field and dynamically generate corresponding HTML form elements.
Each field object (usually named in loops) will contain the following key properties:item) will include the following key attributes:
item.Name: The display name of the field, such as 'Your Name', 'Phone Number', etc.item.FieldName: The internal identifier of the field, used as the HTML form element'snameproperties, such asuser_name/contactThe camel case naming form of the field you customize.item.TypeThe field type determines which type of HTML form element should be rendered. Common types include:textSingle-line text input box (<input type="text">)numberNumber input box (<input type="number">)textarea: Text box (multi-line)<textarea>)radio: Radio button group (<input type="radio">)checkbox: Checkbox group (<input type="checkbox">)select: Dropdown selection box (<select>)
item.Required: Boolean value indicating whether this field is required. If it istrueWe can add in the HTML elementrequiredProperty.item.Content: Default value or placeholder text for the field.item.Items: When the field type isradio/checkboxorselectAt this time, this property will be an array containing all the option values available. We need to loop through thisitem.Itemsarray to generate options.
common template code for building a message form
With this field information, we can build a generic template code that can automatically adapt to changes in the backend configuration without manually modifying the frontend HTML.
The following is a detailed example showing how to dynamically render a message form in a template, and it includes common text, numbers, multi-line text, radio buttons, checkboxes, dropdown selections, and captcha functions:
`twig