How `urlize` and `urlizetrunc` filters automatically convert URLs and email addresses in text to clickable hyperlinks?

Calendar 👁️ 68

In Anqi CMS, managing website content, especially how to efficiently and beautifully convert large amounts of text containing URLs and email addresses into clickable hyperlinks is a concern for many content operators. Manually adding<a>Tags are not only time-consuming and labor-intensive, but also prone to errors. Fortunately, Anqi CMS provides two very practical template filters -urlizeandurlizetruncThey can automatically complete this task, greatly enhancing the efficiency and user experience of content publishing.

urlizeFilter: Make your URL and email 'alive'.

urlizeThe filter is an intelligent tool, its main function is to automatically identify URLs in text (whetherhttp:///https://they start withwww.Beginning with, even in pure domain name form) and email addresses, and convert them to standard HTML hyperlinks (<a href="...">...</a>). It's also thoughtful that it automatically adds properties for these external links to follow SEO practices.rel="nofollow"This helps the website better manage the weight transmission of external link authority.

Imagine that you are writing a technical article, referencing multiple external resource links, or listing customer service email addresses in a product description.If manually adding link tags one by one, it will undoubtedly take a lot of time.urlizeFilter, simply pass the content containing these texts to it, and it will instantly convert it into a clickable hyperlink, making your content come to life instantly.

Usage example:

Assuming your article content has a section of plain text:请访问我们的官网 https://en.anqicms.com 或发送邮件至 [email protected] 获取帮助。

In Anqi CMS templates, you can use it like this:urlizeFilter:

{# 假设 content 变量包含了上述文本 #}
<p>{{ content|urlize|safe }}</p>

After rendering, this text will automatically become:

<p>请访问我们的官网 <a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a> 或发送邮件至 <a href="mailto:[email protected]">[email protected]</a> 获取帮助。</p>

It should be noted that,|safeThe filter is indispensable here. BecauseurlizeThe filter generates HTML tags, if it is missing|safeThe template engine may escape these HTML tags for security reasons (for example, converting<Escape as&lt;), which may cause the link to display incorrectly.

urlizeThe filter also supports a boolean parameter to control the escaping of link content. For exampleurlize:trueorurlize:falseIn most cases, the default behavior is sufficient, but if you have special requirements for the display of link text, you can try it.

urlizetruncFilter: Smart truncation, keep the page tidy

In some cases, the URL may be very long, and if displayed in full, it may destroy the overall layout or aesthetics of the page. At this point,urlizetruncThe filter comes into play. Its basic function is consistent withurlizewhich automatically recognizes and converts URLs and email addresses into clickable links, and addsrel="nofollow". The difference lies in,urlizetruncAllow you to specify a length, when the link text exceeds this length, it will be automatically truncated and replaced with “…”, thus maintaining the neatness and consistency of the page.

For example, a very long technical document link may make the page look disorganized, but byurlizetrunc, you can control its display length to ensure the beauty of the content.

Usage example:

Assuming your text has a very long URL:这是一个关于安企CMS高级功能演示的超长链接:https://en.anqicms.com/docs/advanced-features/templates-and-filters/urlize-urlizetrunc-example.html

You want it to show only the first 25 characters (including "..."):

{# 假设 content 变量包含了上述文本 #}
<p>{{ content|urlizetrunc:25|safe }}</p>

After rendering, the text will look something like this:

<p>这是一个关于安企CMS高级功能演示的超长链接:<a href="https://en.anqicms.com/docs/advanced-features/templates-and-filters/urlize-urlizetrunc-example.html" rel="nofollow">https://en.anqicms.com/do...</a></p>

This way, even a long URL will not affect the overall layout of the page.

Why choose these two filters? What is their practical value?

  1. Efficiency improvement: Say goodbye to the繁琐 of manually adding links, process links in large amounts of text at one time, and greatly save the precious time for content editing and publishing.
  2. User experience optimizationVisitors do not need to copy and paste to click the link directly, which improves the interactivity and ease of use of the website.
  3. Content is tidy and beautifulEspeciallyurlizetruncIt can intelligently handle long links, avoiding content overflow or layout errors, keeping the page with a unified visual style.
  4. SEO-friendly: Automatically add external links torel="nofollow"Attribute, helps the website better control the link weight in search engine optimization and avoid unnecessary loss.
  5. Cross-platform consistency: Regardless of the source of the content, after the filter is processed, the display method of the links will remain consistent, enhancing the professionalism of the website.

In AnQi CMS,urlizeandurlizetruncThe filter is an indispensable tool for content operations.They achieve powerful automation functions with concise syntax, making content publishing more efficient and beautiful, while also considering the needs of search engine optimization.By proficiently using these two filters, you can easily create a user-friendly, content-oriented website.


Frequently Asked Questions (FAQ)

1. Why when usingurlizeorurlizetruncAfter, sometimes it is also necessary to add|safeFilter?Answer: This is becauseurlizeandurlizetruncThe filter will convert URLs and email addresses in the text to HTML<a>Label. The Anqi CMS template engine, to prevent potential XSS attacks (cross-site scripting attacks), defaults to escaping all output content, which means it will convert<to&lt;If missing.|safeFilter generated.<a>The tag will be displayed as plain text instead of being parsed by the browser as a clickable hyperlink. Adding|safeIt tells the template engine that this part of the content is safe HTML that does not need to be escaped and can be output directly.

2. What is the impact of using these two filters if my text does not contain any URLs or email addresses?Answer: There will be no impact.urlizeandurlizetruncThe filter will only search and process strings in the text that match the URL or email address format.If the text does not contain any strings that match these formats, they will return the original text unchanged, without changing the content, without causing any errors or additional overhead.So, even if it is uncertain whether the text contains a link, you can use it with confidence.

3. Can I apply these two filters to all the text in the article (rich text)?Yes, absolutely.When you need to process a large block of text containing URLs and email addresses (such as article content, comments, message board entries, etc.), you can pass these texts as variables to the filter.{% filter %}{% endfilter %}To enclose a large block of content, so that the filter will process the entire block of text. For example:

{% filter urlize|safe %}
    {{ article.Content }} {# article.Content 是文章正文内容 #}
{% endfilter %}

Or use it directly after a variable:

<p>{{ article.Content|urlize|safe }}</p>

Both methods can effectively convert URLs and email addresses in article content to clickable hyperlinks automatically.

Related articles

What are the differences between the `urlencode` and `iriencode` filters in URL parameter encoding, and which scenarios are they applicable to?

In website operation, the construction and processing of URL (Uniform Resource Locator) is a fundamental and critical task.It is crucial to properly encode URLs when dynamically generating links, handling user input as parameters, as it ensures the validity of the links, prevents garbled text, and potential security issues.AnQiCMS provides the `urlencode` and `iriencode` filters to help us better manage special characters in URLs.Although they are all used for encoding, their application scenarios and processing methods are different.

2025-11-08

How to safely truncate text with HTML tags using `truncatechars_html` and `truncatewords_html` filters to prevent structure damage?

In website content operation, we often need to display the summary or part of the content in different scenarios, such as on the list page, search results, or related recommendations.This is when you need to truncate the content.If the content is plain text, a simple character or word cutter can handle the task well.However, when the content is rich in HTML tags, the problem becomes complex: directly cutting may cause the HTML tags to be truncated, thereby destroying the page structure, causing display errors, and even affecting the user experience.

2025-11-08

How to handle truncation and add an ellipsis when truncating text with `truncatechars` and `truncatewords` filters, including HTML content?

In the increasingly fragmented information consumption era, the way websites present content directly affects user experience.Whether it is the abstract of an article list, the preview of product introduction, or various information stream cards, we need to convey the core information within a limited space while maintaining the page's neatness and uniformity of layout.This introduces the need for text truncation. AnQiCMS, as a fully functional content management system, understands this well and provides us with a powerful text truncation filter, especially its intelligent processing capabilities for HTML content, making content operations more relaxed.

2025-11-08

How do the `trim`, `trimLeft`, and `trimRight` filters remove spaces or specific characters from the beginning or end of a string?

In website operation and content management, we often encounter the need for string processing, especially in data entry, content display, or API interaction.For example, the user may have mistakenly entered multiple spaces before and after the text box, or some data may have been sourced with unnecessary specific characters.These seemingly minor issues may affect the layout and aesthetics of the content, even causing functional abnormalities.The AnQi CMS template engine provides a series of powerful filters to help us easily deal with these string cleaning tasks.Today, let's get to know the three very practical filters: `trim`

2025-11-08

How to calculate the number of words in a string with the `wordcount` filter in Anqi CMS template?

When processing text content in the AnQi CMS template, we often need to perform various operations on strings, such as counting words, cutting content, and so on.Among them, calculating the number of words in a string is a common requirement, which is very useful in content analysis, SEO optimization, and even estimating the reading time of an article.AnQi CMS provides a simple and powerful tool——`wordcount` filter.### `wordcount` filter: Core functionality and purpose The `wordcount` filter is specifically used to count the number of words in a given string

2025-11-08

How does the `wordwrap` filter automatically wrap long text to a specified length

In the daily operation of websites, we often encounter such situations: the content of the article is too long, or the text extracted from other places has not been formatted, resulting in the text content exceeding the container width when displayed on the web page, destroying the overall layout and seriously affecting the reading experience of users.Especially on mobile devices, long lines of text are more difficult to read.To solve this problem, Anqi CMS provides a very practical tool - the `wordwrap` filter, which can help us automatically wrap long text to the specified length, thereby optimizing the display effect of the content

2025-11-08

How does AnQiCMS ensure that website content displays perfectly on different devices?

In the digital age, website content can seamlessly adapt to various devices, from large desktop monitors to small smartphone screens, and is no longer an option, but the cornerstone of success.The user expects to have a smooth, beautiful, and fully functional experience regardless of which device is accessed.AnQiCMS as a product designed for efficient content management, provides a series of powerful and flexible features to ensure that your website displays content perfectly on any screen, making it shine on any screen.### AnQiCMS's multi-dimensional adaptive strategy AnQiCMS

2025-11-08

How to manage and present the content of multiple independent brand websites in AnQiCMS in a unified manner?

In the current digital age, the need for enterprises or content operators to manage multiple independent brand websites is increasingly common.Each brand may have a unique style, product line, or target audience, but how to efficiently manage these contents under a unified framework, achieve centralized display under specific scenarios, while maintaining the independence of each brand, is a challenge faced by many teams.AnQiCMS as an enterprise-level content management system, its multi-site management function provides a powerful solution for this.One of the design初衷 of AnQiCMS is to serve users with multi-site management needs

2025-11-08