How to automatically identify URLs and email addresses in the text and convert them into clickable links in `urlize` and `urlizetrunc` filters in AnQi CMS?

Calendar 👁️ 68

In Anqi CMS, we often encounter scenarios where we need to display URLs or email addresses in the article content, comments, or other user input text.If these addresses are just plain text, users cannot click to jump directly, which will greatly reduce the user experience of the website. 幸运的是,AnQi CMS内置了强大的模板过滤器,其中urlizeandurlizetruncIt is a tool specifically designed to solve this problem, which can automatically convert recognized URLs and email addresses into clickable HTML links.

Familiar with Anqi CMS template filters

The Anqi CMS template system adopts a syntax similar to the Django template engine, which makes content display both flexible and powerful.Filters are a very useful part of it, allowing us to modify, format, or process the output content of variables.urlizeandurlizetruncis such a filter, which can intelligently scan text, automatically identify and wrap links.

urlize:The 'magician' of smart links

urlizeThe core function of the filter is to automatically detect URLs (Uniform Resource Locators) and email addresses in text and convert them into带有<a>Clickable tags. This means you don't have to manually add HTML code to each URL or email,urlizeWe'll take care of it for you.

The principle is very simple:

When you pass a text content containing URLs or email addresses throughurlizethe filter, for example:

<p>{{ "请访问我们的网站:https://en.anqicms.com 或联系我们:[email protected]"|urlize|safe }}</p>

Anqi CMS will automatically identify themhttps://en.anqicms.comand[email protected]and convert them into the following HTML format:

<p>请访问我们的网站:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a> 或联系我们:<a href="mailto:[email protected]">[email protected]</a></p>

It not only adds to the URLhrefproperties, but also to the email addressmailto:Prefix, convenient for users to click to send emails. What's more, it is also added for SEO considerationsurlizeIt will automatically add to external linksrel="nofollow"Property, this helps to avoid unnecessary link weight dispersion.

About detailed control of character escaping:

urlizeThe filter also has a very useful feature, which is to control the character escaping of the link text itself.By default, for security reasons, the template engine may escape HTML special characters.But in some cases, we may want the link text to be displayed as is, even if it contains some special characters.

