In content operation, we often encounter such situations: the standard 'article' or 'product' content types cannot fully meet the diverse information display needs of the website.For example, an event page may require "registration deadline" and "event locationThis is when the flexible content model customization function of AnQiCMS (AnQiCMS) becomes particularly important, as it can help us easily build data structures that conform to specific business logic and present them individually on the website front-end.

The importance of understanding content models

In the AnQi CMS, the content model can be considered as the 'skeleton' of the website content, defining which fields each content type (such as articles, products, activities, recruitment, etc.) contains and what data types these fields have.System defaults provide the "Article Model" and "Product Model", but the real strength lies in the fact that we can create or modify these models according to actual business needs to accurately manage and display any type of data.This means that your website is no longer limited by predefined frameworks, but can be built completely according to your imagination.

Step 1: Create or modify the content model

To start customizing, you first need to enter the backend management interface of Anqi CMS.Find the "Content Management" menu in the left navigation bar and then click "Content Model".Here is a list of all existing content models, including built-in and custom ones.

If you want to create a completely new content type, such as "Job Posting" or "Course InformationIf you want to add a field to an existing model (such as an article or product), click the "Edit" button on the right of the corresponding model.

In the model editing interface, you will see several core configuration items:

  • Model name: This is the Chinese name displayed on the front and back ends of the model, for example, "Recruitment Position
  • Model table nameThis is the table name stored in the database for the model data, make sure to use lowercase English letters, and ensure that it is unique among all models. It is also an important identifier when calling data in the template.
  • URL alias:Used for pseudorule in URL path, representing the model, and it is recommended to use English lowercase letters.
  • Title nameThis will be the prompt text for the main title field when you publish the model content, making it convenient for content editors to understand.

Step 2: Define custom fields to enrich the data structure

The next step in implementing personalized display is to add custom fields to your content model.At the bottom of the model editing page, there is an "Content Model Custom Field" area.Here, you can add various types of fields according to your needs to store specific data.

Click 'Add field', you need to configure the following items:

  • Parameter nameThis is the Chinese name displayed in the background editing interface for this field, for example 'Position Salary', 'Work Location', 'Event Date'.
  • Call fieldThis is the English name stored in the database for this field, and it is also an important identifier used in the template to retrieve data. It is recommended to use camelCase or underscore naming conventions, such asjobSalary/workLocation/activityDateThis name must be unique and comply with programming specifications.
  • field typeThe CMS provides various field types to adapt to different data formats:
    • Single Line Text:适用于简短的文字信息,如“产品型号”、“作者姓名”。
    • Number:仅限输入数字,如“库存数量”、“商品价格”。
    • Multi-line text:适用于较长的文本描述,如“职位描述”、“活动详情”。
    • 单项选择、多项选择、下拉选择These three types can have multiple options preset, allowing content editors to choose from a list. It is commonly used for 'House Type', 'Gender', 'Product Color', etc.You need to enter one option per line in the "Default Value".
  • Is required:Determines whether the content editor must fill in this field when publishing.
  • Default value:Sets a default value for the field. For selection fields, this is where all options are defined.

Through the reasonable definition of these custom fields, you can build precise data structures for different types of content, thereby laying a solid foundation for personalized display on the front end.

Third step: Publish content, fill in custom data

After you have completed the definition of the content model and saved it, you can start publishing the corresponding type of content.Select your custom model under "Content Management" and click "Add Document" (or any corresponding button for your model name).

In the content publishing interface, in addition to the default fields such as title, content, and category, you will find a new 'Other Parameters' area defined in your model.All custom fields will be displayed here with the "parameter name" you set as a prompt, waiting for you to enter specific data.

Please note that each document must select a category, which belongs to a content model.Only by selecting the corresponding category, can the content model's unique custom fields be correctly displayed and filled in.

Fourth step: Implement personalized display on the front-end page (template customization)

The data structure has been defined and the content has been filled in. What follows is the most anticipated part: how to make these custom data shine on the front page.The Anqi CMS adopts syntax similar to Django template engine, making template customization intuitive and powerful.

  1. Custom data for accessing a single document

    When you are on the document detail page, you can use it directlyarchiveto access the custom fields you have. For example, if you have a field namedjobSalaryyou can use it directly in the template.{{archive.JobSalary}}Show its content. Note that the field name is typically capitalized at the beginning in the template.

    If you need to loop through all custom fields or the field name is uncertain, you can usearchiveParamsTags:

    {% archiveParams params %}
        <div>
            {% for item in params %}
            <div>
                <span>{{item.Name}}:</span>
                <span>{{item.Value}}</span>
            </div>
            {% endfor %}
        </div>
    {% endarchiveParams %}
    

    If the custom field you are storing is rich text (which may contain HTML), please make sure to add|safeFilter to prevent HTML from being escaped, or if the content is in Markdown format, you can use|renderFilter to render as HTML:

    <div>职位描述:{{archive.JobDescription|safe}}</div>
    {# 如果是Markdown内容 #}
    <div>技术要求:{{archive.TechRequirements|render|safe}}</div>
    
  2. Custom data in the access list

    In the list page (such as recruitment list, course list), you can usearchiveListtags to get the document list. Inforto iteratearchivesor when you define variables, eachitemrepresents a document, and you can also useitem.自定义字段名Visit its custom data:

    {% archiveList archives with moduleId="您模型对应的ID" type="page" limit="10" %}
        {% for item in archives %}
        <div>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>职位薪资:{{item.JobSalary}}</p>
            <p>工作地点:{{item.WorkLocation}}</p>
            {# 其他自定义字段... #}
        </div>
        {% endfor %}
    {% endarchiveList %}
    
  3. Filter content based on custom fields

    A more advanced usage is to allow visitors to filter content based on custom fields. Anqi CMS providesarchiveFiltersLabel to achieve this goal. For example, in the job list, users can filter for 'full-time' or 'part-time' positions.

    Firstly, you need to ensure that these custom fields are searchable in the content model field configuration. Then, use them in the template like this:

    <div>
        <h3>职位筛选:</h3>
        {% archiveFilters filters with moduleId="您模型对应的ID" allText="不限" %}
            {% for item in filters %}
            <p>{{item.Name}}:</p>
            <ul>
                {% for val in item.Items %}
                <li class="{% if val.IsCurrent %}active{% endif %}"><a href="{{val.Link}}">{{val.Label}}</a></li>
                {% endfor %}
            </ul>
            {% endfor %}
        {% endarchiveFilters %}
    </div>
    

    This code will generate a set of filter options, and the page will automatically filter the content based on the selected custom field value when the user clicks.

  4. The correspondence between the model and the template

    auto: The security CMS supports specifying different template files for different content models. By default, the system will attempt to locate{模型表名}/detail.htmlauto: As a detail page template, as well as{模型表名}/list.htmlauto: As a list page template. For example,