One of the core values of the content management system lies in its ability to flexibly define and present various types of content.For users of 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 into how to easily obtain and display individual document content in AnQiCMS using the 'universal key' of template tags.
Get the core content of the document:archiveDetailThe magic use of tags
In AnQiCMS,archiveDetailThe label is the entry point to obtain detailed information about a single document.This tag is very flexible, it can directly output the value of a field, or assign the entire document object to a variable for more complex processing.
English, usually on the document detail page, the system automatically recognizes the current document, and we do not need to specify the ID separately. But if you need to call a specific document on other pages,idParameter to obtain accurately. For example, to obtain 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 core pieces of information in the document. On the document detail page, you usually can directly show it. If you need a more generic format, or when calling it on other pages, you can use{{ archive.Title }}to display. If a more general format is needed, or when calling it on other pages, you can use{% archiveDetail with name="Title" %}. - Document content (
Content): The document body usually contains rich text, images, and may even be in HTML or Markdown format. To ensure the content is correctly parsed and displayed, especially when the content contains HTML tags, it is imperative to use|safeThe filter to prevent content from being escaped. If the document content is written by a Markdown editor and you want to automatically convert it to HTML on the front end, you can add it in the tag.render=trueParameters:
If you do not need to render Markdown, or the content is pure HTML, you can omit it.{# 获取并安全显示文档内容,如果内容是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 for SEO descriptions. The method of obtaining it 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): The publishing timestamp of the content, usually needs to be formatted into a readable date and time. AnQiCMS providesstampToDatea function to handle:
You can customize the output format according to the Go language's date formatting rules, for example{# 获取发布时间戳并格式化为“年-月-日” #} {% archiveDetail createdTime with name="CreatedTime" %} 发布日期:{{ stampToDate(createdTime, "2006-01-02") }}"2006年01月02日 15:04". - Thumbnail (
Thumb) and cover image (Logo): The image is an important element in attracting users. These fields usually return the image URL, which can be used directly.<img>Tags:{# 显示文档缩略图 #} <img src="{% archiveDetail with name="Thumb" %}" alt="{% archiveDetail with name="Title" %}" /> - Views (
Views): Display the popularity of the document:{% archiveDetail with name="Views" %}.
Deep Dive: Get all information of custom fields (archiveParams)'s information
One of the most powerful features of AnQiCMS is its flexible content model, which allows users to customize document fields according to their 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.
archiveParamsLabels can retrieve all custom fields of a document as a collection. You can get an ordered array (default values) for easy traversal; or usesorted=true参数(默认值)获取一个有序的数组,方便遍历;或者使用sorted=falseObtain an unordered Map object, access it directly through the 'call field' name of the field.
1. Traverse all custom fields:
If you want to display all custom fields and their values on the page, you can usearchiveParamsa labeled array, and thenforloop through it. Each item in the loop will containName(Parameter name, i.e. the Chinese name displayed on the back end) 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 get a specific custom field (for example, a field namedauthorThe field (as specified), rather than traversing all fields, usually has two convenient methods:
Proceed directly
archiveObject access (recommended): For custom fields defined in the content model, they are often directly mounted toarchivethe document object. You can use it directly on the document detail page.{{ archive.自定义调用字段名 }}to obtain it.{# 假设你在后台定义了一个调用字段名为 'author' 的自定义字段 #} <p>作者:{{ archive.author }}</p> {# 假设有一个调用字段名为 'product_warranty' 的自定义字段 #} <p>产品保修:{{ archive.product_warranty }}</p>This method is concise and intuitive, the preferred method for accessing specific custom fields.
Pass
archiveParamslabels toMapForm-based access: If accessed directly througharchiveObject is not accessible (this occurs rarely, usually in more complex custom type fields), or you need to make sure that this field is actually being stored asarchiveParamsstored, you can usesorted=falseGet Map and access{{ params.你的调用字段名.Value }}to access.{# 获取无序的自定义参数Map,并直接访问名为 'author' 的字段值 #} {% archiveParams params with sorted=false %} <p>作者:{{ params.author.Value }}</p> {% endarchiveParams %}
Important reminder:
- Custom field "Field Name" (as shown in the example above,
authororproduct_warrantyIt must be identical to the name you set in the "Content Model" on the backend (case-sensitive) to correctly retrieve its value. - If the value of the custom field itself is HTML code, it also needs to be used
|safea filter to ensure correct display, for example{{ archive.my_html_field|safe }}.
Comprehensive Application Examples
Let us take a product detail page as an example to show how to combine basic information with 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