As an experienced CMS website operation personnel of an enterprise, I know it is crucial to flexibly control the presentation form of content in content management.Especially for content such as Markdown, mathematical formulas, and flowcharts that require specific rendering capabilities, we often hope to enable them on specific pages or content models, rather than globally enforced or disabled.The Anqi CMS provides a powerful template engine and content management mechanism, enabling us to accurately achieve this goal.

Core Strategy: Content Tagging and Template Control

AnQi CMS achieves precise control over Markdown, formulas, and flowcharts, with its core lying in two levels: first, ensuring that content is entered in the required format on the backend;The second, and most critical, is to conditionally render and load external libraries at the front-end template level based on content or page characteristics.Although the enablement of the Anqi CMS Markdown editor is a global setting, we can still achieve the purpose of selective enablement through fine-grained control of the frontend template.

Step 1: Ensure Backend Content Creation Ability

Firstly, we need to ensure that the AnQi CMS content editor supports Markdown format input. This is usually done through the global settings in the background.

Navigate to "Global Settings" in the AnQi CMS backend and then select "Content Settings".Here, you will find an option to enable or disable the Markdown editor.Please make sure to set this option to 'Enable Markdown Editor'.The purpose of this setting is to allow content creators to use Markdown syntax while editing the "Content" field of articles, products, single pages, etc., including embedded mathematical formulas (LaTeX syntax) and flowcharts (Mermaid syntax).

It should be clearly stated that this global setting merely determines the input formats supported by the content editor.It does not directly control the rendering method of the front-end page, that is, whether the content will be parsed as HTML, and whether the related mathematical formula and flowchart rendering libraries will be loaded.This lays the foundation for our subsequent refined control.

Step two: accurately identify content requirements

In order to achieve precise control on a specific page or content model, we need a mechanism to identify which content requires special rendering.

An effective method is to use the 'Content Model' function of Anqi CMS.If you want articles in a content model (such as the "Technical Article" model) to generally support Markdown, formulas, and flowcharts, while other models (such as the "News" model) do not require this, you can add a boolean field to the custom fields of the content model, for example named "EnableAdvancedMarkdown" or "EnableAdvancedRendering".The content editor can check this field when publishing an article to clearly mark that the content requires advanced rendering.

For a single page or a specific category, you can also create dedicated templates for them or add a "document template" field in the content model (such ashelp-content-archive.mdAs mentioned above, and specify a template that requires advanced rendering to identify its requirements. For example, create aarticle/tech_detail.htmltemplate specifically designed for technical article detail pages.

Step 3: Use the template for refined control

Once the content is entered in Markdown format and we have a mechanism to identify which content requires advanced rendering, the next critical step is to perform conditional rendering and load external libraries in the frontend template.

Content rendering condition control

Template tags of Anqi CMS, such asarchiveDetail/categoryDetail/pageDetailetc., when calling the contentContentfield, it provided arenderparameter (refer totag-/anqiapi-archive/142.htmlThis parameter allows us to explicitly control whether Markdown formatted content is converted to HTML.

  • Whenrender=trueWhen, the system will parse and render Markdown content to HTML.
  • Whenrender=falseWhen, the system will not convert Markdown content, but output the original content instead (this is usually not what we want to display directly on the front end).

Therefore, in your detail page template (such asarticle/detail.html/product/detail.htmlorpage/detail.html), you can combine the recognition mechanism from the second step to deciderenderThe value of the parameter.

{# 示例:根据内容模型ID或自定义字段决定是否渲染Markdown #}
{%- if archive.ModuleId == 1 %} {# 假设模型ID为1的文章模型需要Markdown渲染 #}
    {%- archiveDetail articleContent with name="Content" render=true %}
{%- else %} {# 其他模型则不进行Markdown渲染,直接输出 #}
    {%- archiveDetail articleContent with name="Content" render=false %}
{%- endif %}

{{- articleContent|safe }} {# 确保HTML内容安全输出 #}

If you add a content model namedEnableAdvancedMarkdownThe custom field is used to mark whether advanced rendering is needed, the template code can be implemented as follows:

{%- archiveDetail currentArchive with name="Id" %} {# 获取当前文档的ID,以便调用其自定义字段 #}
{%- archiveParams params with id=currentArchive %} {# 获取当前文档的所有自定义参数 #}

{%- set enableMarkdown = false %}
{%- for item in params %}
    {%- if item.FieldName == 'EnableAdvancedMarkdown' and item.Value == 'true' %} {# 检查自定义字段值 #}
        {%- set enableMarkdown = true %}
    {%- endif %}
{%- endfor %}

{%- if enableMarkdown %}
    {%- archiveDetail contentHtml with name="Content" render=true %}
{%- else %}
    {%- archiveDetail contentHtml with name="Content" render=false %}
{%- endif %}

{{- contentHtml|safe }}

Lazy loading of external libraries (formulas and flowcharts)

The rendering of mathematical formulas and flowcharts usually depends onMathJaxandMermaidsuch JavaScript libraries ashelp-markdown.mdas described in them). These CDN import codes are placed by default inbase.htmlThis will cause them to be loaded on all pages, even if they are not needed. In order to implement on-demand loading