As a senior website operations expert, I am well aware that how to flexibly and accurately display content in a content management system is the key to operation success.Today, let's delve into a common but often confusing issue in AnQiCMS (AnQiCMS): Can the category details label display the complete content (Content) of the category?And how to operate to ensure correct rendering?
The ability to display category details and category content
The answer is yes, Anqi CMS's "category details label" (categoryDetailIt can fully display the complete content of the category. In AnQi CMS, categories are not only containers for classifying articles or products, but they can also have rich content, such as detailed introductions to product series, in-depth discussions of knowledge fields, or descriptive texts for specific service categories.
To call the full content of the category, you need to use the template{% categoryDetail with name="Content" %}This label. Where,name="Content"Make sure to tell the system that you need to retrieve the "content" field of the current category.This 'content' field is the text, images, formatting, and all other information you enter in the rich text editor when editing categories in the background.AnQi CMS provides a powerful rich text editor, allowing you to add and edit a variety of content for categories, therefore, theoretically, any content entered through this editor can be presented through this tag.
Ensure that the classification content is rendered correctly.
It is not enough to simply call the content out, especially when the content contains rich formatting, such as Markdown syntax, or directly includes HTML code. How to ensure that they are correctly parsed and displayed on the front page, rather than showing the source code unchanged, is particularly important. This involves two core points:Backend Content SettingsandUsage of Front-end Template Tags.
1. Backend Content Settings: Enable and Disable Markdown Editor
The AnqiCMS is affected by the Markdown editor switch in the "Content Settings" under the "Global Settings" when processing rich text content.
If the Markdown editor is enabledWhen you enter content for categories in the background, if you are accustomed to using Markdown syntax, the system will automatically try to convert the Markdown formatted content to HTML by default, so that it can be displayed correctly on the front-end page.This is a convenient feature that allows you to get beautifully formatted page effects without manually writing HTML.
If the Markdown editor is not enabledAt this time, the content you enter in the rich text editor (whether it is plain text, Markdown, or direct HTML) will tend to be treated as ordinary plain text or pre-formatted HTML. This means that if you enter Markdown, the system will not automatically parse it into HTML; if you enter HTML, it will be output as is, but the default security mechanism of the front-end template may escape the HTML tags, resulting in the displayed content being
<p>这是一个段落</p>Such a style, not a real paragraph.
Therefore, when editing category content, you need to be clear about your content format and the backend configuration.
2. Front-end template tags:renderThe parameter withsafeFilter
To better control the rendering of classified content, Anqi CMS'scategoryDetailTags providedrenderParameter, at the same time, the Go template engine (AnQiCMS uses a template engine similar to Django syntax) also provides|safefilter.
renderParameter:- When your categorized content isMarkdown formatand you want it to be parsed as HTML, you can
categoryDetailthe tag withrender=true. Even if the Markdown editor is not globally enabled on the backend, this parameter can force the current content to be converted from Markdown to HTML. For example:{% categoryDetail categoryContent with name="Content" render=true %}. - If your categorized content isplain text or already contains complete HTML codeWhile you do not want the system to parse it as Markdown (this might be because the content is actually HTML, or you are not using Markdown), you can use
render=false. For example:{% categoryDetail categoryContent with name="Content" render=false %}.
- When your categorized content isMarkdown formatand you want it to be parsed as HTML, you can
|safeFilter: No matter if your content is HTML converted from Markdown or raw HTML inputted directly in the backend, the template engine, for security considerations, defaults to escaping the output HTML tags to prevent cross-site scripting (XSS) attacks. This means<p>May be displayed as<p>. To make the browser correctly parse and display these HTML tags, you must add them to the end of the output content.|safefilter. For example:{{ categoryContent|safe }}.Combine both to ensure **rendering effect:
The most common and recommended practice is to use when the category content may contain HTML (whether converted from Markdown or manually entered), you should use both
render=trueAnd if the content is Markdown|safefor example:{% categoryDetail categoryContent with name="Content" render=true %} <div> {{ categoryContent|safe }} </div>Or if your categorized content is clean HTML, you can also just use
|safe:{% categoryDetail categoryContent with name="Content" %} <div> {{ categoryContent|safe }} </div>Please note,
|safeThe filter tells the template engine 'I trust this content, please output HTML as is', therefore, when using it, please ensure that the source of the content is safe and reliable to avoid potential security risks.
In summary, it is about Anqi CMS'scategoryDetailLabel combinationrenderParameters and|safeThe filter provides great flexibility, whether it is rich text in Markdown format or complex layouts with custom HTML code, it can be rendered accurately on the front page.Understanding these mechanisms will help you better utilize the powerful content management capabilities of Anqin CMS to create a more outstanding user experience for your website.
Frequently Asked Questions (FAQ)
Why is my category content displayed as a bunch of strange symbols or tags on the front end instead of a well-formatted page?This is usually because your category content includes HTML or Markdown syntax, but the template engine has not parsed it correctly or escaped it securely. You need to check two points: first, whether the Markdown editor is enabled in the background "Global Settings" > "Content Settings" (if your content is Markdown); second, whether you have used it when calling the category content in the front-end template. Please check the following two points: first, whether the Markdown editor is enabled in the "Global Settings" > "Content Settings" (if your content is Markdown); second, whether you have used it when calling the category content in the front-end template. Ensure that the Markdown editor is enabled in the "Global Settings" > "Content Settings" (if your content is Markdown) and that you have used it correctly when calling the category content in the front-end template. You need to verify the following: first, whether the Markdown editor is enabled in the "Global Settings" > "Content Settings" (if your content is Markdown); second, whether you have used it when calling the category content in the front-end template.
|safeFilter, and whether it adds parameters for Markdown content,render=truefor example, should use{{ categoryContent|safe }}or{% categoryDetail content with name="Content" render=true %}{{ content|safe }}.If I want some category content to use Markdown, and some to use HTML directly, how should I handle it?AnQi CMS provides flexible solutions. You can decide whether to enable Markdown editor by default in the "Global Settings" -> "Content Settings" in the background according to the main content type you have.Then, in the front-end template, for all categories, it is unified to use
render=trueand|safe. If the content is Markdown, it will be parsed;If the content is itself HTML (but not Markdown format), it will be output as HTML.This method can be maximally compatible with different content types. If some categorized content is plain text and no processing is desired, you can userender=falsecooperate|safe.Use
|safeWhat security risks does the filter bring?|safeThe filter's role is to inform the template engine that you trust this content is safe HTML and can be output directly without escaping. This means that if this content is submitted by an untrusted third party (such as user comments, messages, etc.), and it contains malicious scripts (such as<script>alert('XSS');</script>These scripts will be executed on the page, resulting in cross-site scripting (XSS) vulnerabilities. Therefore, be cautious when calling user-generated content.|safeFilter, or ensure that the content has been strictly filtered and sanitized before being stored in the database. For category content edited by administrators in the background, since the source is controllable, use|safeIt is usually safe.