How to flexibly call and display custom fields in front-end templates?

Calendar 👁️ 77

When using Anqin CMS to manage website content, its powerful content model and custom field functions provide great flexibility for the website's personalized needs.At times, we not only need to publish regular article titles, content, etc., but also need to add some unique information for specific types of articles or products, such as the author of the article, product model, launch date, and special selling points.This information is exactly implemented through content model custom fields.How can these custom fields be flexibly called and displayed in the website front-end template?This is the issue we are to delve into today.

Understanding content models and custom fields

First, let's briefly review the concept of content model and custom fields in Anqi CMS.The content model, as the name implies, is like a 'skeleton' or 'template' custom-made for different types of content.For example, you can create an 'article model' for blog posts, and a 'product model' for displaying product information.Each model includes a series of standard fields (such as title, content, classification, etc.), and Anqi CMS allows us to add "custom fields" for each model.

The types of custom fields are very rich, including single-line text (for short text input, such as author name), numbers (for prices, inventory, etc.), multi-line text (for long descriptions), single selection, multiple selection, and drop-down selection (for selecting preset options).In the background, we can easily configure these custom fields for a content model, setting their names, field names, types, whether they are required, and default values.After these settings are completed, when we publish content under this model, we can see the corresponding custom field input box, making it convenient to enter various personalized data.

Basics of front-end template calls

The front-end template system of AnQi CMS is based on Django template engine syntax, which means it has a powerful and intuitive variable and tag calling mechanism. In the template file, we use double curly braces{{变量}}Output the value of the variable, using single curly braces and the percent sign{% 标签 %}To perform logical control or call specific functions. The value of custom fields is essentially part of the content data as well, and it also follows this set of rules.

All template files are stored in/templateUnder the directory, static resources (CSS, JS, images) are/public/static/. Variable names usually follow camel case (for examplearchive.Title), which is convenient for understanding and use.

Flexibly call the core tags of custom fields

In AnQi CMS, calling custom fields of the content model mainly depends on two core tags:archiveDetailandarchiveParams.

1. UsearchiveDetailTags call specific custom fields

When you know the name of the custom field's 'reference field'archiveDetailThe tag is a direct and efficient calling method. This tag is mainly used to obtain detailed data of the current document or a specified document on the document detail page.

Calling method: {% archiveDetail 变量名称 with name="自定义字段的调用字段名" %}

Among them,变量名称can be omitted, if omitted, the label will directly output the value of the field. If specified变量名称, then the value obtained can be assigned to this variable, and used in the template later by{{变量名称}}.

Example scenario:Assuming we added a named "author" field to the "article model":author)" field on a single line text field, and added a named "product batch number (field call:batch_number)" field on a single line text field."}

  • Call the author on the article detail page:

    <p>作者:{% archiveDetail with name="author" %}</p>
    

    Or assign the value to a variable and use it later:

    {% archiveDetail articleAuthor with name="author" %}
    <p>作者:{{ articleAuthor }}</p>
    
  • Call the product batch number on the product details page:

    <p>产品批次号:{% archiveDetail with name="batch_number" %}</p>
    