You can do this byurlizeadded afterwards:And a boolean value (trueorfalse) to control:

  • urlize:true: If the link text contains special HTML characters (such as"), they will be escaped.
  • urlize:false: Special HTML characters will be retained as is and not escaped.

For example, if your URL contains query parameterstest="test":

<p>{% filter urlize:true|safe %}</p>
<p>请访问:www.anqicms.com/test="test" 了解详情</p>
<p>{% endfilter %}</p>

<p>{% filter urlize:false|safe %}</p>
<p>请访问:www.anqicms.com/test="test" 了解详情</p>
<p>{% endfilter %}</p>

The output might be:

<p>请访问:<a href="http://www.anqicms.com/test=%22test%22" rel="nofollow">www.anqicms.com/test=&quot;test&quot;</a> 了解详情</p>
<p>请访问:<a href="http://www.anqicms.com/test=%22test%22" rel="nofollow">www.anqicms.com/test="test"</a> 了解详情</p>

Please note, whether it isurlize:trueOrurlize:falseSince they generate HTML tags, to ensure that the browser parses these links correctly instead of treating them as plain text, you need to use them in conjunction with the output.|safefilter.

urlizetrunc: Elegant link 'Tailor'

Sometimes, a URL may be very long, and displaying it in full on the page may look cluttered and disrupt the overall layout.urlizetruncFilter isurlizeThe upgraded version retainsurlizeall the features and adds the ability to truncate displayed link text.

Its use is very intuitive:

you need tourlizetruncImmediately followed by a colon and the desired display length (a number). If the link text exceeds this value,urlizetruncit will be automatically truncated and an ellipsis will be added at the end....to indicate omission.

For example, if we have a long URL text:

<p>{% filter urlizetrunc:20|safe %}</p>
<p>查看详细文档:https://en.anqicms.com/docs/template/filters/urlize-and-urlizetrunc.html</p>
<p>{% endfilter %}</p>

afterurlizetrunc:20The output HTML will be:

<p>查看详细文档:<a href="https://en.anqicms.com/docs/template/filters/urlize-and-urlizetrunc.html" rel="nofollow">https://www.anqicms...</a></p>

The originally long link address is elegantly shortened, maintaining clickable while maintaining the beauty of the page. Similarly,urlizetruncwill be automatically addedrel="nofollow"properties, and also need to be matched when outputting|safeFilters to ensure that HTML code is parsed correctly.

When and where to use them?

These filters have a wide range of applications in Anqi CMS, especially suitable for handling text areas that contain user-generated content or may contain uncertain links:

  • Article detail page:When you paste external reference links in the article body, they can be automatically converted into clickable forms.
  • Comment section or message board:When a user posts a comment or message and mentions a website or email address, the system can automatically recognize and generate a link, enhancing interactivity.
  • Product Description:Product details include external review links or supplier websites that can be easily converted to active links.
  • Any unstructured text content:Any text that needs to be displayed to the user and may contain URLs or email addresses can be considered for use.

Tip: Ensure security and correct rendering.

While usingurlizeandurlizetruncAt the moment, there is a very important detail that needs to be paid attention to:**always

Related articles

How to use the `wordwrap` filter in AnQi CMS template to automatically wrap long text and avoid layout chaos or overflow?

In website content operation, we often encounter situations where the text content is too long, causing page layout chaos, even overflowing the container, which seriously affects the user experience.Especially when a continuous English word without spaces or a long string of Chinese characters appears in a narrow area, the default line break mechanism of the browser may not meet our design requirements.Fortunately, AnQi CMS provides a very practical tool for template developers to elegantly solve this problem - that is the `wordwrap` filter.

2025-11-08

How to quickly count the number of words (or Chinese words) in the content of an article in AnQi CMS using the `wordcount` filter, for SEO or reading time evaluation?

In website operation, the quality and presentation of content are crucial.The number of words in an article not only affects its performance in search engines, but is also a key indicator for evaluating user reading experience and estimating reading time.No doubt, manually counting the number of words in content is a tedious task, especially when the amount of content is vast.Luckily, AnQiCMS (AnQiCMS) has provided us with a small yet powerful tool—the `wordcount` filter, making content word count easy. ### The Importance of Word Count in Content

2025-11-08

How to center, left-align, and right-align Chinese string with specified width using `center`, `ljust`, and `rjust` filters in the Anqi CMS template?

When building and maintaining website content, we often need to pay attention to the visual presentation of information, especially the alignment of text content.It is particularly important to accurately control the centering, left alignment, or right alignment of strings, whether it is for the neatness and beauty of the table or to maintain visual balance within a fixed width area.AnQiCMS with its flexible and powerful template engine, provides us with several very practical filters to easily achieve this goal, even for strings containing Chinese characters.

2025-11-08

How to split a normal string into an array of individual characters using the `make_list` filter in the Anqi CMS template for more refined text processing?

In the world of AnQi CMS templates, we often need to process content in various ways, sometimes by paragraphs, sometimes by sentences, and sometimes, we need to refine the text more finely, down to every character.When conventional string processing methods fail to meet such refined needs, the `make_list` filter can excel, as it can split a common string into an array of individual characters, providing unprecedented flexibility for template designers.

2025-11-08

How to safely output HTML code passed from the backend in Anqi CMS template using the `safe` filter without escaping?

Build and manage websites in AnQi CMS, we often make good use of its powerful and flexible template system to present colorful content.The template engine of AnQi CMS has adopted the syntax of Django templates, which brings us a familiar development experience and powerful features.However, when handling HTML code passed from the backend to the frontend, we encounter an important security mechanism: **automatic escaping**.

2025-11-08

The `dump` filter has what practices in debugging Anqi CMS templates, and how to clearly view the structure and value of complex variables?

During the template development process of AnQi CMS, we often encounter the need to view variable content and structure, especially when dealing with complex objects passed from the background.If you cannot clearly know what is inside the variable, debugging will be as difficult as a blind man feeling an elephant.The AnqiCMS adopts the Pongo2 template engine syntax similar to Django, providing rich tags and filters to help us build dynamic pages, one extremely powerful debugging tool is the `dump` filter.### `dump` filter

2025-11-08

How to concatenate the article title (Title) with the system-defined website name (SiteName) through the `tdk` filter and output it to the <title> tag in the Anqi CMS template?

In website operation, the importance of the `<title>` tag is self-evident. It is not only the key for search engines to understand the theme of the page, but also the content that users see first in the search results.A well-crafted page title that can effectively increase click-through rates and have a positive impact on SEO optimization.Therefore, how to organically integrate the core title of the article with the brand name of the website to form an accurate and attractive `<title>` tag is a fundamental and important link in the design of website templates.AnQiCMS as a feature-rich enterprise-level content management system

2025-11-08

How to configure the separator (`sep`) and whether to display the parent category title (`showParent`) for the `tdk` filter in Anqin CMS?

During website operations and SEO optimization, the page title (Title) is a key element to attract user clicks and improve search engine rankings.AnQiCMS provides a flexible `tdk` filter, allowing us to finely control the display of page TDK (Title, Description, Keywords).Among these parameters, `sep` and `showParent` play a crucial role in building page titles with rich hierarchy and clarity.

2025-11-08