In the AnQi CMS template world, 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. Among them,urlizeandreplaceThese filters play an important role. Many times, we might wonder if they can work together to bring deeper automation processing to our content?

The answer is affirmative.urlizeFilters andreplaceThe filter can be used creatively, providing more flexibility and possibilities for the website's text content processing.Understanding the individual functions and the execution order when combined is the key to unleashing their potential.

First, let's review the basic functions of these two filters.

urlizeFilter: Intelligent Recognition and Link Transformation

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 user experience, but also usually automatically adds to the generated linksrel="nofollow"The property is a very useful feature for SEO management, especially when dealing with user-generated content (UGC).

While usingurlizeAt the same time, something is very important: because it generates HTML code, so in order to ensure that these codes can be correctly parsed and displayed by the browser, rather than being escaped as plain text, it usually needs to be配合|safeThe filter is 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 it 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. Its function is to find all specified 'old' words in a string and replace them with 'new' words, then return the new string after replacement.This filter is very suitable for keyword correction, uniformity 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非常优秀.

Combine to unleash more potential in text processing

Since both of these filters operate on text, the key is in how they are combinedExecution orderThe execution of the filter is from left to right, which determines the stage of the text each filter acts on.

  1. Firstreplace, thenurlize: Pre-process plain text content

    This combination is suitable for when you want URLs to be converted to HTML linksBeforeThe situation where some parts of the original text are replaced.For example, your content may contain some old domain names, and you want to replace them with new ones before they are linked.

    Imagine that there are many articles on a website that mention old URLsold.example.comand now you want them all to point tonew.example.comand are all clickable links.

    {% set articleContent = "欢迎访问我们的旧网站 old.example.com 获取更多信息。" %}
    {{ articleContent|replace:"old.example.com,new.example.com"|urlize|safe }}
    

    The processing flow here is:

    • first, replaceThe filter willold.example.comReplacenew.example.comAt this point, the text is:欢迎访问我们的旧网站 new.example.com 获取更多信息。
    • Then,urlizeFilter recognitionnew.example.comand convert it to a link.
    • Finally,safeThe filter ensures that HTML code is displayed correctly.

    The final output might be:欢迎访问我们的旧网站 <a href="http://new.example.com" rel="nofollow">new.example.com</a> 获取更多信息。

  2. Firsturlize, thenreplace:Fine-tune the generated HTML links

    This combination is relatively more advanced, allowing you tourlizeThe URL has been converted to full HTML<a>TagAfter thatReplace this HTML string. This may include modifying the display text of links, adding additional HTML attributes (ifreplaceSupports regular expression replacement, but currently Anqi CMS'sreplaceThe filter is mainly used for simple string replacement) or adjustmenthrefspecific part of the properties.

    For example, you want all generated links, no matterhttp://Orhttps://to be displayed as a unified texthttps://starting with (evenurlizehas already convertedhttplinks to clickable links, you only want to modify theirVisible text)

    {% set commentText = "请点击 http://example.com/page1 查看详情。" %}
    {{ commentText|urlize|replace:"http://example.com,https://example.com"|safe }}
    

    The processing flow here is:

    • first, urlizeThe filter willhttp://example.com/page1to<a href="http://example.com/page1" rel="nofollow">http://example.com/page1</a>.
    • Then,replaceThe filter searches within this complete HTML stringhttp://example.comand replace it withhttps://example.com. BecausereplaceIt acts on the entire string, modifying it at the same timehrefProperties and visible text (if they are the same).
    • Finally,safeThe filter ensures that HTML code is displayed correctly.

    The final output might be:请点击 <a href="https://example.com/page1" rel="nofollow">https://example.com/page1</a> 查看详情。

    It should be noted that under this combination,replaceActing on a string that already contains HTML tags, it is necessary to handle it with more care to avoid accidentally modifying the tag structure.Make sure your replacement target is very clear and does not break the integrity of HTML.

Inspiration from practical application scenarios

  • URL standardization and migration:When performing a domain name change, protocol upgrade (from HTTP to HTTPS), or URL structure adjustment on a 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