Deep Analysis of AnQiCMSmoduleDetailTagsKeywordsField: Format and Template Parsing Techniques of Keywords

As an experienced website operations expert, I know that how to efficiently and flexibly handle and display content is the key to success in a content management system.AnQiCMS (AnQiCMS) provides us with great convenience with its simple and efficient architecture.moduleDetailTag returnsKeywordsWhat exactly is a field, a single keyword, or a comma-separated string? And how should it be correctly parsed and utilized in a template?

To put it straightforwardly, frommoduleDetailTag returnsKeywordsA field, as well as all fields involving the "keyword" in the Safe CMS (such as document keywords, tag keywords, etc.), is acomma-separated string.

This is highly consistent with the design philosophy of AnQi CMS in the background content management. When entering keywords in the background, the system explicitly requires users to useEnglish commas(,Come separated by periods.This approach not only conforms to the common processing methods of search engines for keyword lists, but also ensures the uniformity of data formats in internal storage and frontend calls.moduleDetailtag acquisition at the model levelKeywordsWhen, it returns a single string containing all keywords, connected by English commas.

Why is it so important to understand the keyword format?

Understanding this is crucial for us to correctly utilize these keywords in the template. Directly output the entire string as it is to the page<meta name="keywords" content="...">Tags are simple and direct, and also one of their basic uses.However, in a richer and more interactive frontend display, we often need to present each keyword independently.For example, render them as clickable tags (Tag), forming a tag cloud, or as a basis for other content recommendations.If we do not parse this comma-separated string, we will not be able to achieve these more advanced display requirements.

How to correctly parse in the templateKeywordsfields?

The template engine of Anqi CMS (which supports Django template engine syntax) provides us with powerful filter functions. Among them,splitThe filter is the tool that handles such needs.It can split a string into an array of strings based on a specified delimiter, allowing us to operate on each keyword individually.

Below, we demonstrate how to parse through a specific template code examplemoduleDetailTag returnsKeywordsfields and display them as a series of clickable tags.

Assuming we are on a model page (such as an article list page or product list page) where we need to display the keywords associated with the current model.

{# 首先,使用 moduleDetail 标签获取当前模型的 Keywords 字符串 #}
{% moduleDetail moduleKeywordsString with name="Keywords" %}

<div class="module-keywords-section">
    {% if moduleKeywordsString %}
        <h3>当前模型关键词:</h3>
        <ul class="keywords-list">
            {# 使用split过滤器将逗号分隔的字符串解析成数组 #}
            {% set keywordsArray = moduleKeywordsString|split:"," %}
            
            {% for keyword in keywordsArray %}
                {# 
                   在循环中,每个 keyword 变量都代表一个独立的关键词。
                   我们使用 trim 过滤器移除可能存在的首尾空格,并判断关键词是否为空。
                   为了生成有效的搜索链接,我们还对关键词进行了 urlencode 编码。
                #}
                {% if keyword|trim %}
                <li>
                    <a href="/search?q={{ keyword|trim|urlencode }}" class="keyword-tag">
                        {{ keyword|trim }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
        </ul>
    {% else %}
        <p>该模型暂未设置关键词。</p>
    {% endif %}
</div>

{# 
   在某些特定场景下,您可能需要将已经解析过的关键词数组
   重新合并成一个字符串,例如,为了生成符合特定格式的JSON-LD数据。
   这时,`join`过滤器就派上用场了。
#}
{% if keywordsArray %}
    {% set rejoinedKeywords = keywordsArray|join:", " %}
    <p>重新合并后的关键词(用于其他高级用途):{{ rejoinedKeywords }}</p>
{% endif %}

In this code, we first use{% moduleDetail moduleKeywordsString with name="Keywords" %}GottenKeywordsThe original string of the field. Then, by{% set keywordsArray = moduleKeywordsString|split:"," %}this line, we split this string into an array namedkeywordsArrayusing a comma as a delimiter.

Next, we use{% for keyword in keywordsArray %}Loop through this array. In the loop body, eachkeywordVariables represent independent keywords. To ensure the neatness of the display and the validity of the links, we usually perform the following treatments:

  1. {{ keyword|trim }}: UsetrimThe filter removes any whitespace from both ends of the keyword string, as users may accidentally leave spaces when entering it in the background.
  2. {% if keyword|trim %}: