Does the `first` and `last` filter return a single Chinese character when processing Chinese string?

Calendar 👁️ 62

In Anqi CMS template development, we often use various filters (filters) to format or extract data.firstandlastThe filter is one of the most common ones, used to get the first or last element from a string or array.Many friends who use AnQi CMS may be curious. When we process data containing Chinese string, such as article titles or content snippets, will these two filters return individual characters?

The answer is:Yes, Anqi CMS'firstandlastThe filter returns individual Chinese characters intelligently and intuitively when processing Chinese string.

This is due to the good support of Unicode character set by the underlying Go language of AnQi CMS, which enables the template engine to maintain a high degree of accuracy and consistency when handling multi-language characters. Whether it is English characters or Chinese characters,firstFilters can accurately extract the first character of the string,lastFilters extract the last one.

Let's understand this better through some specific examples:

If you have a string in English{{ "AnQiCMS"|first }}it will outputA. Similarly,{{ "AnQiCMS"|last }}it will outputS.

When we face Chinese strings, for example{{ "安企内容管理系统"|first }}the output result isWord. Correspondingly,{{ "安企内容管理系统"|last }}will getWord.

This processing method is very consistent with our daily habits and intuition, eliminating the trouble of writing complex logic to judge and extract Chinese characters, greatly improving the efficiency and simplicity of template development.

It's not just a string: Handling multi-type data

firstandlastThe filter is not limited to strings. Its design philosophy is to obtain the "first or last element", which makes it also very good at handling other types of data.

  • Array or slice (Slice): If your variable is an array or a slice,firstIt will return the first element of the array,lastIt will return the last one. For example, assume you have a comment objectcommentsarray,{{ comments|first }}It will return the first comment object in the array
  • Numbers and BooleansWhen the variable itself is a number (such as5) or a boolean value liketrue),firstandlastThe filter will return the value itself, for example{{ 5|first }}will output5,{{ true|last }}will outputtrue.
  • Empty value ornilIf the variable is an empty string, an empty array, or not defined at all (nilThenfirstandlastThe filter will not output anything and will not raise any errors, making it safer to perform conditional judgments in templates.

This versatility allowsfirstandlastThe filter has good versatility in AnQi CMS templates.No matter if you want to quickly preview the first element of the list or need to make initial judgments and displays of some dynamic data, they can provide concise and effective solutions.

application in real-world scenarios

In the actual content operation and template design of AnQi CMS,firstandlastfilters have many practical scenarios:

  1. List overview displayIn an article list or product list, you may want to display the first character of each entry's title as a decorative icon or marker, or extract the last character of the summary for special treatment.
  2. Truncate dynamic content: When the length of certain fields (such as custom short descriptions) is uncertain, you can usefirstorlastTo get a specific part to be displayed, although in more complex extraction scenarios,truncatecharsortruncatewordsfilters will be more commonly used.
  3. Navigation and classification optimizationExtract the first character of the long category name as a quick index, or briefly display the level name in multi-level navigation.
  4. Data status judgment: CombinedifLabel, determine whether an array has at least one element({% if my_list|first %}) to decide whether to render a block.

When using these two filters, we just need to remember their working principles: they process strings by characters (including Chinese characters) and sets by elements.This can help us better plan the template logic, avoid unnecessary troubles.


Frequently Asked Questions (FAQ)

1. Besides Chinese string,firstandlastWhat types of data can the filter handle?Except for Chinese character strings, they can also handle English character strings, arrays (or slices), numbers, and boolean values.For arrays, they return the first or last element;For numbers and boolean values, they will return the variable itself directly.

2. If the string is empty or the variable isnil, usingfirstorlastWhat will the filter result be?If the string is empty, the array is empty, or the variable itself isnil(undefined),firstandlastFilters do not output anything. They do not raise errors, which increases the robustness of the template when used.

3. When usingfirstorlastWhat potential issues should be noted when using filters?The most important thing is to make sure you have a clear understanding of the data type and expected results.For example, if you expect to get the first character of a string, but actually pass in a number, then you will get the number itself rather than its string representation of the first character.In addition, if the data source is unstable, it is recommended to combineifLogical judgment, for example{% if my_string|first %}{{ my_string|first }}{% endif %}Avoid outputting empty content when the data does not exist.

Related articles

How to get the first or last element of a list in AnQiCMS template?

When building a website, we often encounter the need to pick out the most special one from a pile of content, such as displaying the latest article as the headline, or highlighting a popular product.In the AnQiCMS template, it is crucial to flexibly obtain the first or last element of the list to meet these requirements.Fortunately, AnQiCMS provides a variety of intuitive and efficient methods to handle these scenarios, making content display more vivid.AnQiCMS's template system uses syntax similar to the Django template engine

2025-11-08

Can the `removetags` filter remove specified tags from HTML content (such as `<i>`)?

In AnQiCMS (AnQiCMS) such a flexible content management system, handling HTML content is a common task in daily operations.Sometimes, we want to remove certain tags from the content without completely stripping all HTML structure, in order to maintain consistency in the page display or meet design specifications.At this time, the `removetags` filter has become a very practical tool.### Understanding the `removetags` filter The `removetags` is an embedded filter provided by the Anqi CMS template engine

2025-11-08

How to remove all HTML tags from dynamically generated HTML content?

In website content management, we often encounter a common requirement: to extract pure text information from dynamically formatted content.The reasons behind this are diverse, such as the need to generate concise and clear meta descriptions (Meta Description) for search engines, to display unformatted summaries on list pages, or simply to obtain clean plain text content for data analysis.AnQi CMS as a flexible and efficient content management system, fully considering these scenarios, through its powerful template engine and built-in filters

2025-11-08

How does the `yesno` filter handle boolean or null values and customize the display of 'Yes/No/Unknown'?

In AnQi CMS template development, how to display boolean (true/false) states or handle unknown (empty) values in an intuitive and concise manner is an important aspect for improving user experience and code readability.The `yesno` filter is designed for this purpose, it can simplify complex logical judgments into a single line of code, and allows you to customize the output results, such as displaying as "yes/no/unknown".### `yesno` filter: Smart converter for boolean and null values In a content management system, we often encounter situations where we need to display whether a project is enabled or a feature is turned on

2025-11-08

How to encode URL parameters to ensure the correctness and security of the link?

In the daily operation of Anqi CMS, we often need to deal with various website links, which not only need to be beautiful and SEO-friendly, but more importantly, they must work correctly and safely.Among these, encoding URL parameters is a seemingly trivial but crucial link, which is directly related to the integrity and user experience of our website links.Why is URL parameter encoding so important?Imagine if your website had a search function, and users entered keywords containing spaces, special characters, or even Chinese characters, such as "Anqi CMS"}

