How to get the custom model field content of the `archiveDetail` tag?

In the powerful and flexible AnQiCMS content management system, the content model plays a core role, allowing us to build various personalized content structures according to business needs.Whether it is an article, product, event, or other custom information, we can define unique fields for it.How can one elegantly present these rich custom field contents on the front-end page, which has become the key to template development.archiveDetailThe power of tags, especially how they help us easily obtain the custom model fields of documents.

The flexible cornerstone of AnQiCMS: content model and custom fields

Let's briefly review the content model of AnQiCMS.In the AnQiCMS backend, we can create or edit different content types through the 'Content Management' module under the 'Content Model' feature.For example, an "article" model may include basic fields such as "title", "content", "publish time", but business requirements may also require recording fields such as "article author", "article source", "editor in charge", etc.For example, a "product" model may also require "product model

These 'article author', 'product model', 'brand' and so on are the custom fields we define for a specific content model.AnQiCMS provides various field types such as single-line text, multi-line text, numbers, single selection, multiple selection, and images (group images), greatly enriching the content presentation.After defining these fields, when we publish the content of the corresponding model, we can fill in these additional information.

Deep understandingarchiveDetailLabel: The universal key to document details

In AnQiCMS template design,archiveDetailThe tag is used to obtain all detailed information of a single document on the document detail page.It is like a universal key that can easily extract document data from the current page or a specified ID.archiveDetailIts real power lies in its ability to seamlessly integrate with our custom content model fields.

When we need to display detailed information of a document on the document detail page, we usually use this tag. Its basic usage is:{% archiveDetail 变量名称 with name="字段名称" %}.If a variable name is not specified, it will directly output the field content; if a variable name is specified, the obtained data can be stored in the variable for subsequent more complex processing.

Unlock custom model field:archiveDetailAdvanced usage of

Now, let's focus on the core issue: how toarchiveDetailget the custom model field content through tags.

AnQiCMS provides a very intuitive way for this. Whether your custom field is simple text, numbers, or complex arrays (such as group images) or multiple choices,archiveDetailCan all胜任.

  1. Get a single text or numeric custom field:Assuming we add a field named "author" (the calling field is namedauthor)” and a field named “Reading Time (field calledreadDuration)” custom field. We can easily access them in the article detail page template:

    <p>作者:{% archiveDetail with name="author" %}</p>
    <p>预计阅读时长:{% archiveDetail with name="readDuration" %} 分钟</p>
    

    The key point is herename="author"andname="readDuration".nameThe value of the parameter must match the name of the 'called field' you entered when defining custom fields on the AnQiCMS backend.

  2. Retrieve complex custom fields: take a group chart as an example:If our custom field is a relatively complex type, such as adding a named 'Product image set' field for the 'Product' modelproductImagesThe group field that indicates this is usually an array of image URLs. To display these images in a loop on the front end, we need to convertarchiveDetailAssign the obtained data to a variable, thenforloop through it.

    {% archiveDetail productImages with name="productImages" %}
    <div class="product-gallery">
        {% for imageUrl in productImages %}
            <img src="{{ imageUrl }}" alt="产品图片" />
        {% endfor %}
    </div>
    

    In this example,{% archiveDetail productImages with name="productImages" %}Assign the obtained image URL array toproductImagesthe variable. Then, we can use{% for imageUrl in productImages %}Loop through and display each image

Practical application: Custom information display on product detail page

Let's combine a real-world scenario to build a product detail page fragment, which includes built-in fields and multiple custom fields:

Assuming our 'Product' model in addition to built-in fields also customizes the following fields:

  • Brand:Field invocationbrand(Single-line text)
  • Model:Field invocationmodel(Single-line text)
  • Product features:Field invocationfeatures(Multiline text, may contain HTML format)
  • Product image set:Field invocationgallery(Group photo)

The product detail page template code snippet may be as follows:

