In the Anqi CMS, to retrieve and display the detailed content of the current article, it mainly revolves around the organization of template files and built-in template tags.Understand these core mechanisms, and you can flexibly control the presentation of articles on the front page.
Core Concept: The structure of the article detail page
In the AnQi CMS, the detailed content of each article is usually displayed through a specific template file.This template file will be automatically or manually called by the system based on the content model of the article (such as "Article Model" or "Product Model") and the settings in the background.template/{你的模板目录}/{模型table}/detail.htmlThis path is used to render the article detail page. For example, if your article belongs to the "Article Model" (the defaulttablethe name might bearchive), the system will try to loadtemplate/{你的模板目录}/archive/detail.htmlfile.
In this detail page template, we need to use the powerful template tags provided by the Anqi CMS to "capture" the various data of the article and display it according to the design.archiveDetailLabels are the core to obtain detailed information of articles.
Deep understandingarchiveDetailtags
archiveDetailLabels are specifically used to extract detailed data of a single article from the database. Its basic usage is{% archiveDetail 变量名称 with name="字段名称" %}.
Obtain the current article dataWhen you are on the article detail page (for example
archive/detail.html) usedarchiveDetailTags are usually not required to specify the article ID.The system will intelligently recognize the article being accessed at the moment and automatically retrieve its data.{% archiveDetail with name="Title" %}Pass
nameThe parameter specifies the fields to be retrievedarchiveDetailTagged throughnameParameters can be used to specify which specific fields of the article you want to retrieve. These fields include the basic information of the article, SEO information, the content itself, and fields customized through the content model.The following are some commonly used fields and their applications:
Article title (
Title):{% archiveDetail with name="Title" %}This will directly output the title of the article.Article content (
Content):{% archiveDetail with name="Content" %}This is the main part of the article page. It should be noted that if the article content contains HTML tags (such as images, links, formatted text, etc.), it is usually necessary to cooperate with|safeFilter usage. If the article is written in Markdown and you have enabled the Markdown editor in the background, Safe CMS will automatically convert it to HTML; if manual control is needed, you can add it in the tag.render=trueorrender=falseParameter. Example:{% archiveDetail articleContent with name="Content" %}{{articleContent|safe}}{% endarchiveDetail %}Publish time (
CreatedTime) And update time (UpdatedTime): These fields return timestamps, and to display them in a readable date format on the page, you need to usestampToDatetimestamp formatting labels. Example:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}(Among whicharchiveisarchiveDetailVariable name defined by the tag,2006-01-02 15:04is the time formatting standard of Go language.)Article link (
Link) Keywords (Keywords) and the description (Description) views (Views): The ways to obtain these fields are similarTitle. Example:{% archiveDetail with name="Link" %}{% archiveDetail with name="Keywords" %}{% archiveDetail with name="Description" %}{% archiveDetail with name="Views" %}Cover image (
Logo/Thumb/Images):LogoGenerally refers to the main image or large image of an article,Thumbwhich is its thumbnail.ImagesIt may be a group of images (multiple images). Example:<img src="{% archiveDetail with name="Logo" %}" alt="{% archiveDetail with name="Title" %}" />If there are multiple images, they need to be defined as variables and then cycled through:{% archiveDetail archiveImages with name="Images" %}{% for img in archiveImages %}<img src="{{img}}" alt="文章图片" />{% endfor %}{% endarchiveDetail %}Category (
Category)This field can retrieve the detailed information of the category to which the article belongs. If you need to display the category name or link, you can define it as a variable and then access its properties, or use it directly.categoryDetailLabel acquisition. Example:{% archiveDetail currentCategory with name="Category" %}<a href="{{ currentCategory.Link }}">{{ currentCategory.Title }}</a>{% endarchiveDetail %}Tag label (
tagList): The Tag label of the article is usually notarchiveDetaila direct attribute, but obtained throughtagListthe label. You canarchiveDetailThe context in which it is calledtagList, and specifyitemIdIs the ID of the current article. Example:{% tagList tags with itemId=archive.Id %}{% for tag in tags %}<a href="{{ tag.Link }}">{{ tag.Title }}</a>{% endfor %}{% endtagList %}Custom field (
archiveParamsOr directly by name to get): Security CMS supports defining additional custom fields for content models. These fields can be accessed in the template in two ways:- Directly by the field nameIf the calling field name of the custom field is
authorthen you can{% archiveDetail with name="author" %}. - loop through all custom fields: Use
archiveParamsLabel to iterate over all custom fields, which is very useful when the field names are uncertain or need to be displayed uniformly.{% archiveParams params %}{% for item in params %}<span>{{ item.Name }}:{{ item.Value }}</span>{% endfor %}{% endarchiveParams %}
- Directly by the field nameIf the calling field name of the custom field is
Step by step to build the article detail page
Understood the core tags, we can then follow the following steps to build a functional article detail page:
Confirm the template file locationFirst, you need to find or create your article detail page template file. For example, if it is an article model, it is usually in
template/你的模板名称/archive/detail.html.Get and display basic article informationIn
detail.htmlIn the document, you can first place the article title and main content.<h1>{% archiveDetail with name="Title" %}</h1> <div class="article-content"> {% archiveDetail articleContent with name="Content" %}{{ articleContent|safe }}{% endarchiveDetail %} </div>Display auxiliary informationNext, add auxiliary information such as the publication date, views, category, and Tag tags.
<div class="article-meta"> <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span> <span>浏览量:{% archiveDetail with name="Views" %}</span> <span>所属分类: {% archiveDetail currentCategory with name="Category" %}<a href="{{ currentCategory.Link }}">{{ currentCategory.Title }}</a>{% endarchiveDetail %} </span> <span>标签: {% tagList tags with itemId=archive.Id %}{% for tag in tags %}<a href="{{ tag.Link }}">{{ tag.Title }}</a>{% endfor %}{% endtagList %} </span> </div>(Note:)
archive.CreatedTimeHere are thearchiveis a hypothetical variable name, if you have not passedwiththe parameter willarchiveDetailassign the result toarchive