In the content operation of AnQiCMS, we often encounter scenarios where we need to handle keyword strings, such as when setting multiple keywords for an article or product in the background, which are usually entered in the form of a comma-separated list, for example,"关键词1,关键词2,关键词3"When these string data need to be displayed flexibly on the front-end template of the website or further processed,splitThe filter becomes a very practical tool. It can easily convert such strings into an operable array, bringing multiple possibilities for SEO optimization.
splitFilter basic parsing
splitThe filter is a powerful feature built into the AnQiCMS template engine, which mainly serves to split a string of a specific format into an array (or list) according to the specified delimiter. For example, if your keyword string is"SEO优化,关键词管理,内容营销"UsesplitFilter and specify a comma as the delimiter, and it can be split into["SEO优化", "关键词管理", "内容营销"]Such an array.
The basic usage is to pass through the pipe symbol after the variable|ConnectsplitFilter, and pass the delimiter as a parameter, for example:{{ 变量名|split:"分隔符" }}If the specified delimiter does not exist in the string,splitThe filter will return a single-element array containing the original string; if the delimiter is empty, the string will be split into an array at each UTF8 character.splitThere are also features similar to this.make_listIt will split the string character by character directly, which is suitable for more fine-grained character-level processing.
UnderstoodsplitThe basic usage of the filter, and we can explore how it plays a role in the SEO optimization practice of AnQiCMS.
1. Dynamically generate page TDK (Title, Description, Keywords)
In AnQiCMS, the core of content management lies in the publication and management of content, which includes SEO-related TDK settings.通常,我们会在文档的关键词字段中填入一组用逗号分隔的关键词。【en】Generally, we will fill in a set of keywords separated by commas in the keywords field of the document.splitFilter, we can flexibly apply these keywords to the generation of page TDK, making the title, description, and keyword tags more accurate and diverse.
Imagine, we set up for an article文档关键词response for"AnQiCMS教程,Go语言CMS,网站优化技巧"In the template, we can use it like thissplitFilter:
{% archiveDetail articleKeywords with name="Keywords" %}
{% set keywordArray = articleKeywords|split:"," %}
<title>{{ keywordArray[0]|trim }} - {{ archive.Title }} - {% system with name="SiteName" %}</title>
<meta name="keywords" content="{% for kw in keywordArray %}{{ kw|trim }}{% if not forloop.Last %},{% endif %}{% endfor %}">
<meta name="description" content="{{ keywordArray|slice:"0:3"|join:", "|trim }},这是关于AnQiCMS的深度教程,帮助您掌握网站优化技巧。">
In this way, we can extract the first keyword as a prefix for the title, use all keywords as page keyword tags, and even filter out some keywords from the keyword list to generate more attractive page descriptions.|trimThe use of filters is very important here, it can remove spaces before and after each keyword to ensure the output is neat.
2. Enrich article tags (Tag) and keyword cloud
AnQiCMS provides a powerful tagging feature that can associate documents with the same topic, which is greatly beneficial for user experience and the construction of internal links for SEO. If we are accustomed to entering a series of related words in the document keyword field, thensplitThe filter can help us quickly convert these words into clickable tag links or build a dynamic keyword cloud.
For example, suppose we have a custom fieldrelated_termsstored"网站建设,SEO指南,内容策略":
{% archiveDetail relatedTerms with name="related_terms" %}
{% set termsArray = relatedTerms|split:"," %}
<div class="article-tags">
<span>相关标签:</span>
{% for term in termsArray %}
<a href="/tag/{{ term|trim|urlencode }}">{{ term|trim }}</a>
{% endfor %}
</div>
Here, we first go throughsplitSplit the string into separate words, then iterate through the array, and generate a linked tag for each word.|urlencodeEnsured the correct encoding of the URL, while|trimIt ensures the purity of the keywords. This dynamically generated tag not only enhances the internal link structure of the page but also helps search engines better understand the theme of the page content.
3. Constructing a list of keywords in structured data (JSON-LD)
Structured data (such as JSON-LD) is an indispensable part of modern SEO, helping search engines to understand page content more accurately and display it in search results in a richer and more attractive form. Many types of structured data (such asArticleorProductAll contain akeywordsfield, which usually requires a keyword array.
PasssplitFilter, we can directly convert the keyword string entered in the AnQiCMS background to the array format required by JSON-LD:
{% archiveDetail articleKeywords with name="Keywords" %}
{% set keywordArray = articleKeywords|split:"," %}
{% jsonLd %}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "{{ archive.Title }}",
"keywords": [
{% for keyword in keywordArray %}
"{{ keyword|trim }}"{% if not forloop.Last %},{% endif %}
{% endfor %}
],
"url": "{{ archive.Link }}",
"description": "{{ archive.Description }}"
// ... 其他字段
}
</script>
{% endjsonLd %}
Here,jsonLdIt is a convenient tag provided by AnQiCMS, used to manage structured data. We use it inkeywordsto traverse thesplitafterkeywordArray,并逐一输出,每个关键词用双引号包裹,并用逗号分隔(注意利用forloop.Last