In the daily operation of website content, we often need to ensure that the information presented on the page is both beautiful and standardized.Especially for contact information such as phone numbers and email addresses, maintaining a uniform display style not only improves user experience but is also sometimes to meet specific design or security requirements.When using AnQi CMS for template development and content display, many friends may be curious about: what AnQi CMS providesreplaceFilter, can it be used flexibly to format the output of these basic styles?
Today, let's delve deeply into this issue.
Know about AnQi CMSreplaceFilter
The AnqiCMS template engine is designed to be very powerful and easy to use, inheriting the syntax style of the Django template engine, providing many practical built-in tags and filters to help us efficiently handle data. Among them,replaceThe filter is a tool used for text content replacement.
As the name implies,replaceThe core function of the filter is to performDirect, literal text replacement. Its usage is very intuitive: you need to specify an 'old keyword' and a 'new keyword', and the system will find all matches of the 'old keyword' in the target string and replace them with the 'new keyword'.For example, if you have a text like “Welcome to AnQi CMS” and you want to replace “AnQi” with “AnQi”, you just need to use{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }}Welcome to AnQiCMS.
This replacement method is very effective for modifying fixed text, such as correcting a repeated spelling error, or updating an old product name to a new one.
Consideration for formatting phone numbers
Now, let's turn our attention to the formatting of phone numbers. Imagine that we receive a pure numeric phone number from the backend, such as13812345678We hope it can be displayed in such a format.138-1234-5678If it is displayed using such a filter, some challenges will be encountered.replaceLiteral matching replacement will be encountered.
due toreplaceThe filter is forLiteral matching replacementIt does not know the structure rules of phone numbers (such as which are area codes, which are prefixes, and which are the middle four digits). If we try to replace138With138-Or5678With-5678This is valid only for phone numbers that exactly contain these specific substrings and have a fixed position.
For example:{{ "13812345678"|replace:"138,138-" }}The result is138-12345678
{{ "13812345678"|replace:"1234,-1234-" }}The result is138-1234-5678This requires knowing the exact number segment.
But if the phone number is13900001111or01087654321These rules may become invalid. You cannot simplyreplaceIt indicates that a hyphen should be inserted after the third and seventh positions as it does not have pattern recognition capabilities. For different length or regional phone numbers, you need to write a large number of specificreplaceRules, this is clearly not an efficient and maintainable solution.
In the filter of AnQi CMS, there are likesphone2numericThis is a filter related to phone numbers, but it mainly converts letters to numbers (such as999-PONGO2to999-766462), but not for unified formatting output.
Consideration for formatting email addresses
For email address formatting, for example, to[email protected]Formatted asuser [at] example [dot] comprevent it from being scraped by robots,replacethe filter seems to have some use.
We can try it this way:{{ "[email protected]"|replace:"@, [at] "|replace:".com, [dot] com" }}
This seems feasible because it targets fixed symbols in email addresses@and.comHowever, there are still some subtle issues:
- If the email address itself contains
[at]or[dot]Such a string may cause unexpected replacement. replaceIt is a literal replacement, it cannot intelligently recognize..com/.cn/.orgSuffixes of different domain names, you need to write replacement rules for each suffix.
Considering the need to prevent scraping, the Anqicms CMS built-inurlizeandurlizetruncfilter will automatically convert URLs and email addresses torel="nofollow"The link, which to some extent can also achieve the purpose of anti-crawling, and focuses more on link generation rather than pure text replacement style.
replaceThe applicable scenarios of the filter
So, notreplaceThe filter is not strong enough, but its original intention and positioning are different. It is most suitable for:
- Simple, literal text correctionWhen you know the exact "old text" and want it to be replaced with the exact "new text".
- Unified keywords or phrasesIn the article content, replace a specific word with another uniformly.
- Clean up specific sensitive wordsIf it is not through the background sensitive word filtering function, but is temporarily cleaned at the template rendering time, and the sensitive words are fixed and do not contain a pattern.
Exploration of a more flexible formatting scheme
For the need of phone numbers or email addresses such asbased on pattern matching or structured processingformatting requirements, AnQi CMS provides a more suitable solution:
- backend content management tool: Anqi CMS provides the 'Document Keyword Replacement' function in the 'Document Management' section of the backend, andSupports regular expressionsThis means you can define powerful regular expression replacement rules in the background (for example, matching all 11-digit numbers and grouping them in phone number format), and then apply them to the entire site with one click.This is a more powerful and unified format management method, especially suitable for batch processing of existing content.
- front-end JavaScriptIf formatting is only for frontend display needs, and the original data has already been output to the page, then writing regular expression matching and replacement logic with JavaScript to render on the client side is also a common practice.This can provide extremely high flexibility, but the SEO value of the content itself may need to be further considered.