In the daily content operation of Anqi CMS, the tidiness and consistency of content are the key to improving user experience and the professionalism of the website.Sometimes, when editing or importing article content, some unnecessary spaces or newline characters may be inadvertently introduced. These seemingly minor details may affect the layout of the content and even cause slight interference in certain situations, such as the crawling and parsing by search engines.
幸运的是,安企CMS强大的模板引擎提供了丰富的过滤器,其中trimFilter is a powerful helper for solving such problems.It can help us easily clean up the spaces and newline characters at both ends of the article content, making your website content more standardized and beautiful.
Understandingtrim过滤器:内容整形的秘密武器
trimFilter is a simple and practical tool in the AnQi CMS template engine, its core function is to remove whitespace characters from both ends of a string (i.e., the beginning and the end). Here, "whitespace characters" not only refer to ordinary spaces, but also include tabs (\t)、newline(\n)、carriage return(\r)等。This means that regardless of how many extra spaces or blank lines are at the beginning and end of the content,trimit can clean them up all at once.
It is worth emphasizing that,trimthe filter is on the contentdisplayWorks automatically; it does not modify the original article data stored in the security CMS background database.This means you can safely use it to optimize the front-end display without worrying about modifying the original version of the content.
How to make use oftrimAre you cleaning the blank spaces at both ends of the article content in bulk?
In Anqi CMS, you will mainly apply in template filestrimFilter. When you retrieve fields such as article titles, descriptions, or content from the database and output them to the page, it is usingtrimthe **opportunity**.
Assuming you are on the article detail page ({模型table}/detail.htmlor{模型table}/detail-{文档ID}.htmlIn it is displayed the content of the article. Usually, we would usearchiveDetailtags to obtain article details, and then output specific fields through double curly braces{{ }}Output specific fields.
The following is an applicationtrimBasic steps and examples of the filter:
Find the location of the output contentOpen your template file (usually located in
/templateDirectory under the template folder you are using). Locate the output position of the field to be cleaned up, such as the article title (archive.Title) Article summary (archive.Description) or article content (archive.Content).Apply
trimFilter只需在输出变量的后面加上 English|trim.例如,如果您要输出文章标题,并且担心标题两端有多余的空格: English
<h1>{{ archive.Title|trim }}</h1>Therefore, regardless of
archive.TitleThe original value is" 我的文章标题 "Or"我的文章标题\n\n", 输出到页面时都会变成 English"我的文章标题".For the article abstract or description:
<p>{{ archive.Description|trim }}</p>Pay special attention to the processing of the article body
Article body (
archive.Content) Typically contains HTML tags. In the security CMS, to prevent XSS attacks or to avoid HTML tags from being incorrectly escaped, we usually use in conjunction with|safeThe filter ensures that HTML content can be correctly parsed by the browser.When
trimThe filter is used together with content containing HTML.|safeshould be placed|trimafterThis is because:trimIt operates on the raw string.safeThis tells the template engine that the processed string is safe and can be output directly as HTML.Example of cleaning up whitespace at both ends of the article body:
<div class="article-content"> {{ archive.Content|trim|safe }} </div>So, even when editing articles in the background, any extra newline characters or spaces at the beginning or end of the main content will be removed when displayed, ensuring that the content appears clean and tidy in the HTML structure.
More fine-grained control:
trimLeftandtrimRightIf your requirement is to only clean the left or right whitespace, Anqi CMS also provides
trimLeftandtrimRightfilters. Their usage is similar totrim, but the direction of action is different.- Clean the left whitespace:
{{ archive.Title|trimLeft }} - Clean right-hand whitespace:
{{ archive.Title|trimRight }}
These refined controls give you greater flexibility when dealing with specific layout requirements.
- Clean the left whitespace:
Clean specific characters (not just whitespace) only
trimThe filter can not only clean up whitespace characters, but also remove these specific characters from both ends of the target string when you provide a string parameter. For example, if you want to remove the characters at both ends of the article title,*auto:<h1>{{ archive.Title|trim:"*" }}</h1>If
archive.TitleThe value of"***安企CMS教程***"auto,输出会变成"安企CMS教程".auto,不过,对于批量清理文章内容两端的“空格和换行符”这一核心需求,不带参数的
|trimauto过滤器是最直接和有效的解决方案。
Summary
By simply adding to the template|trimFilter, you can let the Anq CMS achieve batch cleaning of spaces and line breaks at both ends of the article content in the content display level.This can not only make your website page look more professional and improve the reading experience, but also avoid potential layout issues caused by excessive blank space.As a content operator, mastering these little tricks can effectively improve work efficiency and website content quality.
Common Questions (FAQ)
Q1:trimDoes the filter modify the article content stored in the database?A1: No.trimThe filter only takes effect when the content is rendered on the web page, it is an operation of the 'display layer'. Your original article data remains unchanged in the database and will not be affected.
Q2: I find that there are also extra spaces or line breaks in the middle of the article content,trimCan the filter clean these up?A2:trimThe filter only processes strings.from the beginning and endFor multiple spaces or newline characters in the middle of the content,trimthey cannot be processed directly. If you need to clean up such characters in the content, you may need to combine with other filters (for example,replaceFilter to replace consecutive spaces or line breaks) or pay attention to the specification when editing content.
Q3:trimFilter can handle which types of whitespace characters? For example, tab (Tab) or consecutive blank lines?A3: Yes,trimFilter can identify and remove multiple types of whitespace characters, including regular spaces, tabs (\t)、newline(\n)、carriage return(\rThese characters are removed effectively whether they appear at the beginning or end of a string, whether alone or in sequence.