In website content operation, we often need to finely control the displayed information.Sometimes, a title may be very long, and we only want to display the first word in a specific area, or highlight the last word for some design needs.AnQiCMS (AnQiCMS) provides a powerful and flexible template engine, drawing on the essence of Django template syntax, allowing us to achieve these seemingly complex text processing needs through simple filters (Filters).

Today, let's talk about how to cleverly extract and display the first or last word of a string in the AnQiCMS template.

Understand the core tool: Filter

In AnQiCMS templates, filters are used to modify the display content of variables. They are through|The symbol is connected after the variable name. To extract the first or last word, we need to combine several key filters:

  1. splitFilterThis filter can split a string into an array of words (or substrings) based on the delimiter you specify.
  2. firstFilterIt is used to get the first element of an array.
  3. lastFilterUsed to get the last element of the array.

With these tools, we can combine the desired functions like building blocks.

Get the first word of the string.

Suppose we have a namedarticle.TitleThe variable contains the full title of an article, for example: "AnQi CMS: Providing Efficient Solutions for Your Enterprise-Level Website".We hope to only display the first word of the title "AnQi CMS".

First, we need to split this complete title string into individual words. At this point,splitthe filter comes into play. We can write it like this:

{{ article.Title|split:" " }}

This line of code will return an array containing all words, for example["安企CMS:", "为您的", "企业级网站", "提供高效解决方案"]Please note that if the first "word" in the title contains punctuation,split:" "it will be treated as part of the word by default.

Next, we just need to take the first element from this array of words. We can directlyfirsta filter tosplitput it on the result of the filter. The full syntax is:

{{ (article.Title|split:" ")|first }}

This will display on the page, such as “AnQiCMS:”. If your title is all English, such as “AnQiCMS: Efficient Solutions for Your Enterprise Website”, then|split:" "after,|firstWill get "AnQiCMS:".

Get the last word of the string

Similarly, if we want to get the last word 'solution' from the title 'AnQi CMS: Provide an efficient solution for your enterprise-level website', the operation process is also very similar.

We still need to use firstsplitThe filter splits the title into an array of words:

{{ article.Title|split:" " }}

After getting the array, this time we no longer choose the first one, but the last element. At this point,lastThe filter is very suitable. The complete code would be:

{{ (article.Title|split:" ")|last }}

Thus, the page will display the "Solution".

Actual application scenarios and some tips

This function of extracting specific words from strings is very useful in website construction:

  • Dynamic title abbreviationIn a card-style list, sidebar, or navigation menu, if the full title is too long, the first word can be dynamically displayed as a summary.
  • Highlight the key pointsIn marketing copy, sometimes the last word is deliberately made the core keyword, and this method can be styled separately to make it more prominent.
  • SEO optimizationIn some layouts, it may be necessary to use a part of the title as microcopy or alt text, keeping it concise.

Handling empty string or single-word cases:If you are concerned about the variablearticle.TitleMay be empty, or contain only a word, the above method still works well. If the string is empty,split:" "an empty array will be returned, at this timefirstorlastNothing will be output. If the string contains only one word,split:" "it will return an array containing only that word,firstandlastit will return this unique word correctly.

Handle existing HTML tags:If your string content may contain HTML tags (for example, the title is obtained from a rich text editor), and you want to remove these tags before extracting words, you can usestriptagsa filter. For example:

{{ ((article.Title|striptags)|split:" ")|first }}

This ensures that you get pure text words and not fragments with HTML tags.

Set the default value:To enhance user experience, to prevent in some extreme cases (such asarticle.TitleVariable does not exist or is empty) The page appears blank, you can combinedefaulta filter to provide an alternative text:

{{ (article.Title|split:" ")|first|default:"无标题" }}

In this way, if the first word cannot be extracted, 'No Title' will be displayed on the page.

By combining these simple filters, Anqi CMS provides great convenience to content operators, allowing us to control the display of content more flexibly and accurately.


Frequently Asked Questions (FAQ)

1. If my string content is purely Chinese, for example “Anqi Content Management System”, thensplit:" "Can it split the words correctly?For continuous Chinese text,split:" "The entire string is treated as a 'word' because there are no spaces between Chinese characters. If you want to treat each Chinese character as an independent 'word', you need to usemake_listA filter that splits a string into an array of individual characters. For example{{ ("安企内容管理系统"|make_list)|first }}It will output '安'.

2. Can I use characters other than spaces to separate strings?Of course you can.splitThe filter is very flexible, you can specify any character you want as a delimiter. For example, if your keyword list is "SEO, marketing, website", you can get the first keyword in this way: {{ ("SEO,营销,建站"|split:",")|first }}This will get 'SEO'.

3. How to ensure that the extracted words are plain text and not fragments with HTML tags?If the original string may contain HTML tags (for example, the title is obtained from a rich text editor), it is recommended to usesplitthe filter beforestriptagsfilter.striptagsRemove all HTML and XML tags from the string, leaving only plain text content.In this way, during the subsequent word segmentation and extraction, it can ensure that pure text words are obtained. For example: {{ ((article.Content|striptags)|split:" ")|first }}.