1. Background preparation: Configure the custom fields of the comment form
Before starting template development, we need to make some basic configurations in the background management system of AnQi CMS. These configurations are crucial for the form to dynamically display custom fields.
- Enter the message management interface: Log in to the Anqi CMS backend, navigate to the "Function Management" menu, find and click the "Website Messages" option.
- Set additional form fieldsIn the “Website Comments” page, you will see a section named “Form Additional Fields Settings”. This is where we define custom fields.
- Add new fieldClick the add button to add a new field to your comment form.
- Parameter name (display name)'} ]This is the field name used in the background and front-end templates to display to users, such as 'Your industry', 'Intended product', etc.
- Call Field (FieldName)This is the unique identifier stored in the database and referenced in the template. It is recommended to use lowercase English letters, for example
your_industry/product_interest. - field typeAnqi CMS provides various field types to meet different needs:
- Single-line text (text): Suitable for short text input, such as name, email.
- Number (number): Only allows input of numbers, such as phone, quantity.
- Multi-line text (textarea): Suitable for long text input, such as detailed description, specific requirements.
- Single choice (radio)Provide multiple options, the user can only select one.
- Multiple choice (checkbox)Provide multiple options, the user can select multiple.
- 下拉选择 (select):提供一个下拉菜单,用户从预设选项中选择一个。
- Is requiredEnglish: According to requirements, mark the fields as required for verification when the user submits.
- [en]Default value/option:For single-line text, numbers, and multi-line text, you can set the default placeholder content here.For selection fields (single choice, multiple choice, dropdown), enter one option per line as the values that users can select.
- Configure the message verification code (optional but recommended)To effectively prevent spam and malicious submissions, it is recommended to enable the comment captcha feature in the "Website Message" settings.This usually includes selecting the captcha type and style.After enabling, the corresponding captcha display and input box also need to be added in the template.
Through these steps, you have defined all the necessary fields and their properties for the comment form.
II. Implementation in the template: dynamically present custom fields
After completing the background configuration, the next step is to edit the front-end template files, so that these custom fields can be dynamically displayed to the user and collect user input.
The leave message form template file of Anqi CMS is usually located under the theme you are currently using.guestbook/index.htmlorguestbook.html(Please refer to the template directory structure agreement for the specific path).
Use
guestbookLabel field data acquisition: In the template file, SafeCMS provides a specialguestbookLabels to retrieve the backend configuration of the comment field. It will pass all fields as an array object to the variable you define.<form method="post" action="/guestbook.html"> {% guestbook fields %} {# 在这里循环遍历fields数组,动态生成表单字段 #} {% endguestbook %} {# 提交按钮 #} <div> <button type="submit">提交留言</button> <button type="reset">重置</button> </div> </form>In the above code,
fieldsThat is the array of all the comment fields you define.Dynamically generated form fields:
fieldseach element in the arrayitemAll represent a custom field, which includes the configurations we made previously in the backgroundName(Display Name),FieldName(Field name),Type(Field type),Required(Required) andContent(Default value/option list). We can useforloop andif/elif/elsecondition judgment to dynamically generate different HTML form elements based on field type.`twig