One of the core values of a content management system is its ability to flexibly define and present various types of content.For users of AnQiCMS (AnQiCMS), whether it is to display a detailed article, a product page, or a custom data entry, how to efficiently obtain its complete content and all associated fields is the key to front-end template development and content presentation.
Today, let's delve deeply into how to easily control the acquisition and display of individual document content in AnQiCMS through this '万能钥匙' template tag.
Get the core content of the document:archiveDetailThe clever use of tags
In AnQiCMS,archiveDetailThe tag is the entry point to obtain detailed information about a single document. This tag is very flexible, as it can directly output the value of a field or assign the entire document object to a variable for more complex processing.
As a rule, on the document detail page, the system automatically recognizes the current document, and we do not need to specify an ID. But if you need to call a specific document on another page, you can do so byidUse parameters to accurately obtain. For example, to get the title of the document with ID 10, you can write it like this:{% archiveDetail with name="Title" id="10" %}.
The following are some common methods for obtaining and displaying fields:
- Document title (
Title)This is one of the most core pieces of information in the document. On the document detail page, you can usually display it directly through{{ archive.Title }}To display. If you need a more general way of writing, or to call it on other pages, you can use{% archiveDetail with name="Title" %}. - Document content (
Content): The main content of the document often includes rich text, images, and may even be in HTML or Markdown format. To ensure that the content is correctly parsed and displayed, especially when the content contains HTML tags, it is necessary to use|safeA filter to prevent content from being escaped. If the document content is written using a Markdown editor and you want to automatically convert it to HTML on the front end, you can add to the tagrender=trueparameters:
If you do not need to render Markdown, or the content itself is pure HTML, you can omit{# 获取并安全显示文档内容,如果内容是Markdown格式,则自动渲染为HTML #} {% archiveDetail documentContent with name="Content" render=true %} {{ documentContent|safe }}render=true. - Document summary (
Description): A concise document summary, often used on list pages or SEO descriptions. The method of acquisition is similar to the title:{% archiveDetail with name="Description" %}. - Document link (
Link)Each document has a unique access address. Obtain the link to build internal navigation or share:{% archiveDetail with name="Link" %}. - Publish time (
CreatedTime): Content's publish timestamp, usually needs to be formatted into a readable date and time. AnQiCMS providesstampToDatefunction to handle:
You can customize the output format according to the Go language date formatting rules, for example{# 获取发布时间戳并格式化为“年-月-日” #} {% archiveDetail createdTime with name="CreatedTime" %} 发布日期:{{ stampToDate(createdTime, "2006-01-02") }}"2006年01月02日 15:04". - Thumbnail (
Thumb) and the cover image (Logo): The image is an important element to attract users. These fields usually return the image URL, which can be used directly for<img>Tags:{# 显示文档缩略图 #} <img src="{% archiveDetail with name="Thumb" %}" alt="{% archiveDetail with name="Title" %}" /> - Views (
Views): Show the popularity of the document:{% archiveDetail with name="Views" %}.
Deeply explore: Get all information of custom field (archiveParams)'s information
One of the strongest features of AnQiCMS is its flexible content model, which allows users to customize document fields according to business needs.For example, a product document may have fields such as "price", "inventory", "brand", etc., while a technical article may have fields such as "author", "source", "tags", etc.archiveParamsTags are used to get these custom fields.
archiveParamsThe tag can retrieve all custom fields of the document as a collection. You cansorted=trueget an ordered array (default value) for easy traversal; or usesorted=falseGet an unordered Map object, access it directly by the field name "call field".
1. Traverse all custom fields:
If you want to display all custom fields and their values on the page, you can usearchiveParamsthe tag to get an ordered array andforiterate through it with a loop. Each iteration will containName(Parameter name, i.e., the Chinese name displayed on the backend) andValue(Parameter value).
{# 获取当前文档的所有自定义参数,并遍历显示 #}
{% archiveParams params %}
<div class="document-parameters">
{% for item in params %}
<p>
<span>{{ item.Name }}:</span>
<span>{{ item.Value }}</span>
</p>
{% endfor %}
</div>
{% endarchiveParams %}
2. Directly access a specific custom field:
If you only need to retrieve a specific custom field (for example, one namedauthorThe field that you don't want to iterate through all fields, there are usually two convenient methods:
Direct pass.
archiveObject access (recommended): For custom fields defined in the content model, they are often directly mounted toarchiveOn the document object. You can use it directly on the document details page{{ archive.自定义调用字段名 }}To get it in the form{# 假设你在后台定义了一个调用字段名为 'author' 的自定义字段 #} <p>作者:{{ archive.author }}</p> {# 假设有一个调用字段名为 'product_warranty' 的自定义字段 #} <p>产品保修:{{ archive.product_warranty }}</p>This method is concise and intuitive, and is the preferred method for obtaining specific custom fields
By
archiveParamstags toMapto access in the formIf directly througharchiveThe object cannot be accessed (this situation is rare, usually occurs in complex custom type fields), or you need to make sure that the field is indeed asarchiveParamsStored, can be usedsorted=falseGet Map, and through{{ params.你的调用字段名.Value }}.{# 获取无序的自定义参数Map,并直接访问名为 'author' 的字段值 #} {% archiveParams params with sorted=false %} <p>作者:{{ params.author.Value }}</p> {% endarchiveParams %}
Important reminder:
- Custom field name of 'called field' (as shown in the above example,
authororproduct_warrantyThe name must match exactly (case-sensitive) the name you set in the background "Content Model" in order to correctly retrieve its value. - If the value of a custom field itself is HTML code, it also needs to use
|safea filter to ensure correct display, for example{{ archive.my_html_field|safe }}.
Comprehensive Application Example
Let's take a product detail page as an example to show how to combine basic information and custom fields:
`twig
{# 产品封面图 #}
<div class="product-image">
<img src="{% archiveDetail with name='Logo' %}" alt="{% archiveDetail with name='Title' %}" />
</div>
<div class="product-info">
{# 产品标题 #}
<h1>{% archiveDetail with name="Title" %}</h1>
{# 产品简介 #}
<p class="description">{% archiveDetail with name="Description" %}</p>
{# 获取并显示自定义产品参数 #}
<div class="product-specs">
<h3>产品参数</h3>
{% archiveParams params %}
{% for item