In-depth Analysis of AnQiCMSmoduleDetaillabel'sKeywordsField: Format and Template Analysis of Keywords
As an experienced website operations expert, I know that how to efficiently and flexibly handle and display content in a content management system is the key to success.AnQiCMS (AnQiCMS) provides us with great convenience with its simple and efficient architecture.Today, let's delve deeply into a specific issue that often arises in template creation:moduleDetailthe tags returned byKeywordsIs the field a single keyword or a comma-separated string? How should it be correctly parsed and utilized in the template?
To put it directly, frommoduleDetailthe tags returned byKeywordsA field, as well as all fields involving "keywords" in Anqi CMS (such as document keywords, tag keywords, etc.), is acomma-separated string.
This is highly consistent with the design concept of AnQi CMS in the background content management. When entering keywords in the background, the system explicitly requires users to useEnglish comma(,) to separate different keywords. This not only conforms to the common processing method of keyword lists by search engines, but also ensures the uniformity of the data format in internal storage and front-end calls.Therefore, when we pass throughmoduleDetailtags to obtain model-levelKeywordsWhen, you get is a single string containing all the keywords, connected by English commas.
Why is it so important to understand the format of keywords?
Understanding this is crucial for us to correctly utilize these keywords in the template. Directly output the entire string as is to the page.<meta name="keywords" content="...">The label is indeed simple and direct, and it is also one of its basic uses.However, in more rich and interactive front-end presentations, we often need to display 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 templateKeywords?
The AnqiCMS template engine (which supports Django template engine syntax) provides us with powerful filter functions. Among them,splitThe filter is exactly the tool to handle such needs. It can split a string into an array of strings (slice) based on the specified delimiter, so that we can operate on each keyword individually.
Below, we will demonstrate how to parse through a specific template code examplemoduleDetailthe tags returned byKeywordsand display the fields as a series of clickable tags.
Assuming we are on a model page (such as an article list page or product list page), 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" %}We have obtainedKeywordsThe original string of the field. Then, through{% set keywordsArray = moduleKeywordsString|split:"," %}This line, we split this string using a comma as a delimiter into a namedkeywordsArrayarray.
Next, we use{% for keyword in keywordsArray %}Loop through this array. In the loop body, eachkeywordVariables represent an independent keyword. To ensure the neatness of the display and the validity of the links, we usually perform the following processing:
{{ keyword|trim }}: Use.trimThe filter removes any leading and trailing spaces from the keyword string, as the user may accidentally leave spaces when entering in the background.{% if keyword|trim %}: