In AnQiCMS template design, in order to present the expected content effect, we often need to process strings finely. Among many built-in filters,cutFilter is a seemingly simple but extremely practical tool.Its core function is to remove specified characters from any position in template strings, which makes it have unique application value in content cleaning, formatting, and enhancing user reading experience.

cutThe working principle of the filter is very direct: it traverses the target string and removes all segments that match the specified character. Its usage is also very concise, for example, if you have a variablemyStringThe value is"Hello world"And you want to remove the spaces, you can use it like this:.{{ myString|cut:" " }}The result will be.HelloworldThis feature makes it an indispensable auxiliary tool in many scenarios.

Below, let's discuss severalcutCommon practical application scenarios of filters in security CMS templates:

Clean up extra spaces, optimize text display

In content display, especially for some short titles, keywords, or list items, excessive spaces can affect the visual neatness and layout. Although we havetrimThe filter can remove spaces from both ends of a string, but if spaces appear in the middle of the string,trimit is powerless. At this time,cutThe filter comes in handy.

Imagine that you obtain a product name from an external data source, its value is" 智能 手机 Pro ". If you display it directly, it will look very unprofessional. BycutFilter, can easily remove all internal spaces, making it display as"智能手机Pro". Just write it like this:{{ productName|cut:" " }}. This meticulous cleaning makes the page look more refined and orderly.

Remove specific punctuation marks or special characters

很多时候,我们希望内容以一种“纯粹”的形式展现,例如在生成一些标签(Tag)云、面包屑导航的特定部分,或者需要将包含特殊字符的标题转换为更简洁的显示形式时。一些数据库存储的字段可能包含不适用于前端展示的标点符号(如,/:/-etc.).

Assuming an article's tag list is output as a string in some way“CMS,Go-Lang,内容管理”In a compact area, you only want to display plain text and do not want to see commas. At this point,{{ articleTags|cut:"," }}it can be converted to“CMSGo-Lang内容管理”Similarly, if there is a product code fieldproductCodeThe value is"PRO#12345#"and you just need12345, you can use{{ productCode|cut:"#" }}to achieve the purpose.

It is worth noting that,cutFilter can remove only one specified character at a time.If you need to remove multiple different characters, we can achieve this by using chained filter calls.{{ someText|cut:","|cut:"-" }}.

Format numbers or prices, extract pure numeric information

In e-commerce websites or pages related to amounts, product prices are usually accompanied by currency symbols (such as¥/$) or units (such as/kg)。Although backend data usually distinguishes between numeric values and units, in certain front-end display scenarios, you may need to display pure numbers, such as in calculators or chart data.

If the price of a product isitemPriceThe value of the field is"¥199.00"and you want to display only199.00, you can use{{ itemPrice|cut:"¥" }}Similarly, if inventory informationitemStockYes"100件"is needed to extract100so that{{ itemStock|cut:"件" }}It can help you complete the task. This is very convenient for maintaining visual consistency of the data and for possible JavaScript processing later.

Simplify URL path or filename display

Although AnQiCMS has done a good optimization in generating static URLs, in some custom templates, you may need to extract a specific segment without certain characters from a complete URL string, so as to use it for frontend CSS classes, IDs, or JavaScript variables.

For example, the URL of an image ishttps://example.com/uploads/images/product_image_large.jpgand you just needproduct_image_largeThis part of the filename is used as an identifier for a component. If the backend cannot directly provide this field, you can gradually do so in the templatecutfallhttps://example.com/uploads/images/and.jpg,or simply,cutdrop all/and.symbols, then combine with other string processing methods (such asreplace), to obtain the part you want.

Summary

cut


Common Questions and Answers (FAQ)

1.cutfilters andtrimWhat are the differences between filters? trimThe filter is mainly used to remove stringsfrom the beginning and endof the specified character (default is space). AndcutThe filter can remove specified characters from a stringfrom any position(including those at the beginning, end, and middle). If your target character only exists at the ends of the string,trimIt will be more efficient; if characters are scattered within the string,cutthen it is a better choice.

2. If I want to remove multiple different characters,cutcan the filter handle them all at once? cutThe filter can only remove one specified character at a time. If you need to remove multiple different characters, you need to separate them.cutChain the filter calls. For example, to remove commas and other characters at the same time, separate them with commas.,)and hyphen(-),you can write it like this:“{{ myString|cut:","|cut:"-" }}.

3.cutWill the filter affect the original data stored in my background database?No.cutThe filter (and all filters in the AnQiCMS template) only process the data at the time of template rendering to generate the final HTML output.It will not modify any original data stored in the database.You can safely use it in the template to format and clean content without worrying about data integrity issues.