In the template world of AnQi CMS, text processing is an indispensable part of daily content operation.We often need to format and convert the content displayed to users to ensure clarity and friendly interaction.urlizeandreplaceThese two filters play an important role. Many times, we may wonder whether they can work together to bring deeper levels of automation to our content?
The answer is yes.urlizefilters andreplaceThe filter can be cleverly combined to provide more flexibility and possibilities for the text content processing of the website.Understanding the individual functions and the execution order when combined is the key to unleashing their potential.
Firstly, let's review the basic functions of these two filters.
urlizeFilter: Smart Recognition and Link Conversion
urlizeThe core function of the filter is to intelligently search and identify URL strings (such ashttp://www.example.com/www.example.comevenexample.com) and email addresses, and then automatically convert them into clickable HTML<a>Label. This process not only improves the user experience, but usually also automatically adds links generatedrel="nofollow"属性,这对于SEO管理,特别是处理用户生成内容(UGC)时,是一个非常实用的功能。
When usingurlizeIt is very important at this point: because it generates HTML code, it is usually necessary to work with it to ensure that these codes can be correctly parsed and displayed by the browser instead of being escaped as plain text.|safeFilter used together. In addition,urlizeThere is also a sibling filterurlizetruncIt allows us to specify the display length of the link text, and the part exceeding the limit will be automatically abbreviated with an ellipsis....Replace, this is especially useful in layouts with limited space.
For example, if the content is请访问我们的网站:www.anqicms.comafterurlize|safeAfter processing, it will become请访问我们的网站:<a href="http://www.anqicms.com" rel="nofollow">www.anqicms.com</a>.
replaceFilter: The Precision Text Replacement Expert
replaceThe filter is a more direct text manipulation tool.The function is to find all specified 'old words' in a string and replace them with 'new words', and then return the new string after replacement.This filter is very suitable for keyword correction, uniform correction of misspellings, or batch updating certain fixed text information.
replaceThe usage is intuitive and concise, usually in the form of{{ obj|replace:"old,new" }}. For example,{{ "安企CMS非常棒"|replace:"棒,优秀" }}It will output安企CMS非常优秀.
Combined use: unleash more potential of text processing
Since both of these filters operate on text, the key is how they combineExecution order. The execution of the filter is from left to right, which determines which stage of text each filter acts on.
first
replace, thenurlize: Pre-process plain text content.This combination is suitable for when you want the URL to be converted into an HTML linkpreviouslyThis is a case where some parts of the original text are replaced.For example, your content may contain some old domain names that you want to uniformly replace with new domain names before linking them.
Imagine, there are many articles on the website that mention the old URL
old.example.comand now you want them all to point tonew.example.com,and are all clickable links in the end.{% set articleContent = "欢迎访问我们的旧网站 old.example.com 获取更多信息。" %} {{ articleContent|replace:"old.example.com,new.example.com"|urlize|safe }}The processing flow here is:
- Firstly,
replaceThe filter willold.example.comwithnew.example.com,at this time the text is欢迎访问我们的旧网站 new.example.com 获取更多信息。 - Next,
urlizeFilter recognitionnew.example.comand convert it to a link. - Finally,
safeFilter ensures that HTML code is displayed correctly.
The final output may be:
欢迎访问我们的旧网站 <a href="http://new.example.com" rel="nofollow">new.example.com</a> 获取更多信息。- Firstly,
first
urlize, thenreplace: Refine the generated HTML linksThis combination is relatively more advanced, allowing you to
urlizeThe URL has been converted to complete HTML<a>tagsafterEnglishreplaceSupports regular expression replacement, but currently the AnQi CMSreplacefilter is mainly used for simple string replacement) or adjustmenthrefof the specific part in the properties.For example, you want all generated links, regardless of
http://Orhttps://to be displayed as text uniformly in the displayhttps://starting with (even ifurlizethe link has already been converted to a clickable link, and you only want tohttpmodify itsVisible text).{% set commentText = "请点击 http://example.com/page1 查看详情。" %} {{ commentText|urlize|replace:"http://example.com,https://example.com"|safe }}The processing flow here is:
- Firstly,
urlizeThe filter willhttp://example.com/page1Converted to<a href="http://example.com/page1" rel="nofollow">http://example.com/page1</a>. - Next,
replaceThe filter searches for this complete HTML stringhttp://example.comand replaces it withhttps://example.com. Due toreplaceacts on the entire string, and it modifies it at the same timehrefProperties and visible text (if they are the same). - Finally,
safeFilter ensures that HTML code is displayed correctly.
The final output may be:
请点击 <a href="https://example.com/page1" rel="nofollow">https://example.com/page1</a> 查看详情。It should be noted that,
replaceAffects strings that already contain HTML tags, so it needs to be handled more carefully to avoid accidentally modifying the tag structure.Ensure your replacement target is very clear and does not disrupt the integrity of HTML.- Firstly,
Inspiration for actual application scenarios
- URL standardization and migration:When performing domain name changes, protocol upgrades (from HTTP to HTTPS), or URL structure adjustments on the website, this combination can be flexibly used to automatically update old links in the content.
- Content cleaning and brand unification:When publishing or displaying user comments, forum posts, and other content, sensitive words can be removed before linking or,