In AnQiCMS (AnQiCMS) daily content operation, we usually hope that the system can automatically render Markdown format content into beautiful HTML for direct display on the website.However, in certain specific scenarios, such as when you may need to export the original Markdown to other platforms, or display Markdown code blocks directly on the page (for example, in tutorials or technical articles), or provide pure Markdown data for API interfaces, it is particularly important for AnQiCMS to display the original Markdown text rather than the rendered HTML.

AnQiCMS provides flexible control options, allowing you to meet this need at the content output level.

Understand the default rendering mechanism of AnQiCMS

Firstly, we need to understand the basic logic of AnQiCMS handling Markdown content.When you go to the "Global Settings" in the background, enter the "Content Settings" and enable the Markdown editor, the system will automatically parse and render the Markdown content edited by the editor into HTML by default when it is output on the front-end page.This is convenient and efficient for most content display scenarios.

But if you want to override this default behavior at a specific display location or for specific content, AnQiCMS allows you to fine-tune control through template tag parameters.

Control the rendering behavior of Markdown in the template

To implement displaying Markdown original text instead of rendering it as HTML, the core lies in modifying your website template file. AnQiCMS handles the core content types such as 'Document Details', 'Category Details', and 'Single Page Details'.Content(Content) field, all providedrenderparameter to control whether Markdown to HTML conversion is performed.

Specifically, when you call content in the template,ContentField when, by puttingrenderthe parameter tofalseindicating the system does not perform Markdown rendering.

Let us take the most common document content as an example to explain in detail:

  1. Locate the template file to be modified:AnQiCMS template files are usually stored in the root directory of your system./templateFolder. For example, the document detail page may be located in{模型table}/detail.html, the category list page may be located at{模型table}/list.html, while the single page detail page may be located atpage/detail.htmlEtc. You can edit or download the template file for modification through the "Template Design" feature of the AnQiCMS background.

  2. Tag call for modifying content:In the template, you will find similar{% archiveDetail archiveContent with name="Content" %}{{archiveContent|safe}}content tag calls. Here,archiveContentis the variable name you specify for the content field,{{archiveContent|safe}}and is responsible for safely outputting the content obtained to the page.

    To make the system display the original Markdown, you just need to inarchiveDetail(orcategoryDetail/pageDetailtag, forname="Content"call to addrender=falsethis parameter. The modified code will look like this:

    {# 显示Markdown原文,不进行渲染 #}
    {% archiveDetail archiveContent with name="Content" render=false %}{{archiveContent}}
    

    Or if you are accessing directlyarchive.Contentsuch variables to retrieve content (you can usually directly access the properties of the current document on the document detail page), and you can alsoContentfield.render=falseparameters:

    {# 同样显示Markdown原文,适用于直接访问文档对象的Content属性 #}
    {{archive.Content|render:false}}
    

    It is noteworthy that when you setrender=falseWhen, if your content field contains Markdown syntax, the system will directly output these Markdown texts. At this point, if your Markdown text itself contains HTML tags (such as<div>/<p>And), if you want these HTML tags to be parsed by the browser as normal rather than plain text, then you still need to add the closing brace at the end of the output variable.|safeFilter, that is{{archiveContent|safe}}But for the pure Markdown original text,|safeit mainly prevents<>and other characters from being escaped.

Application scenario example

Assuming you are building a technical blog, you need to display Markdown code snippets in the article, rather than rendering them as HTML.You can directly paste Markdown code in the text box of the document content (for example, using three backticks to enclose code blocks).Then in the corresponding article detail templatearticle/detail.htmlIn it, the tag of the article content will be modified to:

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

This way, the Markdown code block in the article will be displayed in its original Markdown syntax on the page, and will not be rendered as code highlighting or other HTML structures.If you wish to implement Markdown rendering on the front-end (for example, using a JavaScript library), then after obtaining the Markdown source, you can handle it flexibly.

By controlling at this template level, you can decide flexibly how to display website content, whether it is automatic rendering or displaying the original text, it is all in your hands.

Frequently Asked Questions (FAQ)

1. Can I make part of the same document display as rendered HTML and the other part as the original Markdown text?

This requires more complex processing.render=falseThe parameter acts on the entireContentfield. AnQiCMS itself does not have built-in features to convert aContentField split into part rendering and part original output. If you have such a need, the usual solution is:

  • Split content field:Create two or more content fields in the content model, for exampleRenderedContentandRawMarkdownContent. When publishing content in the background, the parts that need to be rendered will be placed inRenderedContent, and the parts that need the original text will be placed inRawMarkdownContentThen call them separately in the templateRawMarkdownContentWhen calling the fieldrender=false.
  • Front-end JS rendering:Always get the Markdown original (usingrender=falseThen use a front-end JavaScript library (such asmarked.jsorshowdown.js) to process the part that needs to be rendered on the browser side.

2. Disable the Markdown editor in the background "Global Settings -> Content Settings" and its use in templatesrender=falseWhat is the difference?

Disable the background Markdown editor is a global setting, which means when editing content in the background,ContentThe field will act as a plain text area, no longer providing Markdown editor functionality, and the system will no longer attempt toContentThe content of the field is parsed as HTML. This will affect all pages using the content model.

Andrender=falseIt is a local control at the template level. Even though the Markdown editor is enabled in the background, and the content is written in Markdown syntax,render=falseAlso, it will force the original Markdown to be displayed when outputting in the current template.This approach is more flexible, allowing you to keep Markdown rendering on most pages, but obtain the original text in specific scenarios (such as tutorial pages, API interfaces).

3. If my content field stores not Markdown, but pure HTML code, userender=falseWhat will be the impact?

If the content field stores pure HTML code (not Markdown), userender=falseThe parameter will have no negative impact. Because it will only tell AnQiCMS 'do not try to parse Markdown', and HTML itself is not Markdown.Therefore, the system will directly output the pure HTML code you have stored.In this case, ensure that you use{{yourVariable|safe}}Come