In AnQiCMS content operation, 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 comma-separated, such as"关键词1,关键词2,关键词3". When these string data need to be displayed flexibly in the website front-end template or further processed, splitThe filter has become a very practical tool. It can easily convert such strings into operable arrays, bringing many possibilities for SEO optimization.

splitFilter basic parsing

splitThe filter is a powerful built-in feature of the AnQiCMS template engine, whose main function is to split a string of a specific format into an array (or list) according to a specified delimiter. For example, if your keyword string is"SEO优化,关键词管理,内容营销", usingsplitA filter can be specified with a comma as a delimiter to split it into["SEO优化", "关键词管理", "内容营销"]such an array.

Its basic usage is to pass through a pipe symbol after the variable|连接splitFilter and pass the delimiter as a parameter, for example:{{ 变量名|split:"分隔符" }}If the specified delimiter does not exist in the string,splitThe filter will return an array containing a single element with the original string; if the delimiter is empty, it will split the string into an array based on each UTF-8 character. AndsplitThere are also functions similar to this.make_listIt will split the string into individual characters directly, suitable for finer character-level processing.

UnderstoodsplitThe basic usage of the filter, 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 the TDK settings related to SEO.Generally, we would enter a set of comma-separated keywords in the document's keyword field. With the help ofsplitThe filter allows us to flexibly apply these keywords to the generation of page TDK, making the title, description, and keyword tags more accurate and diverse.

Imagine that we have set up an article文档关键词With"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 keywords 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 around each keyword to ensure the output is neat.

2. Enrich article tags (Tag) and keyword cloud

AnQiCMS provides powerful tag features that can associate documents with the same theme, which is very 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 individual words, then iterate through the array to generate a linked tag for each word.|urlencodeEnsured the correct encoding of the URL while|trimIt ensures the purity of the keyword. 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. Build the 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 accurately understand page content and display it in search results in a richer and more attractive way. Many types of structured data include such asArticleorProduct) contains akeywordsField, it usually needs a keyword array.

BysplitFilter, 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 for managing structured data. We are inkeywordstraversing thesplitafter that.keywordArrayutilize output each keyword enclosed double quotes comma separated note utilize specificallyforloop.Last