How to display the title and content of articles in AnQiCMS templates?

Calendar 👁️ 69

Managing website content in AnQiCMS, the ultimate purpose is to be able to present these contents elegantly and efficiently to visitors.For the most common content type of articles, how to accurately display the title and body in the front-end template is a basic skill that every website operator needs to master.AnQiCMS powerful template function, providing us with flexible and intuitive implementation methods.

Core: RecognitionarchiveDetailTag

The AnQiCMS template system is designed very user-friendly, it provides a special function for getting detailed data of a single articlearchiveDetailLabel. When you visit an article detail page, this label will intelligently recognize the article on the current page and allow you to extract the required information from it.

Its basic usage form is as follows:{% archiveDetail 变量名称 with name="字段名称" %}Of course, if you just want to output a field directly, you can omit it.变量名称For example,{% archiveDetail with name="Title" %}It will directly output the title of the current article.

How to display the article title

The article title is undoubtedly the core element of the page, displaying it is very simple in the AnQiCMS template.archiveDetailthe label provides a namedTitleThe field is specifically used to retrieve the article title.

In the template of the article detail page (usually){模型table}/detail.htmlIn the parenthesis, you can display the article title like this:

<h1>{% archiveDetail with name="Title" %}</h1>

This line of code will directly insert the title of the current article into<h1>in the tag.

You may also find that on the article detail page, you can use it directly{{archive.Title}}The title can also be displayed. This is because in the context environment of the article detail page, AnQiCMS will automatically encapsulate the complete data of the current article into a name calledarchiveThe global variable is. Therefore,{{archive.Title}}and{% archiveDetail with name="Title" %}Can all achieve the purpose of displaying the title. Usually, in order to be concise and directly access the properties of the current document, people tend to use{{archive.Title}}this form.

How to display article content

The article content is the main part of the page, containing detailed information, images, layout, etc. In AnQiCMS, the content is stored inContentthe field.

We can use it to display article contentarchiveDetaillabel'sContentField:

<div>{% archiveDetail with name="Content" %}</div>

However, simply outputting it directly may encounter a problem: AnQiCMS defaults to escaping all HTML tags output from templates to prevent potential XSS attacks.This means that if you use bold, images, links, and other HTML tags in the article content, they will be displayed as plain text, rather than being parsed by the browser into the intended styles.

To render HTML content correctly, we need to use a special filtersafe.safeThe filter's role is to inform the template engine that this content is 'safe', and it does not need to be HTML-escaped. It can be output directly. Therefore, the recommended way to display article content is:

<div class="article-content">
    {{ archive.Content|safe }}
</div>

Here we directly usedarchiveGlobal variables, and through|safeThe filter ensures that HTML content can be parsed correctly. Please remember to do so.|safeThe importance of the filter, as it directly affects the presentation of the article layout.

Additionally, if the content of your article is written using a Markdown editor and you want to render it into HTML on the front end, you canarchiveDetailthe tag withrender=trueparameters:

<div class="article-content">
    {% archiveDetail articleContent with name="Content" render=true %}{{ articleContent|safe }}
</div>

This, Markdown syntax will be correctly parsed into HTML and displayed. If you need to optimize lazy loading of images in the content, you can combinelazy="data-src"The parameter allows the template to automatically replace the image URL in outputsrcProperty, to cooperate with the front-end lazy loading script.

Comprehensive Application Example

Combine the title and content, usually a template snippet of an article detail page will contain the following similar code structure, and also display other basic information of the article, such as the publication time, category and page views:

<article class="article-detail">
    <h1 class="article-title">{{ archive.Title }}</h1>
    <div class="article-meta">
        <span class="category">分类:<a href="{{ archive.Category.Link }}">{{ archive.Category.Title }}</a></span>
        <span class="publish-date">发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
        <span class="views">阅读量:{{ archive.Views }}</span>
    </div>
    <div class="article-content">
        {{ archive.Content|safe }}
    </div>
</article>

In this example, we directly usedarchiveglobal variables to access the various fields of the article, and bystampToDatetags to format the timestamp into a readable date,archive.CategoryThis is an object that can be directly accessed to view the category links and titles. This method is concise and clear, and can efficiently build the article detail page.

Through these flexible tags and filters, AnQiCMS makes the display of website content intuitive and powerful, whether you want to show simple articles or complex product details, you can do it with ease.


Frequently Asked Questions (FAQ)

Q1: Why do I only write{{archive.Title}}It can also display the title instead of having to use{% archiveDetail with name="Title" %}?

