As an experienced website operation expert, I am also a deep user of AnQi CMS, I am glad to have an in-depth discussion with everyone{% diy %}The ability of the tag in handling rich text content rendering.In the flexible and powerful template system of AnQi CMS, understanding the characteristics of each tag is crucial for achieving accurate and efficient content display.
Of Security CMS{% diy %}Label: Does custom content support deep parsing of rich text rendering?
The Anqi CMS, with its simple and efficient features and highly customizable characteristics, has become a powerful assistant for many small and medium-sized enterprises and content operation teams.In the operation of daily websites, we often need to add some customized, global content, such as copyright information in the footer, contact information in the sidebar, or some special marketing slogans.{% diy %}The label has become our preferred tool.But a common question that arises is: Can this tag directly support content created from a rich text editor and render it as the article details do?
{% diy %}The core positioning and function of the label
First, let's review{% diy %}The core positioning of tags in AnQi CMS. Based on our in-depth understanding of AnQi CMS template tags,{% diy %}The primary function of the label (i.e., "custom content label") is to obtain various custom parameters configured in the "custom settings" module of the background. It is a powerful "universal getter" that can retrieve values based on the custom fields you set up in the background.nameParameters, extract the corresponding values. These values are usually text information such as website names, phone numbers, social media links, etc., which are relatively flat and have a simple structure.
For example, if you add a parameter namedAuthorwith the value of “AnQi CMS Operation Team”, you can easily pass through it in the template{% diy with name="Author" %}To fetch and display this text. Its basic usage, whether it is to fetch simple text or URL, always follows the direct string retrieval logic.
The conventional way to handle rich text content in AnQi CMS
However, rich text content, such as the main text of articles entered through the background rich text editor, product descriptions, etc., includes HTML structure and may even be in Markdown format.AnQi CMS has a special rendering mechanism for these core content fields.
For example, when you go through the document detail page,{% archiveDetail with name="Content" %}When retrieving the article content, if the Markdown editor is enabled on the backend, the system will automatically convert Markdown to HTML. In addition, it also supportsrenderParameter, allows you to manually control whether to perform Markdown to HTML conversion(render=true|false), as well aslazy="data-src"Such properties are used to handle advanced functions like lazy loading of images. Moreover, to avoid the browser displaying any output that may contain HTML code as plain text, we usually need to use it in conjunction with|safeA filter that tells the template engine that this content is safe HTML and can be directly parsed by the browser.
{% diy %}The truth about tags and rich text content
Now, let's go back to{% diy %}Label. By consulting the documentation and actual operating experience, we can clearly draw a conclusion: {% diy %}The tag itself does not have the built-in feature of automatically rendering rich text editor content, it does not automatically convert Markdown to HTML, nor does itrenderThis parameter is provided for direct use.
{% diy %}The original intention of the tag is to obtain the original string value stored in the background custom settings. This means that when you use{% diy %}When a label retrieves the value of a custom field from the backend, it will output any content you enter in the backend unchanged.
If your custom field accidentally contains HTML or Markdown formatted text, then use it directly in the template{% diy %}You will see the raw code uninterpreted, not the expected rendering effect.
To solve this problem, we need to intervene manually:
For custom content that includes HTML code:You need to handle it in the template as you would with core content fields:
{% diy %}Apply the output of the tags to|safeFilter. This will inform the template engine that the output string is safe HTML that can be parsed by the browser.{# 假设后台某个名为"FooterRichText"的diy字段中存储了HTML内容 #} <div> {% diy footerInfo with name="FooterRichText" %} {{ footerInfo|safe }} </div>For custom content that includes Markdown formatting:due to
{% diy %}It does not automatically convert Markdown, you need to apply it first|renderThe filter converts it to HTML first, then use|safeThe filter ensures that HTML is rendered correctly.{# 假设后台某个名为"AboutUsContent"的diy字段中存储了Markdown内容 #} <div> {% diy aboutUsMarkdown with name="AboutUsContent" %} {{ aboutUsMarkdown|render|safe }} </div>Please noteHere is the
|renderThe filter is a general-purpose content rendering filter (such asfilter-render.mdThe aforementioned can be applied to any string variable that may contain Markdown, not limited to core content fields.
Operation suggestions and **practice**
Based on the above analysis, as a website operator, we should choose an appropriate management method according to the nature of the content:
- Simple, non-formatted text or links:Use the background custom settings preference
{% diy %}Tags. This is more lightweight and convenient to manage. - Need structured and formatted rich text content (such as articles, product introductions, independent 'About Us' pages, etc.):It is strongly recommended to create and edit through a content model (article, product) or single-page management.These modules are built-in with rich text editors and provide comprehensive Markdown conversion and HTML parsing support during template rendering.
By properly distinguishing and using the various functions of the Anqi CMS, you will be able to manage website content more efficiently and ensure the correct rendering and user experience.
Frequently Asked Questions (FAQ)
Q:
{% diy %}What types of custom content can the tag retrieve?A:{% diy %}Tags are mainly used to obtain unstructured or semi-structured data configured in the "Custom Settings" of the background, such as plain text, numbers, brief descriptions, URLs, switch states, and so on.It is usually in a simple key-value pair form.Q: Why do I use
{% diy %}The HTML content output by the tag is displayed as raw code on the page instead of the rendered effect?A:{% diy %}The tag defaults to outputting the original string and does not automatically parse HTML. If you enter HTML code in a custom field, you need to add the output result in the template.|safeFilter, for example{{ diyField|safe }}This way the browser can identify and render it as HTML.**Q: If I want to in
{% diy %}