2025-11-08

What is the difference between the `urlencode` and `iriencode` filters in URL escaping?

In the daily content operation and website development of Anqi CMS, we often need to handle URL links to ensure that they are both secure and effective, and can be correctly parsed by browsers and search engines.This is an indispensable part of URL escaping (also known as encoding).Our Anqi CMS provides us with two very practical filters: `urlencode` and `iriencode`, both of which can help us with URL escaping, but their purposes and processing methods are different in actual application.###

2025-11-08

How does AnQiCMS automatically identify URL links in text and convert them into clickable `<a>` tags?

During content creation and publication, we often encounter situations where we need to cite external sources or share relevant links.If these URLs are in plain text form, users not only need to manually copy and paste them, but it will also affect the reading experience and the professionalism of the website.Anqi CMS knows this pain point, and therefore integrated the intelligent URL automatic recognition and conversion function from the beginning of the system design, making your content publishing more efficient and convenient, and improving the user experience accordingly.### Intelligent recognition, automatic conversion: Say goodbye to manual operation AnQi CMS through its powerful template engine

2025-11-08

How to limit the display length of the link text when converting URLs with the `urlizetrunc` filter?

In AnQiCMS (AnQiCMS) content operation practice, we often encounter some details that require fine-grained processing, one of which is how to elegantly display the super-long URL on the page.When a text contains a long URL, it may destroy the page layout and affect the overall aesthetics and the reading experience of the user.Fortunately, Anqi CMS provides the `urlizetrunc` filter, which can help us easily solve this problem, allowing URLs to be converted into clickable links while still controlling their display length

2025-11-08