How to use the `archiveDetail` tag in AnQiCMS template to get document details and custom fields?
As a senior security CMS website operator, I fully understand the core position of content in website operation.We must not only create high-quality content, but also ensure that this content is presented to users in a **way.In this, the flexible application of templates is crucial. Today, we will delve into a powerful and commonly used tag in the AnQiCMS template -archiveDetailIt can help us easily obtain the detailed information of the document, including those carefully designed custom fields.
UnderstandingarchiveDetailThe core role of tags
In AnQiCMS,archiveDetailTags are an important tool used to obtain all detailed data of specific documents (such as articles, products, etc.) in templates. Whether it is to display the content of the current page's document or to obtain data from other specified documents as needed,archiveDetailEverything can provide everything needed. It defaults to fetching the information of the document being accessed at the moment, but by simple parameter settings, we can also accurately capture the data of any document on the website.
The basic usage of this tag typically includes specifying a variable name to store the retrieved data, and usingnameThe parameter is used to specify the specific field to be retrieved. If the variable name is not specified, the tag will directly output the field value requested.
archiveDetailCommon parameters of the tag
archiveDetailThe flexibility of the tag is reflected in the parameters it supports. In addition to the default retrieval of the current document, it also allows precise control of the data source through the following parameters:
- Document ID (
id)This is an optional parameter used to get the details of the document with a specified ID.When you need to display document data that is not on the current page, such as recommended articles in the sidebar, you can use this parameter. - Document URL alias (
token): withidsimilar,tokenIt is also used to specify the document. If you are more familiar with the document's URL alias (usually pinyin or a custom short link), you can use this parameter. - Site ID (
siteId)For users using the AnQiCMS multi-site management function,siteIdIt can help you call data across sites. If your website is a single-site architecture, it is usually not necessary to set this parameter.
The clever combination of these parameters, allowsarchiveDetailCan adapt to various complex template requirements.
Get the standard field information of the document.
archiveDetailThe tag can return various standard fields of the document, which cover the basic properties, SEO information, and media content of the document. The following are some common available fields and their usage examples:
Document ID (
Id), Title (Title) and link (Link), Description (Description)These are the most basic and commonly used information.{# 获取当前文档的标题 #} <h1>{% archiveDetail with name="Title" %}</h1> {# 获取指定ID文档的链接 #} <a href="{% archiveDetail with name="Link" id="10" %}">查看详情</a> {# 将文档描述赋值给变量并使用 #} {% archiveDetail docDesc with name="Description" %}<meta name="description" content="{{docDesc}}">Document content (
Content)This is the main content of the document. It is worth noting that,ContentFields support additional properties to handle image lazy loading and Markdown rendering.- Image lazy loading (
lazy): If your website uses a lazy loading plugin, you can uselazy="data-src"and so on toimglabel'ssrcProperties should be replaced with the properties required by the plugin. - Markdown rendering (
render): When the Markdown editor is enabled on the back end, the content will be automatically rendered. You can also userender=trueorrender=falseManually control whether to perform Markdown to HTML conversion.
{# 获取当前文档内容,并处理图片懒加载和Markdown渲染 #} {% archiveDetail docContent with name="Content" lazy="data-src" render=true %}{{docContent|safe}} {# `|safe`过滤器用于避免HTML实体转义,确保内容正确显示 #}- Image lazy loading (
Views (
Views), Number of comments (CommentCount)Used to display interactive data in the document.<span>浏览量:{% archiveDetail with name="Views" %}次</span>Cover image (
Logo/Thumb/Images):LogoGenerally refers to large images,ThumbRefers to thumbnails, andImagesIt may be an image array used to display a group of images.{# 获取文档Logo #} <img src="{% archiveDetail with name="Logo" %}" alt="{% archiveDetail with name="Title" %}"> {# 循环输出文档组图 #} {% archiveDetail docImages with name="Images" %} <ul> {% for img in docImages %} <li><img src="{{img}}"></li> {% endfor %} </ul>Creation time (
CreatedTime) and update time (UpdatedTime): These fields return timestamps and need to be coordinated withformatorstampToDateformatted by the filter.<span>发布日期:{% archiveDetail with name="CreatedTime" format="2006-01-02 15:04" %}</span> {# 或者使用变量和过滤器 #} {% archiveDetail createdTime with name="CreatedTime" %}{{stampToDate(createdTime, "2006年01月02日")}}Document classification (
Category)This field returns an object containing classification information. You can directly access its properties, or consider usingcategoryDetailtags to get more comprehensive classification information.{% archiveDetail docCategory with name="Category" %} <p>所属分类:<a href="{{docCategory.Link}}">{{docCategory.Title}}</a></p>Document tags (Tag): Although
archiveDetailIt does not directly return the tag list, but you can combine it withtagListtags to get related tags.{% archiveDetail currentDocId with name="Id" %} {% tagList tags with itemId=currentDocId %} {% for tag in tags %} <a href="{{tag.Link}}">{{tag.Title}}</a> {% endfor %} {% endtagList %}
Get the custom field information of the document
AnQiCMS's powerful flexible content model allows us to define dedicated custom fields for different types of documents. These custom fields can also be accessed througharchiveDetailLabel to obtain, which greatly enhances the customization capabilities of the template.
If you know the name of the custom field explicitly (for example, if you have defined a field named in the content model)authorThe field can be accessed directlynameCall the parameter:
{# 获取自定义字段“author”的值 #}
<p>作者:{% archiveDetail with name="author" %}</p>
Further, if you need to iterate over all custom fields or are unsure of the specific names of all fields, you can usearchiveParamsTag. It will return all custom fields as array objects, making it convenient for you to loop through and display:
{% archiveParams params %}
<div>
{% for item in params %}
<p>
<span>{{item.Name}}:</span> {# 字段的显示名称 #}
<span>{{item.Value}}</span> {# 字段的值 #}
</p>
{% endfor %}
</div>
Combining with specific business scenarios, for example, a product model defines a field namedspecificationsThe custom field, whose value is a piece of HTML code or a JSON string, you can handle it like this in the template:
{% archiveDetail productSpecs with name="specifications" %}<div class="product-specs">{{productSpecs|safe}}</div>
Or, ifarcimagesIs a custom group chart field, you can display it like this:
{% archiveDetail customImages with name="arcimages" %}
<div class="custom-gallery">
{% for imgUrl in customImages %}
<img src="{{imgUrl}}" alt="产品图">
{% endfor %}
</div>
Document detail display in actual application
ByarchiveDetailThe combination of related tags allows us to easily build a rich and detailed document detail page.
Consider a typical article detail page, which needs to display the article title, category, publish time, associated tags, reading volume, and article content. The template code may look something like this:
<article>
<h1>{% archiveDetail with name="Title" %}</h1>
<div class="meta-info">
{% archiveDetail currentCategory with name="Category" %}
<span class="category-link"><a href="{{currentCategory.Link}}">{{currentCategory.Title}}</a></span>
<span>发布于:{% archiveDetail with name="CreatedTime" format="2006-01-02" %}</span>
{% archiveDetail currentDocId with name="Id" %}
{% tagList tags with itemId=currentDocId %}
<span class="tags">标签:
{% for tag in tags %}<a href="{{tag.Link}}">{{tag.Title}}</a> {% endfor %}
</span>
{% endtagList %}
<span>阅读:{% archiveDetail with name="Views" %}次</span>
</div>
<div class="article-content">
{% archiveDetail articleBody with name="Content" render=true lazy="data-src" %}{{articleBody|safe}}
</div>
</article>
And for a product detail page, in addition to the product name and image, it is usually necessary to display its unique parameters, introduction, and detailed description. If we define in the product model thatbrand(Brand),model(Model) and other custom fields, the template can be implemented as follows:
<div class="product-detail-container">
<div class="product-media">
<img src="{% archiveDetail with name="Logo" %}" alt="{% archiveDetail with name="Title" %}" class="main-product-image">
{% archiveDetail galleryImages with name="Images" %}
<div class="product-gallery">
{% for img in galleryImages %}<img src="{{img}}">{% endfor %}
</div>
</div>
<div class="product-info">
<h1>{% archiveDetail with name="Title" %}</h1>
<p class="product-description">{% archiveDetail with name="Description" %}</p>
<div class="product-specs">
<h3>产品参数</h3>
{% archiveParams productParams %}
<ul>
{% for item in productParams %}
<li><strong>{{item.Name}}:</strong>{{item.Value}}</li>
{% endfor %}
</ul>
{% endarchiveParams %}
</div>
<div class="full-description">
<h3>详细介绍</h3>
{% archiveDetail productContent with name="Content" render=true %}{{productContent|safe}}
</div>
</div>
</div>
Summary
archiveDetailTags are an indispensable tool in AnQiCMS template development.It provides an intuitive and powerful way to access and display various information about documents, whether it is built-in standard fields or customized fields tailored to business needs.Mastery of its usage and parameters will give you a high degree of freedom and efficiency in presenting content on the website, ensuring that every piece of content is presented to your users in the most perfect state.
Frequently Asked Questions
Why does my custom field not display in the template? Is there a problem?First, make sure that you have correctly defined these custom fields in the AnQiCMS backend content model and filled in the content when publishing documents. Second, check the custom fields in the template.nameIs the parameter exactly consistent with the name of the 'call field' defined in the background, and pay attention to the case.AnQiCMS template tags are case-sensitive.If the value of the custom field is an empty string ornilIt may not output anything, you can use a filter likedefaultto set the default value for troubleshooting.
How can I get more information about the current document's category, such as the category's Logo or Banner image?When you go through{% archiveDetail docCategory with name="Category" %}After obtaining the document's category object,docCategoryThe variable includes all properties of the category, such asdocCategory.Link/docCategory.Title. If you need to get the category'sLogoorBannerchart, etc., you can use it directlydocCategory.LogoordocCategory.Images. If you need to get more complex category custom fields or do not want to rely onarchiveDetailGet the category object, you can also usecategoryDetailtags, through{% categoryDetail with name="Logo" id=docCategory.Id %}To get more details of the category with specified ID.
Why is myContentThe field displays HTML or Markdown syntax, not the rendered content?IfContentThe field displays the original HTML code, possibly because you did not use|safeFilter. AnQiCMS for security, will automatically escape HTML tags,|safeThe filter tells the template engine that this is safe content and does not need to be escaped. If the display is Markdown syntax, it may be that the Markdown editor is not enabled on the backend, or thatarchiveDetailThe tag is not setrender=trueAdd parameters to force Markdown to HTML conversion. Please add according to your needs|safeFilters andrenderParameter.