A1: On the AnQiCMS article detail page, the system will automatically encapsulate all the data of the current article into a global variable namedarchive. Therefore, you can directly access{{archive.Title}}In this way to access the article title, this method is usually more concise.{% archiveDetail with name="Title" %}Tags are more used on non-article detail pages (such as the homepage, list page) when you need to retrieve specific article information based on a specified ID or alias, or when you want to use tag parameters (such asrender/lazyWhen making more fine-grained controls. But in the context of the article detail page, both can effectively display the title.

Q2:|safeWhat is the function of the filter? Is it safe to use it?

A2:|safeThe filter is an important feature of the AnQiCMS template engine, its role is to inform the template engine that the processed string content is safe HTML code and does not require default HTML entity escaping. This means that if you include such as<b>/<p>/<img>Use HTML tags,|safeAfter the filter, these tags will be correctly parsed and rendered by the browser, rather than displayed as plain text.

Regarding security,|safeThe filter itself does not verify the safety of HTML content.It is just 'trust' that the content you provide is safe. If the content of your article comes from untrusted user input (such as users directly inputting HTML in the comments), and it has not been strictly filtered for XSS (cross-site scripting attacks), then use it directly|safeIt could lead to a security vulnerability. Therefore, before using|safeMake sure the source of your content is reliable or that the content has been thoroughly filtered for security before saving it to the database.

Q3: How can I display the summary of an article instead of the full content?

A3: AnQiCMS provides a 'Document Summary' field in the article management, which is usually used to display the article abstract. In the template, you can use the{{archive.Description}}To retrieve and display this summary content. If the 'Document Summary' field is empty, the system will usually automatically extract the first 150 characters from the article content as a summary.Moreover, if you need more flexibility in extracting article content as a summary, you can use the filters provided by the template, for example{{archive.Content|truncatewords:50}}You can extract the first 50 words of the article content as a summary and automatically add an ellipsis at the end.

Related articles

How to get the string in AnQiCMS template?

In AnQiCMS template development, text and strings are the foundation for building website content.Flexible and efficient string retrieval and manipulation are skills that template developers must master, whether it is to display article titles, website names, or process user input.AnQiCMS's powerful template engine is based on Go language, providing rich tags and filters, making string acquisition and processing intuitive and practical.### Core Mechanism: Understanding the String Retrieval Method of AnQiCMS Template The most direct way to retrieve strings in AnQiCMS templates is through variable references

2025-11-08

What is the type of the string array after splitting the `fields` filter in AnQiCMS template?

In AnQiCMS template development, we often need to process text content, such as splitting a string of keywords, tags, or descriptions into independent entries for list display or further operations.At this time, the `fields` filter has become a very practical tool.It can efficiently complete the splitting of strings, but many users may be curious about the data types of the split strings after the `fields` filter is applied.Understand the working mechanism of the `fields` filter

2025-11-08

How to split a long string into an array of strings in AnQiCMS template?

During AnQiCMS template development, we often encounter situations where we need to flexibly handle data, one common requirement being to split a long string containing multiple pieces of information into an independent string array using a specific delimiter (such as a space), so as to facilitate loop display or further operations.AnQiCMS's template engine provides a concise and efficient filter (Filter) function, which can easily achieve this goal.### Understand `split`

2025-11-08

How to use the `autoescape` tag to control the automatic escaping of HTML content in AnQiCMS templates?

It is crucial to understand and effectively control the escaping behavior of HTML content during AnQiCMS template development.This not only concerns the correct display of page content, but also directly affects the security of the website, especially in preventing cross-site scripting (XSS) attacks.AnQiCMS's template system provides a flexible mechanism to manage this, with the `autoescape` tag playing a core role.

2025-11-08

How to implement personalized display of front-end content in AnQiCMS?

In today's era of content overload on the internet, how to make a website stand out and provide a unique and valuable experience to visitors has become crucial.The personalized display of content is an important means to enhance user stickiness, conversion rate, and brand image.AnQiCMS as an efficient and customizable content management system provides many features to help us easily achieve this goal.The flexible construction of content models: the foundation of personalized display The first step in personalized display is to make our content inherently personalized.The flexible content model feature of AnQiCMS

2025-11-08

What template file extensions and storage locations does AnQiCMS support?

AnQiCMS as a flexible and efficient content management system provides powerful template customization capabilities.It is crucial to make full use of this advantage of AnQiCMS and understand the suffix and storage location of its template files.This can not only help you design websites more efficiently, but also locate and solve problems faster when encountering them.First, AnQiCMS template files use the `.html` suffix.This means that all template files you create or edit should be saved in HTML format. These`

2025-11-08

How to use AnQiCMS built-in tags to control content display logic?

In AnQiCMS, the built-in tag system is the core tool you use to control the logic of displaying website content.It provides a powerful and flexible syntax that allows you to control the acquisition, filtering, sorting, formatting, and final layout of content in template files without writing complex backend code.This system is similar to the Django template engine syntax, easy to use, and can help you convert technical details into intuitive display effects.

2025-11-08

How does AnQiCMS adapt the display of website content for PC and mobile endpoints?

AnQiCMS: Providing seamless PC and mobile end adaptive display solutions for your website In today's digital age, it is common for users to access websites through various devices, from large computer screens to small smartphones, and the display effect of the website directly affects user experience and the efficiency of information transmission.A website that cannot be displayed friendliness on mobile devices will undoubtedly lose a large number of potential users and business opportunities.AnQiCMS fully understands this requirement and has provided flexible and diverse solutions to ensure that your website content is perfectly presented on both PC and mobile ends

2025-11-08