Unlock the charm of custom field customization in AnQi CMS content model:archiveDetailAdvanced application of tags

As an experienced website operations expert, I am well aware of the importance of a flexible content management system for improving operational efficiency and achieving personalized content display.AnQiCMS (AnQiCMS) excels in this aspect with its efficient architecture based on the Go language and its excellent customizability.One of its core highlights is the "flexible content model" feature, which allows us to create dedicated fields for different types of content according to specific business needs, greatly broadening the possibilities of content presentation.

However, having these powerful custom fields is just the first step.How to present these unique data of the model accurately and elegantly on the detail page of the website front-end is a concern for many operators.authorField), how to use it in AnQi CMS'sarchiveDetailTags to call and display these unique field information.

Content Model: The Foundation of Anqi CMS Flexible Content Architecture

Before delving into the technical details, let's quickly review the content model of Anqi CMS.In AnQi CMS backend, you can easily add additional custom fields for built-in models such as 'Articles', 'Products', or create new content models.author)","Source(source)","Editor in Charge(editor

In the "Content Model Usage Help", you will find that each custom field has a "call field" setting when created, which is usually an identifier consisting of English lowercase letters, such as the one we mentionedauthor. This 'call field' is the key we use to refer to the custom data in the front-end template.

archiveDetailLabel: The core of the data center of the content detail page.

When a user visits a specific article or product detail page, Anqi CMS willarchiveDetailTag to load and display all relevant data for the page.This tag is the core of the content detail page, it can not only obtain the basic information such as the title, content, and publication time of the article, but is also the channel we use to access the custom fields of the model.

The AnQi CMS template engine uses a syntax similar to Django, making data calls both intuitive and powerful.We mainly have two practical and efficient methods for calling custom fields.

Method one: Direct accessarchiveObject, simple and intuitive

In the template file of the document detail page (usually{模型table}/detail.htmlIn this case, AnQi CMS will intelligently aggregate all data of the current document, including built-in fields and fields you customize, into a namedarchiveThe global variable is. This means that once you enter a document's detail page, thisarchivevariable carries all the information of the document.

Therefore, to call a namedauthorThe custom field, the most direct and simplest way is to use dot notation, as if accessing the properties of an object:

<p>作者:{{ archive.author }}</p>

here,archiveIs the object of the current document, and.authorIt directly points to what you have defined in the content modelauthorThe value of the custom field. This method is widely used in daily template development due to its conciseness

Method two: througharchiveDetailExplicitly call the tag, flexible and accurate

Although direct accessarchiveObjects are very convenient, but AnQi CMS also provides a more explicit way to go througharchiveDetailThe method to label to get the value of a single custom field.This method is particularly suitable for cases where you need to obtain a specific custom field from a non-current document (such as through ID specification) or in certain specific context scenarios where you want to have more explicit control over the source of data acquisition.

Its basic usage is to utilizearchiveDetaillabel'snameThe parameter, specifying the name of the 'call field' of the custom field you want to retrieve:

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

If you want to store the obtained value in a temporary variable for subsequent processing, you can also do it like this:

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

This method provides greater flexibility, allowing you to precisely control the source and destination of each data call.

Method three: dynamically traverse all custom fields to meet various demands.

Sometimes, you may want to dynamically display all custom fields, especially when the content model fields may change frequently, or when you want to create a universal detail page template without needing to know all the custom field names in advance. At this point,archiveParamsThe tag comes in handy. It can retrieve all custom parameters of a specified document and return them as an array, allowing you to easily iterate over them.

Here's how to combinearchiveParamsTags andforLoop to display all custom field examples:

<h3>更多文档参数:</h3>
<div>
    {% archiveParams params %}
    {% for item in params %}
        <p>{{ item.Name }}:<span>{{ item.Value }}</span></p>
    {% endfor %}
    {% endarchiveParams %}
</div>

In this example,archiveParamsTags wrap all custom fields of the current document into an array namedparams(default is sorted). ThroughforLoop through thisparamsarray,item.NameIt will display the custom field name set in the background (usually the Chinese display name), anditem.ValueThis field will display specific content. This is very useful for creating highly dynamic and configurable detail page templates.

Practice exercise: Author information on the article detail page.

Let's go through a simple practical case to demonstrate how to elegantly display a custom 'author' field on the article detail page.

Assuming we have added a "call field" for the "article model" namedauthorcustom field. In yourtemplate/default/article/detail.htmlIn the template (or any custom article detail template) you can lay out like this:

”`twig {% extends ‘base.html’ %} {# Inherit base layout, assuming it exists #}

{% block content %}

<h1>{{ archive.Title }}</h1>
<div class="meta-info">
    <span>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</span>
    {# 调用自定义作者字段 #}
    {% if archive.author %}
    <span>作者:<span class="author-name">{{ archive.author }}</span></span>