In AnQi CMS, when processing text content, we often need to automatically convert the included URLs or email addresses into clickable links.This not only improves the user experience, but also helps search engines better understand the content of the page.urlizeandurlizetrunc。They all have the core function of intelligently converting URLs and email addresses in text to HTML.<a>Labels, but there are significant differences in specific application scenarios and effects.

urlizeFilter: Make links jump off the page

urlizeFilters are mainly used to extract URLs from a block of plain text (such ashttp://example.com/www.example.com)and email addresses(for example[email protected])will be automatically recognized and converted to standard clickable HTML hyperlinks. It will automatically addrel="nofollow"Properties, this is very important for the SEO strategy of the website, it can avoid unnecessary weight loss and at the same time indicate to search engines that these links are user-generated or external links and should not pass weight.

The advantage of this filter lies in its automation and convenience.You do not need to manually write complex regular expressions to match and replace links, just simply apply it to the text variable that needs to be processed.It is very suitable for handling article text, detailed descriptions, or any area where you want users to be able to click and access the full link directly.

Example Usage:

Suppose your content variablearchiveContentcontains the following text:请访问我们的官网:www.anqicms.com,或发送邮件至 [email protected] 获取更多信息。

ApplyurlizeAfter filter:

{# 假设 archiveContent 是您要处理的文本内容 #}
{{ archiveContent|urlize|safe }}

Will be displayed on the page:

请访问我们的官网:<a href="http://www.anqicms.com" rel="nofollow">www.anqicms.com</a>,或发送邮件至 <a href="mailto:[email protected]">[email protected]</a> 获取更多信息。

Please note that due tourlizeThe filter will generate HTML tags, so it must be used in conjunction with|safeFilter to ensure that the browser can correctly parse and render these links instead of displaying the HTML code as is.

urlizetruncFilter: Optimize display, streamline text

urlizetruncFilter isurlizeOn this basis, an extremely useful feature has been added:Link text truncation.它同样能够自动识别并转换文本中的网址和电子邮件地址为可点击的超链接,但在此基础上,您还可以指定一个数字作为参数,限制链接文本的显示长度。urlizetruncIt will be truncated and an ellipsis will be added at the end,...).

The value of this feature is mainly reflected in the consideration of page layout and visual tidiness.In some space-limited areas, such as article comment sections, sidebar widgets, list item summaries, or brief prompts, the original full URL may appear too long and destroy the beauty of the page or occupy too much space.urlizetruncCan intelligently shorten these long links while preserving the link's usability and maintaining the interface's neatness.

Example Usage:

Assuming your review content variablecommentTextcontains the following text:我发现了一个很棒的资源,链接是:https://very-long-and-descriptive-url-for-a-great-resource.com/path/to/specific/page/index.html

ApplyurlizetruncFilter and specify the truncation length to 25:

{# 假设 commentText 是您要处理的评论内容 #}
{{ commentText|urlizetrunc:25|safe }}

Will be displayed on the page:

我发现了一个很棒的资源,链接是:<a href="https://very-long-and-descriptive-url-for-a-great-resource.com/path/to/specific/page/index.html" rel="nofollow">https://very-long-and-...</a>

Similarly,urlizetruncThe filter also generates HTML, therefore it must also be used accordingly.|safeUsed together with the filter.

How to choose:urlizeOrurlizetrunc?

Choose to useurlizeOrurlizetruncIt mainly depends on the context of content presentation and the needs of user experience:

  • ChooseurlizeWhen you want users to see the full link information, and there is enough space on the page, for example, in the main text of the article, product details, important announcements, and other areas.It ensures the complete communication of information.
  • ChooseurlizetruncWhen you need to display links in limited space, prioritize the page's tidiness and readability, for example, in comment lists, sidebar recommendations, list summaries, and user activity areas.It can optimize the visual effects without sacrificing the link function.

Understanding the differences between these two filters and applying them flexibly according to the actual situation will help you build beautiful and functional website content in the Anqi CMS.


Common Questions (FAQ)

  1. Why do you need to addurlizeorurlizetruncAfter, what's displayed on the page is an HTML tag instead of a clickable link?This is usually because you forgot to add at the end of the filter chain|safeFilter. The template engine of AnQi CMS defaults to escaping all output content as HTML entities for security reasons, to prevent XSS attacks. Whenurlizeorurlizetruncgenerated<a>After labeling such HTML code, if missing|safeThese tags will be escaped and displayed as is, rather than being parsed by the browser as clickable links. The correct usage is:{{ 您的变量|urlize|safe }}or{{ 您的变量|urlizetrunc:数字|safe }}.

  2. urlizeandurlizetruncCan it automatically recognize all forms of URL?These filters can intelligently recognize common URL patterns, including thosehttp:///https://starting with, as well as commonwww.yourdomain.comThe URL format, even withoutwww.the naked domain name (such asyourdomain.com) and email addresses. Their design goal is to cover most of the link formats that users naturally input in text.

  3. urlizeandurlizetrunc生成的链接会自动添加rel="nofollow",可以修改或移除这个属性吗?根据安企CMS的设计,urlizeandurlizetrunc过滤器在将URL转换为链接时,会自动附加rel="nofollow"Property.This is an integrated security and SEO optimization mechanism, mainly used to avoid unnecessary page authority transfer due to user-generated content (such as comments) or external links.nofollowProperties. If you have special requirements that need to customize the link properties, you may need to consider handling it on the front-end JavaScript level, or through backend customized development.