As an experienced website operations expert, I am well aware of the importance of a flexible and efficient content management system in the increasingly complex network environment.AnQiCMS (AnQiCMS) boasts its high-performance architecture based on the Go language and rich features, displaying unique advantages in the content management field.Today, let's delve into a common issue encountered in website operations: custom fields in message forms, such as the 'City' option we often use to collect user information, can it dynamically read data from the database?
The core capability of the Anqi CMS comment form custom field
Firstly, it is clear that Anqi CMS provides strong support for customization of the comment form.Added online message feature and supports custom message fields.This means that the operator can add various personalized information collection items to the message form based on actual business needs, not limited to basic fields such as 'name', 'email', and so on.For example, adding 'city', 'industry', 'products of interest', etc., greatly enhances the practicality of the form.
Custom field type and option configuration
The self-designed form custom field of AnQi CMS is quite flexible, supporting various field types to meet different data collection needs:
- Single-line text (text)andNumber (number)This field is used to collect short user input information, such as phone numbers, postal codes, etc.
- Multi-line text (textarea)This is suitable for collecting longer user feedback or suggestions.
- Single choice (radio)/Multiple selection (checkbox)andDropdown selection (select)These three types are the focus of this discussion, allowing users to choose from preset options.
When talking about the 'City' option, we tend to use 'drop-down selection' or 'single selection'.What is the data source for these options?According to the document description, these options are defined by the 'default value' set in the background.ItemsArray, for front-end template iteration output.
The interpretation of 'dynamic reading': The implementation mechanism of Anqi CMS
Now, let's positively answer the core question: Can the 'City' option be dynamically read from the database?
In a strict sense, the default values for the custom field options of the Anqi CMS message form (such as the "City" list) are configured and saved in the back-end management interface. This configuration data includes the field names, types, and preset option lists, as well asStored in the database of Anqicms itself.
When the front-end page of the website loads a page containing a message form, Anqicms template engine will go throughguestbookTags such as{% guestbook fields %}...{% endguestbook %})toQuery and read the configuration information of the saved comment form custom field in the databaseThen, it will generate the corresponding HTML form elements dynamically according to these configurations, and fill in the preset options (i.e., the array mentioned earlier).Itemsarray).
So, from this perspective, it is indeed 'reading data dynamically from the database', because the structure of the form and the content of the options are not hardcoded in the template file, but are rendered in real-time based on the configuration in the background database.The operator modified the "City" option in the background, and the front-end page will refresh immediately, which is the "dynamic" manifestation.
However, the 'dynamic reading' here is different from what some developers may expect, such as 'executing a custom database query when the form is loaded (for example, fetching all city lists in real-time from an independent city database table) and filling the query results into the form options.' The Anqi CMS built-inguestbookThe label options data comes from the manually input 'default value' list by the backend administrator, rather than directly supporting any SQL query to fill in the options.
Practical scenario: How to configure and display the 'City' option
Assuming you add a custom field named "City" for the message form in the AnQi CMS backend, set its type to "dropdown selection", and input cities such as "Beijing", "Shanghai", "Guangzhou", "Shenzhen", etc. in the "default value" field one by one.
In the front-end template file (usuallyguestbook/index.html), you can output this "City" dropdown by usingguestbooktags and theirforloop:
<form method="post" action="/guestbook.html">
{% guestbook fields %}
{% for item in fields %}
{% if item.FieldName == "city" %} {# 假设你在后台设置的字段名为 city #}
<div>
<label>{{item.Name}}</label>
<div>
<select name="{{item.FieldName}}" {% if item.Required %}required{% endif %}>
{% for val in item.Items %}
<option value="{{val}}">{{val}}</option>
{% endfor %}
</select>
</div>
</div>
{% else %}
{# 处理其他自定义字段,此处省略 #}
<div>
<label>{{item.Name}}</label>
<div>
{% if item.Type == "text" || item.Type == "number" %}
<input type="{{item.Type}}" name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" autocomplete="off">
{% elif item.Type == "textarea" %}
<textarea name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" rows="5"></textarea>
{# 其他类型字段的渲染逻辑 #}
{% endif %}
</div>
</div>
{% endif %}
{% endfor %}
<div>
<button type="submit">提交留言</button>
</div>
{% endguestbook %}
</form>
In the above code,item.ItemsThe array is the city list parsed from the default value configured in the background.
If you need a more advanced “dynamic”: explore expansion possibilities
If the operator has a more complex need for “dynamic reading”, for example:
- Retrieve city lists from external APIsFor example, calling a third-party geospatial service interface.
- Real-time data from other content models of AnQi CMS as optionsFor example, all regions in a certain 'region management' content model are options.
- The city list has a huge amount of data, which is not suitable for manual maintenanceIt requires batch import or automated update.
In these cases, the built-in message form tag of Anqicms may not be able to meet the needs directly. However, as a highly customizable and extensible Go language CMS, Anqicms provides a variety of possibilities for solutions:
- secondary developmentUtilizing the powerful capabilities of Go language, develop customized logic for the message module to be able to call external APIs or perform complex database queries to fill options.
- Custom template and JavaScriptIn the front-end template, use JavaScript code to make AJAX requests to the custom API provided by Anqicms (if there is one or