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 enhances user experience but is also sometimes necessary to meet specific design or security requirements.replaceFilter, can it be flexibly used to format and output these basic styles?
Today, let's delve into this issue in depth.
Get to know the AnQi CMS.replaceFilter
The template engine of AnQi CMS is designed to be very powerful and easy to use, inheriting the syntax style of Django template engine, providing a multitude of practical built-in tags and filters, which help us efficiently handle data. Among them,replaceFilter is a tool used for text content replacement.
As the name suggests,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'.{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }},输出结果就会是“Welcome to AnQiCMS”.
This replacement method is very effective for modifying fixed text, such as correcting a repeated spelling error, or uniformly updating the name of an old product to a new name.
Considerations 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,13812345678,we hope it can be displayed with138-1234-5678in such a format. If it usesreplacea filter to achieve this, some challenges will be encountered.
Due toreplacea filter is used forliteral matching replacementIt does not know the structure rules of the phone number (such as which are area codes, which are prefixes, and which are the middle four digits). If we try to replace138response for138-, or5678response for-5678This is valid for phone numbers that exactly contain these specific substrings and are in fixed positions.
For example:{{ "13812345678"|replace:"138,138-" }}The result is138-12345678
{{ "13812345678"|replace:"1234,-1234-" }}The result is138-1234-5678(This requires knowing the exact digit segments)
But if the phone number is13900001111or01087654321The above 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 lengths or different regions of phone numbers, you need to write a large number of specificreplaceRules, this is clearly not an efficient and maintainable solution.
Although there are filters like in the Anqi CMS,phone2numericThis is a filter related to the phone, but it mainly converts letters to numbers (like)999-PONGO2Converted to999-766462), not for unified formatting output.
Considerations for formatting email addresses
For email address formatting, for example, to[email protected]格式化为Englishuser [at] example [dot] comto prevent 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 the email@and.comHowever, there are still some subtle issues:
- If the email address itself contains
[at]or[dot]This string may cause unexpected replacements. replaceIt is a literal replacement and cannot intelligently recognize.com/.cn/.orgsuffixes of different domains, you need to write replacement rules for each suffix.
Considering the need to prevent scraping, the built-in in the CMSurlizeandurlizetruncfilterer automatically converts URLs and email addresses torel="nofollow"The link, which can also achieve the purpose of anti-crawling to some extent, and is more focused on link generation rather than pure text replacement style.
replaceThe applicable scenarios of the filter
So, it is notreplaceThe filter is not powerful enough, but its original design 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'.
- Uniform keywords or phrasesIn the article content, replace a specific word with another uniformly.
- Clean up specific sensitive wordsIf it is not filtered by the background sensitive word filtering feature, but temporarily cleaned during template rendering, and the sensitive words are fixed and do not contain patterns.
Exploration of more flexible formatting schemes
For phone numbers or email addresses of this kind,Based on pattern matching or structured processingformatting requirements, the Aqi CMS provides a more appropriate solution:
- backend content management tool:Security CMS provides the "Document Keyword Replacement" feature in the "Document Management" section of the backend,supports regular expressions.This means you can define powerful regular expression replacement rules in the background (for example, matching all 11-digit numbers and grouping them in mobile 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 the formatting is only for frontend display needs, and the original data has already been output to the page, then using JavaScript to write regular expression matching and replacement logic, and rendering on the client side, is also a common practice.This can provide high flexibility, but the SEO value of the content itself may need further consideration.