As an expert who has been deeply involved in website operations for many years, I am well aware of the intricacies of content presentation, especially in content management systems. How to flexibly and efficiently handle different formats of content is the key to improving website user experience and operational efficiency. Today, let's discuss a common question about the content presentation of AnQiCMS (AnQiCMS):categoryListLabel outputitem.ContentDoes the field support automatic conversion of Markdown format content?

categoryListin the labelitem.ContentField and Markdown conversion mechanism parsing

In AnQi CMS,categoryListTags are a very practical feature that allows us to iterate through and display the classification information of a website, including the names, links, and descriptions of the categories. Among them,item.ContentThe field is usually used to store detailed introduction or content of the category.Many operators, after using Markdown to write categorized content, naturally expect that this content can be automatically rendered into a beautiful HTML format on the front-end page.

However, according to the design philosophy of Anqi CMS and the conventions of related template tags,categoryListtags, by default, theiritem.Contentfields output isunprocessed original text contentThis means that if you use Markdown syntax while editing category content, throughcategoryListtags are output directly.item.ContentIt will not automatically parse it as HTML, but will display the Markdown source code unchanged. This is similar toarchiveDetail/categoryDetailorpageDetailTags used to display individual content details are different, these details tags are usually enabled in the background after the Markdown editor is turned on, theirContentThe field can automatically convert Markdown to HTML.

Does this mean that we arecategoryListHow can you not enjoy the convenience and beauty that Markdown brings?Of course, the answer is no.Filterto solve this problem, letcategoryListThe Markdown content can also be perfectly presented.

Skillfully userenderFilter to implement Markdown conversion

The template engine of AnQi CMS supports various filters, among whichrenderThe filter is exactly designed to solve such problems. It can render content containing Markdown syntax and output it as HTML. CombinedsafeThe filter ensures that the output HTML structure can be correctly parsed and displayed by the browser, rather than as plain text.

The specific operation steps usually include two parts: first, ensure that the background Markdown editing function is enabled, and second, apply the correct filter to the field in the front-end template.item.ContentApply the correct filter to the field.

  1. Backend Settings ConfirmationBefore using the Markdown feature, make sure that the Markdown editor is enabled in the "Global Settings" -> "Content Settings" of the Anqicms background.This is the content that can be correctly identified as Markdown syntax basics.

  2. Front-end Template ModificationIn your template file (for example, for displaying a certain category list in your.htmlfile), when you usecategoryListto loop output the category information with the tag,item.ContentField processing as follows:

    {% categoryList categories with moduleId="1" parentId="0" %}
        {% for item in categories %}
        <div class="category-item">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            {# 原始输出,不会转换Markdown #}
            {# <p>{{ item.Content }}</p> #}
    
    
            {# 经过render过滤器处理后,Markdown内容将被转换为HTML #}
            <div class="category-description">
                {{ item.Content | render | safe }}
            </div>
        </div>
        {% endfor %}
    {% endcategoryList %}
    

    Here, | renderThe filter is responsible for convertingitem.ContentParse the Markdown syntax in it and convert it to HTML structure. Following the next| safeThe filter indicates to the template engine to output this content as safe HTML, avoiding the issue of HTML tags being displayed as plain text due to the default escaping mechanism.

WhycategoryListDon't automatically convert by default?

You might be curious, since the Markdown conversion feature exists, whycategoryListnotarchiveDetailautomatically do that? This is usually for performance and flexibility considerations.categoryListGenerally used to generate categorization lists, which may be very long anditem.ContentThe field may be used for various scenarios, such as brief categorization summaries (no Markdown required) or direct text input from the backend.If the default Markdown rendering is performed, it will increase the computational burden on the server when generating lists.renderFilter, Anqi CMS gives the choice to the template developer, you can flexibly decide whether to filter according to the content requirements of the specific page,item.ContentRender Markdown to optimize page loading performance and content presentation.

Conclusion

In summary, of Anqi CMS'scategoryListLabel outputitem.ContentfieldDoes not support automatic conversion from Markdown to HTML.. But as a senior website operation expert, we know that Anqi CMS provides powerfulrenderfilters, as well as coordinationsafeFilter that can perfectly realize the dynamic parsing and presentation of Markdown content.This design ensures the flexibility and performance of the system, as well as gives developers fine control over the presentation of content.| render | safe, your categorized content will shine with new vitality.


Frequently Asked Questions (FAQ)

Q1: WhycategoryListofitem.ContentNot by default to automatically convert Markdown, butarchiveDetailtags can be? A1:This is mainly based on design considerations and usage scenarios.archiveDetail/categoryDetailThe "Details" tag is usually used to display a single, complete article or category details page, and its content is likely to contain complex formatting, therefore the default Markdown conversion can improve development efficiency. AndcategoryListUsed for looping to output the list, the list in theitem.ContentMay be used as a brief summary or plain text description. Forcing Markdown rendering on all list items may increase unnecessary system resource consumption. By providingrenderFilter, Anqi CMS allows developers to selectively convert based on specific requirements, thereby optimizing performance and flexibility.

Q2: Besidesitem.ContentIn AnQi CMS, what fields may need to be manually used?|render|safePerform Markdown conversion? A2:Any field that is entered through a Markdown editor in the background but still displays the original Markdown syntax when directly outputted in the front-end template may need to be used|render|safeFilter.This includes but is not limited to long text fields of custom content models, specific parts of single-page content, and rich text content output from certain custom tags, etc.The basis for judgment is: If you expect the Markdown syntax of a field to be parsed as HTML, but the actual output is raw Markdown text, then you should try using these two filters.

Q3: If I forget to add{{ item.Content | render }}after|safeWhat will happen to the filter? A3:If you have used|renderthe filter to convert Markdown to HTML, but forget to add|safeA filter, then the converted HTML tag (for example<p>/<strong>/<ul>The text within the brackets will not be parsed by the browser but will be displayed as plain text on the page.This is because the template engine of AnQi CMS defaults to encode all output content as HTML entities (i.e., escaping) for security reasons to prevent XSS attacks.|safeThe filter explicitly tells the template engine that this content is safe HTML, and it does not need to be escaped. It can be output directly.