In website content operation, we often need to finely control the displayed information.Sometimes, a title might be very long and we only want to display the first word in a specific area, or highlight the last word for some design reasons.AnQiCMS (AnQiCMS) provides a powerful and flexible template engine, which draws on the essence of Django template syntax, allowing us to implement 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 AnQiCMS templates.
Understand the core tools: Filters
In AnQiCMS templates, filters are used as tools to modify the displayed content of variables. They go through|The symbol concatenation comes after the variable name. To extract the first or last word, we need to combine several key filters:
splitFilter:This filter can split a string into an array of words (or substrings) based on the delimiter you specify.firstFilter:Used to get the first element in the array.lastFilterEnglish for getting the last element of an array.
With these tools, we can combine the desired functions like stacking blocks.
Get the first word of a string.
Suppose we have a name calledarticle.TitleThe variable, it 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".
Firstly, we need to split the complete title string into individual words by spaces.splitThe filter comes into play here. We can write it like this:
{{ article.Title|split:" " }}
This line of code will return an array containing all the words, for example["安企CMS:", "为您的", "企业级网站", "提供高效解决方案"]. Please note that if the first 'word' in the title contains punctuation markssplit:" "it will be considered as part of the word by default.
Next, we just need to take the first element from this word array. We can directly writefirstapply thesplitthe result of the filter. The full notation is:
{{ (article.Title|split:" ")|first }}
So, the page will display “AnQiCMS:”. If your title is in English, for example “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" of the title "Safe CMS: Providing high-efficiency solutions for your enterprise-level website", the operation process is also very similar.
We still need to use it 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 }}
So, the page will display “Solution”.
Actual application scenarios and some tips
This function of extracting specific words from strings is very useful in many practical scenarios in website construction:
- Dynamic title abbreviationIn card-based lists, sidebars, or navigation menus, if the full title is too long, the first word can be dynamically displayed as a summary.
- Highlight the key points.In marketing copy, sometimes the last word is deliberately made as the core keyword. Using this method can set the style separately, making it more prominent.
- SEO optimizationIn some layouts, it may be necessary to treat a part of the title as microcopy or alt text while keeping it concise.
Handling empty string or single-word situations:If you are concerned about the variablearticle.TitleIt may be empty or contain only one word, the above method still works well. If the string is empty,split:" "an empty array will be returned, at this timefirstorlastIt will not output any content. If the string contains only one word,split:" "it will return an array containing only that word,firstandlastit will return the unique word correctly.
Process any existing HTML tags:If your string content may contain HTML tags (for example, titles are obtained from a rich text editor), and you wish to remove these tags before extracting words, you can usestriptagsfilter. For example:
{{ ((article.Title|striptags)|split:" ")|first }}
This ensures that you get the word as plain text, not as a fragment with HTML tags.
Set default value:To enhance user experience and prevent blank pages in extreme cases (such asarticle.Titlethe variable does not exist or is empty) you can combinedefaulta filter to provide an alternative text:
{{ (article.Title|split:" ")|first|default:"无标题" }}
So, if the first word cannot be extracted, "No title" will be displayed on the page.
Through these simple filter combinations, the security CMS provides great convenience for content operators, allowing us to control the display of content more flexibly and precisely.
Common Questions (FAQ)
1. If my string content is purely Chinese, for example, '安企内容管理系统', thensplit:" "can it split words correctly?for continuous text in Chinese,split:" "The entire string is treated as a "wordmake_listThe filter, it splits the string into an array of individual characters. For example{{ ("安企内容管理系统"|make_list)|first }}The output will be '安'.
2. Can I use other characters 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 construction', you can retrieve 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 your original string may contain HTML tags (for example, titles are obtained from rich text editors), it is recommended to usesplitthe filter beforestriptagsFilter.striptags{{ ((article.Content|striptags)|split:" ")|first }}.