Handle special field types:

  • Group image field (for example named)')product_imagesCustom field of the group image):The group field usually returns an array of image URLs. In this case, you need to loop through to display all images:

    {% archiveDetail productImages with name="product_images" %}
    <div class="product-gallery">
        {% for imgUrl in productImages %}
            <img src="{{ imgUrl }}" alt="产品图片" />
        {% endfor %}
    </div>
    
  • Rich text field (such as namedfull_descriptionThe multi-line text field, using Markdown or rich text editor):If the custom field stores HTML content or Markdown content, direct output may display the original tags. In this case, it is necessary to usesafeOr filter.renderFilter to parse HTML or render Markdown:

    <div class="full-description">
        {% archiveDetail productDescription with name="full_description" %}
        {{ productDescription|safe }}  {# 如果是HTML内容 #}
        {# 或者 {{ productDescription|render|safe }} 如果是Markdown内容 #}
    </div>
    

    renderThe filter will convert Markdown content to HTML,safeThe filter ensures that HTML content is displayed directly without being escaped.

2. UsearchiveParamsLoop through all custom fields with tags.

When you are unsure about the custom fields or need to display all custom fields and their values in a uniform way,archiveParamsLabels are very useful. They can get all custom parameters of the current document or a specified document.

Calling method: {% archiveParams 变量名称 with id="文档ID" sorted=true %}

  • 变量名称: Used to receive an array or Map object of all custom fields.
  • idOptional parameter, used to specify the custom field of the document to be retrieved. If omitted, the document of the current page is retrieved by default.
  • sortedOptional parameter, default istrue.
    • sorted=true(Default):变量名称It will be an ordered array containingName(parameter name, that is, the Chinese name) andValue(parameter value). This method is suitable forforto loop through and display.
    • sorted=false:变量名称It will be an unordered Map object, you can use变量名称.调用字段名.Nameand变量名称.调用字段名.ValueDirectly access specific fields.

Example scenario:We hope to display all product parameters on the product details page, and these parameters may be dynamically added or modified on the backend.

  • Display all custom fields in an ordered array:

    <div class="product-parameters">
        <h3>产品参数</h3>
        <ul>
            {% archiveParams params %} {# 默认 sorted=true #}
            {% for item in params %}
                <li>
                    <span>{{ item.Name }}:</span>
                    <span>{{ item.Value }}</span>
                </li>
            {% endfor %}
            {% endarchiveParams %}
        </ul>
    </div>
    

    This method is very suitable for displaying generic parameters, without knowing the specific field names of each field.

  • Access specific custom fields directly in an unordered Map:Assuming we havematerial(Material) andweight(Weight) two custom fields.

    <div class="product-specs">
        {% archiveParams productSpecs with sorted=false %}
        <p>材质:{{ productSpecs.material.Value }}</p>
        <p>重量:{{ productSpecs.weight.Value }}</p>
        {% endarchiveParams %}
    </div>
    

    The advantage of this method is that it can directly obtain the field name by calling the custom field, but it needs to ensure that the called field name of the custom field is known and stable.

Build in accordance with the actual page

When building a website in practice, the flexible invocation of custom fields can greatly enhance the richness of content display.For example, a product page of an e-commerce website can use custom fields to display various product properties: color, size, material, stock, origin, etc.An article page of a content website, which can display the originality statement of the article, the source of citation, the column it belongs to, and so on.

Common steps:

  1. Determine the content type:What types of content are needed, such as news, products, cases, etc.
  2. Create/Edit content model:Create or edit the corresponding content model in the AnQi CMS backend.
  3. Add custom field:According to the needs of the content type, add necessary custom fields to the model and carefully define the names and types of 'invoked fields'.
  4. Design the front-end template: Intemplatecorresponding template file in the directory (for example, article detail page)archive/detail.htmlUse it inarchiveDetailorarchiveParamstags to call and display these custom fields
  5. Content release:When publishing content in the background, fill in data for custom fields.

By following these steps, we can fully utilize the customized field function of Anqi CMS to build highly customized and expressive website content.

Frequently Asked Questions (FAQ)

  1. Why am IarchiveListCannot directly pass in the loopitem.我的自定义字段Call? archiveListTags are mainly used to obtain common basic fields of document lists, such as titles, links, descriptions, etc. To maintain the efficiency of list queries and data brevity, it defaults to not including all custom fields.itemIn the object. If you need to display a custom field on the list page, the recommended practice is toarchiveListWithin a loop, for eachitem, usingarchiveDetailtag (viaid=item.Idparameters to specify the current document) orarchiveParamsLabel to retrieve and display its custom fields. However, if the value of the custom field is only used as a filter condition or simple prompt on the list page, it can also be considered to be set through the background settings, inarchiveListThe query parameters are explicitly specified, but this usually adds complexity to the database query.

  2. I have a custom field that is a checkbox. How can I display all selected values on the front end?Check box type custom fields are usually stored on the backend as strings containing multiple values (such as值1,值2,值3) or arrays. You can use them in the frontend template.splitThe filter splits this string by delimiter (usually comma,) into an array, then goes throughforLoop through this array to display all selected values. For example:

    {% archiveDetail selectedOptions with name="my_multi_select_field" %}
    <ul>
    {% for option in selectedOptions|split:"," %}
        <li>{{ option }}</li>
    {% endfor %}
    </ul>
    
  3. I have customized a rich text editor field, why does it display as text with HTML tags directly?The content stored in a rich text editor is usually with

Related articles

How to retrieve and display a list of related documents based on the current document to enhance content relevance?

How to keep visitors immersed in your site after they have finished reading an exciting article and discover more interesting information?One of the answers is to intelligently display relevant documents. This can effectively enhance user experience, extend the time users spend on the website, and for search engine optimization (SEO), it can also help spiders better crawl and understand the website structure through the construction of internal links, thereby improving the relevance of the content and the overall weight of the website.In Anqi CMS, it is not a complex thing to get and display the list of related documents

2025-11-09

How to implement the links and title display of the previous and next document pages on the content detail page?

In website operation, the navigation function of the previous and next pages on the content detail page may seem trivial, but it has a significant impact on user experience and the SEO performance of the website.It not only guides users to continue browsing more related content, improving the depth and stay time of page visits, but also provides a clear page flow for search engine crawlers, helping to index and rank the website's content.For those who use AnQiCMS to manage content, achieving this function is very direct and efficient

2025-11-09

How to display the document Tag label on the front-end page and use it as an entry for content association?

In modern website operations, how to effectively organize and present content is not only related to user experience, but also a key aspect of Search Engine Optimization (SEO).AnQiCMS (AnQiCMS) provides a powerful and flexible Tag label feature, which not only helps you better manage website content but also serves as a convenient entry point for users to find related information, greatly enhancing the association of content and the interactivity of the website. ### Overview of Tag Labels in AnQi CMS The design philosophy of Tag labels in AnQi CMS is very open and flexible.They are different from the traditional classification system

2025-11-09

How to set a default thumbnail to ensure that documents without uploaded images also have a unified display?

When using AnQi CMS to manage website content, we often encounter such situations: some articles, products, or pages may not have uploaded exclusive thumbnails due to insufficient content or oversight.This not only makes the website page look unprofessional and inconsistent, but may also affect users' desire to click on the content.Luckyly, Anqi CMS provides a very practical feature that can solve this problem once and for all - that is, setting a default thumbnail to ensure that even documents that do not upload pictures separately can have a unified and beautiful display effect.Why is it necessary to set a default thumbnail

2025-11-09

How to use the `archiveFilters` tag to implement dynamic display of a multi-condition filter list?

How can we make it quick for users to find the information they need on the website, which is a key factor in improving user experience and conversion rate.When a website contains a variety of content, and each type of content has multiple attributes to choose from, providing a flexible multi-condition filtering function becomes particularly important.AnQiCMS provides a powerful `archiveFilters` tag that can help us easily implement dynamic filtering of content lists, making your website content come to life.Why do we need multi-condition filtering? Imagine that you are browsing a real estate website

2025-11-09

How does Anqi CMS implement highly customized display of the website front-end page?

How to implement high-level custom display of the website front-end page in AnqiCMS? Want to create a unique and powerful website? The key is to have a flexible grasp of the front-end page.AnqiCMS fully understands this, it not only provides an efficient and stable background management, but also endows users with extremely high customization capabilities on the front end.This is due to its exquisite template design, flexible content organization, and rich tag system, allowing users to build and present websites according to their own imagination.### One, Flexible Template System

2025-11-09

How to use the flexible content model of Anqi CMS to display diverse content structures?

In this era where content is king, the content of websites is no longer limited to simple articles and news.From e-commerce products to event information, from customer cases to job positions, each type of content has its unique structure and display requirements.If a content management system (CMS) can only use a single "article" template to carry all information, it will undoubtedly bring great difficulties to operation, not only making content management efficiency low, but also making the front-end display monotonous, difficult to attract users.

2025-11-09

How to configure and switch the multilingual content display on the Anqi CMS website?

Today, in an era of increasing globalization, it is an important step to enable your website content to be presented in multiple languages, to expand the market and serve users in different regions.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that fully considers this need and provides a variety of solutions to help you easily configure and switch between multilingual content.Next, we will explore how to effectively manage and display multilingual content in Anqi CMS.### The multilingual concept of AnQi CMS: Distinction between system and content In AnQi CMS, multilingual support is mainly reflected in two levels

2025-11-09