<div class="product-detail-container">
    <h1>{% archiveDetail with name="Title" %}</h1> {# 内置标题字段 #}

    <div class="product-info">
        <p>品牌:{% archiveDetail with name="brand" %}</p> {# 自定义品牌字段 #}
        <p>型号:{% archiveDetail with name="model" %}</p> {# 自定义型号字段 #}
        <p>浏览量:{% archiveDetail with name="Views" %}</p> {# 内置浏览量字段 #}
        <p>发布日期:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</p> {# 内置发布时间字段 #}
    </div>

    <div class="product-gallery">
        {% archiveDetail productGalleryImages with name="gallery" %} {# 获取自定义组图字段 #}
        {% if productGalleryImages %}
            {% for image in productGalleryImages %}
                <img src="{{ image }}" alt="{% archiveDetail with name="Title" %} - 产品图" />
            {% endfor %}
        {% else %}
            <img src="{% archiveDetail with name="Logo" %}" alt="{% archiveDetail with name="Title" %} - 主图" /> {# 如果没有组图,显示内置Logo #}
        {% endif %}
    </div>

    <div class="product-description">
        <h2>产品概述</h2>
        {% archiveDetail productDescription with name="Description" %} {# 内置描述字段 #}
        <p>{{ productDescription }}</p>
    </div>

    <div class="product-features">
        <h2>产品特点</h2>
        {% archiveDetail productFeatures with name="features" %} {# 获取自定义产品特点字段 #}
        <div class="features-content">{{ productFeatures|safe }}</div> {# 假设产品特点可能包含HTML,使用|safe过滤器 #}
    </div>

    <div class="product-full-content">
        <h2>详细内容</h2>
        {% archiveDetail articleContent with name="Content" %} {# 内置内容字段 #}
        <div class="full-content">{{ articleContent|safe }}</div> {# 内容通常包含HTML,需要|safe #}
    </div>
</div>

In this example, we clearly see thatarchiveDetailHow to flexibly obtain built-in information of documents, as well as the 'brand', 'model', 'product image set', and 'product features' fields we have customized for the 'product' model. For fields that may contain HTML content (such as 'product features' or main content), we have used|safeFilter, ensure that HTML tags are parsed correctly and not displayed as plain text.

Summary

archiveDetailTags are an indispensable tool in the development of AnQiCMS templates.It not only helps us display the basic information of the document, but more importantly, it provides a simple and powerful mechanism to access and utilize various fields defined in the custom content model.nameParameters and flexible variable assignment, we can perfectly map the personalized content structure built in the background to the front-end page, thereby providing users with rich and customized content experience.Mastering this usage will undoubtedly greatly enhance your efficiency in content operation and website development on AnQiCMS.


Frequently Asked Questions (FAQ)

  1. Q: Why do I use{% archiveDetail with name="my_custom_field" %}but can't display the content? A:This situation usually has several reasons: first, please checknameDoes the value of the parameter match the name of the 'call field' you defined in the AnQiCMS backend, including case sensitivity.Next, confirm that the document you are viewing has indeed filled in the content of the custom field.If the field content is empty, the front-end will not display any information naturally.my_custom_fieldcustom field.

  2. Q: Why is my custom field displayed as HTML source code instead of formatted content, being a rich text editor type? A:This is because the AnQiCMS template engine defaults to escaping all output content to prevent XSS attacks. If your custom field content is already HTML code (for example, from a rich text editor), you need to use|safeThe filter tells the template engine that this content is safe and does not need to be escaped. For example:{% archiveDetail customRichText with name="myRichTextField" %}{{ customRichText|safe }}.

  3. Q: BesidesarchiveDetailAre there other ways to retrieve the custom field content of the document? A:Yes, except when used on the document detail pagearchiveDetailTags get the custom fields of the current document, and you can also use them in the document list loop (for example, usingarchiveListWhen a label is used), it can be accessed by referring to the custom properties of the loop variable. For example, in aarchiveListofforloop, ifitemit is the current document object, and you have a namedauthorThe custom field can be accessed directly{{ item.Author }}Note that the field name is usually capitalized first