As an experienced website operations expert, I know that how to flexibly and accurately display content is the key to operational success.Today, let's delve into a common yet easily confusing issue in AnQiCMS: 'Can the category details tag display the full content (Content) of the category?'What are the steps to ensure correct rendering?

Category details label and display ability of category content

The answer is affirmative, the 'Category Details Label' of Anqi CMScategoryDetail) Can fully display the complete content of the category.In the Aqy CMS, categories are not only containers for classifying articles or products, but they can also contain rich content, such as detailed introductions of a product series, in-depth discussions of a knowledge field, or descriptive text for specific service categories.

To call the full content of the category, you need to use it in the template{% categoryDetail with name="Content" %}This label. Wherein,name="Content"Explicitly tell the system what you need to get is 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 diverse content for categories. Therefore, theoretically, any content entered through this editor can be presented through this tag.

The key operation to ensure the correct rendering of the classification content

仅仅将内容调用出来是不够的,尤其当这些内容包含丰富的格式,例如使用了Markdown语法,或者直接包含了HTML代码时,如何确保它们在前台页面上能够被正确地解析和显示,而不是原封不动地展示出源代码,就显得尤为重要。这涉及到两个核心点:Background content settingsandUsage of front-end template tags.

1. Background content settings: Enabling and disabling Markdown editor

The 'AnQi CMS' may be 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 attempt to convert this Markdown-formatted content to HTML by default, so that it can be displayed correctly on the frontend 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 enabled:At this time, the content you enter in the rich text editor (whether it is plain text, Markdown, or direct HTML) will be treated as plain text or preformatted HTML by the system. This means that if you enter Markdown, the system will not automatically parse it as 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 within it, resulting in the tags being displayed as plain text on the page.<p>这是一个段落</p>Such text, rather than a true paragraph.

Therefore, when editing category content, you need to be clear about the format of your content and the configuration of the backend.

2. Front-end Template Tags:renderParameters withsafeFilter

To more accurately control the rendering of classified content, the Anqi CMS ofcategoryDetailTags providerenderParameter, at the same time, the Go template engine (AnQiCMS uses a template engine similar to Django syntax) also provides|safeFilter.

  • renderParameters:

    • When your category content isMarkdown formatand you wish it to be parsed as HTML, you cancategoryDetailtag.render=true. Even if the Markdown editor is not globally enabled, this parameter can force the conversion of the current content from Markdown to HTML. For example:{% categoryDetail categoryContent with name="Content" render=true %}.
    • If your category content isplain text or already contains complete HTML codeWhile you do not want the system to perform any Markdown parsing on it (this may be because the content is already HTML, or you do not use Markdown), you can userender=false. For example:{% categoryDetail categoryContent with name="Content" render=false %}.
  • |safeFilter: No matter whether your content is HTML converted from Markdown or the original HTML input directly in the background, 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&lt;p&gt;. In order for the browser to correctly parse and display these HTML tags, you must add it after the variable containing the output content|safeFilter. For example:{{ categoryContent|safe }}.

    Combine the two to ensure the rendering effect:**

    The most common and recommended approach is that when the classified content may contain HTML (whether converted from Markdown or manually entered), you should use it at the same time.render=trueEnglish if the content is Markdown and|safeFilter, for example:

    {% categoryDetail categoryContent with name="Content" render=true %}
    <div>
        {{ categoryContent|safe }}
    </div>
    

    or, if your classified 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 the HTML as is', so please make sure that the source of the content is safe and reliable when using it to avoid potential security risks.

In summary, the Anqi CMS'scategoryDetailTag combinationrenderparameters with|safeThe filter provides you with great flexibility, whether it is rich text in Markdown format or complex layouts containing custom HTML code, they can be rendered accurately on the front page.Understanding these mechanisms will help you better utilize the powerful content management capabilities of AnQi CMS, creating an even better user experience for your website.


Common Questions and Answers (FAQ)

  1. Why is the classification content displayed on the front end as a bunch of strange symbols or tags, rather than a well-formatted page?This is usually because your classification content includes HTML or Markdown syntax, but the template engine did not parse it correctly or escape 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, when calling the classification content in the front-end template, did you use|safeFilter, and whether to add for Markdown contentrender=trueParameters. For example, it should use{{ categoryContent|safe }}or{% categoryDetail content with name="Content" render=true %}{{ content|safe }}.

  2. How can I handle some categories that should use Markdown and others that should use HTML directly?The CMS provides flexible solutions.You can decide whether to enable the Markdown editor by default in the "Global Settings" -> "Content Settings" on the backend, based on the main content type you choose.render=trueand|safe.Here, if the content is Markdown, it will be parsed; if the content itself is HTML (but not in Markdown format), it will be output as HTML.This way can achieve maximum compatibility with different content types.render=falsewith|safe.

  3. Use|safeWhat security risks does the filter present? |safeThe purpose of the filter 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 user (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, leading to cross-site scripting (XSS) vulnerabilities. Therefore, be cautious when using user-generated content.|safeFilter, or ensure that the content has been strictly filtered and sanitized before being stored in the database. For the classified content edited by administrators in the background, since the source is controllable, use|safeIt is usually safe.