AnQi CMS features a flexible content model design, allowing users to add various custom fields to documents according to their business needs.These custom fields greatly enrich the presentation of the website's content, making it easy to handle product specifications, event details, or additional article information.archiveDetailTag to display these valuable custom field contents?
UnderstandingarchiveDetailThe core function of the tag
In the template system of AnQi CMS,archiveDetailTags are a powerful tool for obtaining detailed information about a single document.When you ask for a specific article or a product page on a website, it is usually this tag working behind the scenes, presenting all the document's data to the visitor.
The basic usage of this tag is as follows:{% archiveDetail 变量名称 with name="字段名称" id="1" %}.
nameParameter specifies which field's content you want to retrieve, such as document titleTitle, document contentContentetc.idThe parameter is used to specify the specific document ID. If you want to get information about a particular document, you can use this parameter.tokenThe parameter is the URL alias of the document, which can also be used to specify the document.siteIdIt is used to specify which site's data to retrieve in a multi-site environment.
However, most of the time, when you are on the document detail page (for example, the file name{模型table}/detail.htmltemplate) usingarchiveDetailtags, it is usually not necessary to explicitly specifyidortokenThe system will intelligently recognize the document on the current page and automatically obtain its detailed data. At this point, you can directly use{{archive.字段名称}}This abbreviated way of calling the standard fields of the document is convenient and intuitive. However, for custom fields, we often rely more onarchiveDetailTagsnameparameters to accurately obtain.
Display the standard field content of the document
Before we delve into custom fields, let's take a look atarchiveDetailHow to display some common standard fields. These fields are usually present in all documents and do not require additional configuration:
For example, to display the title, description, content, or view count of a document, you can do this:
<div>文档标题:{% archiveDetail with name="Title" %}</div>
<div>文档描述:{% archiveDetail with name="Description" %}</div>
<div>文档浏览量:{% archiveDetail with name="Views" %}</div>
<div>文档发布时间:{% archiveDetail with name="CreatedTime" format="2006-01-02" %}</div>
<div>文档内容:{% archiveDetail archiveContent with name="Content" %}{{archiveContent|safe}}</div>
Please note that when displaying the content of a document,Contentwe usually use|safeFilter. This is because the document content may contain HTML tags,|safeTell the template engine that this content is safe and does not need to be escaped, thus ensuring that HTML can be rendered normally rather than displayed as plain text.
Core Feature: Display custom field content
The strength of AnQi CMS lies in its flexible content model.Under the 'Content Management' menu in the background, you can enter the 'Content Model' settings, create or modify models for different content types (such as articles, products), and add unique custom fields for them.These custom fields can be single-line text, numbers, multi-line text, single selection, multiple selection, or dropdown selection, among other types, aiming to meet various personalized content display needs.
1. Call a single custom field
Once a custom field is defined for a document model in the background, such as adding a field named “产品型号” with the field nameproduct_modelThen on the product detail page, you can call it directly througharchiveDetailTagsnameparameters:
<div>产品型号:{% archiveDetail with name="product_model" %}</div>
If you name the custom fieldauthoryou can get it like this:
<div>文章作者:{% archiveDetail with name="author" %}</div>
This method is simple and direct, suitable for the scenario where you know exactly which custom field to display.
2. Loop to display all custom fields
Sometimes, you may want to display all custom fields of a document in an area without having to list them explicitly.archiveParamsTags come into play.archiveParamsLabels can retrieve all custom parameters of the current document or a specified document and return them as an array.
You can use it like this:
{% archiveParams params %}
<div>
{% for item in params %}
<div>
<span>{{item.Name}}:</span>
<span>{{item.Value}}</span>
</div>
{% endfor %}
</div>
{% endarchiveParams %}
In this code,paramsis an array containing multiple objects, each object havingName(the Chinese name of the custom field) andValue(the value of the field) properties. ThroughforLoop, you can easily traverse and display all custom fields of the document. This is especially convenient for scenarios that require dynamic display of product parameter lists or detailed properties.
3. Handle special custom fields (such as image sets)
If your custom field type is an image set (multiple images), for example, you define a field named in the backgroundarcimagesThe group field, if it exists, will be recognized as an array of image URLs in the template. You need to use a loop to display each image:
{% archiveDetail arcimages with name="arcimages" %}
<ul class="arc-images">
{% for img in arcimages %}
<li><img src="{{img}}" alt="文档图片" /></li>
{% endfor %}
</ul>
Here, we first use the archiveDetailtag to get the name ofarcimagesAssign the group chart data toarcimagesthe variable, and then pass throughforloop through this array, and embed the URL of each image into<img>the tag.
Practice case: Combine standard and custom fields to display
To better illustratearchiveDetailThe practical application, let's see how to combine standard fields and custom fields in common articles and product detail pages.
Article detail page layout example:
Assuming you have a detailed article page, in addition to the standard information such as title, content, publish time, and views, you also want to display the "Article Source" (a custom field)source)and“Editor”(custom field)editor).
<article>
<h1>{% archiveDetail with name="Title" %}</h1>
<div>
<a href="{% categoryDetail with name='Link' %}">{% categoryDetail with name='Title' %}</a>
<span>发布时间:{% archiveDetail with name="CreatedTime" format="2006-01-02" %}</span>
<span>浏览量:{% archiveDetail with name="Views" %}°</span>
</div>
<div>
<span>文章来源:{% archiveDetail with name="source" %}</span>
<span>编辑者:{% archiveDetail with name="editor" %}</span>
</div>
<div>
{%- archiveDetail articleContent with name="Content" %}
{{articleContent|safe}}
</div>
</article>
Product detail page layout example:“
For the product detail page, you may need to display product images, product names, product descriptions, andarchiveParamsDisplay all product specifications dynamically.
<article>
<div>
<div>
<img src="{% archiveDetail with name='Logo' %}" alt="{% archiveDetail with name='Title' %}" />
</div>
<div>
<h1>{% archiveDetail with name="Title" %}</h1>
<div>产品简介:{% archiveDetail with name="Description" %}</div>
<h3>产品参数:</h3>
{% archiveParams params %}
{% for item in params %}
<div>
<span>{{item.Name}}:</span>
<span>{{item.Value}}</span>
</div>
{% endfor %}
{% endarchiveParams %}
<div>
<a href="tel:{% contact with name='Cellphone' %}" rel="nofollow">电话联系:{% contact with name="Cellphone" %}</a>
</div>
</div>
</div>
<div>
<h2>产品详情:</h2>
<div>
{%- archiveDetail articleContent with name="Content" %}
{{articleContent|safe}}
</div>
</div>
</article>
Tips and precautions for use
|safeThe filter is indispensable:When your custom field content (especially of 'Multi-line text' type) may contain HTML tags, be sure to use|safeFilter, otherwise the HTML code will be escaped to plain text and cannot be rendered normally.- Rendering of Markdown content:If the content of your custom field is entered using a Markdown editor and you want to render it as HTML on the frontend, you can
archiveDetailtag.render=trueparameters, such as{% archiveDetail customMarkdownField with name="customMarkdown" render=true %}{{customMarkdownField|safe}}. - Field names are strictly case-sensitive:In