In content management and website operation, we often need to convert web addresses in text into clickable hyperlinks so that users can directly access them.Manual operation is not only inefficient but also prone to omissions, especially on websites with a large amount of content.A secure CMS knows this pain point and provides an elegant and efficient solution, allowing us to easily recognize and convert URLs in text.

How to make AnQiCMS automatically recognize URLs in text and convert them to clickable<a>Labels, we mainly use its powerful template filter function. Specifically,urlizeandurlizetruncThese two filters are powerful assistants for completing this task. They can intelligently scan text during content output, automatically identify URLs and email addresses, and wrap them in HTML tags.<a>the tag.

Core Tools:urlizeandurlizetruncFilter

Imagine you have a piece of text that includes something like “Please visithttps://en.anqicms.comFor more details, or send an email to[email protected]Seek help. Such text. If displayed directly, these website URLs and email addresses are just plain text. But after filtering through the CMS, they will automatically become:urlize

请访问 <a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a> 了解更多详情,或者发送邮件到 <a href="mailto:[email protected]">[email protected]</a> 寻求帮助。

Is it not very convenient? And,urlizeThe filter will also automatically add external links when converting links,rel="nofollow"Properties, this is a good default setting for the SEO optimization of the website, which can effectively control the flow of page weight.

UseurlizeThe filter is very simple, it is usually applied to the text content variable you want to process.For example, on the article detail page, we usually need to handle the main content of the article.archive.ContentYou can call it like this in the template:

{{ archive.Content|urlize|safe }}

This needs to be emphasized specially.|safeThis filter.The template engine of AnQi CMS defaults to HTML-escape all output content for website security to prevent cross-site scripting (XSS) attacks.urlizegenerated<a>Tags),they will be displayed as plain text.|safeThe role of this value is to inform the template engine: "This content is safe, please do not escape, and display it as HTML code directly." Therefore, be sure to add it when usingurlizeorurlizetrunc.|safe.

Sometimes, the URL may be very long and displaying it directly on the page may not look aesthetically pleasing or affect the layout.urlizetruncThe filter comes into play.urlizetruncfunctionality isurlize类似,但它允许你指定链接文本的最大显示长度。The part exceeding this length will automatically be replaced with “…”, while maintaining the integrity of the link.

{{ archive.Content|urlizetrunc:15|safe }}

So, even if the original link is very long, only the truncated text will be displayed on the page, for example,https://www.anqi...and you can still click to jump to the full link.

Application scenarios and key points of operation

这两个过滤器最常见的应用场景包括:

  • Article details page:将文章正文(archive.Content)中的所有URL和邮箱地址转换为可点击的链接。
  • 产品描述页:Handle product details (if it includes a website or contact email).
  • Custom text fieldIf your content model defines other text fields that may contain URLs, you can also handle them in the same way.

In practice, you just need to find the text variable that needs to be automatically converted to a URL, and then add|urlize|safe(or}|urlizetrunc:长度|safeto the template when outputting the variable.detail.htmlorlist.htmlThis is the corresponding content area of this template file.

It is worth noting that the Aiqi CMS also has a feature, that is, you can choose whether to automatically filter external links in the "Content Settings" section of the backend. This feature is related tourlizeThe filter is different, as it processes external links at the time of content publishing (such as removing or addingrel="nofollow"), whileurlizeThe content is dynamically converted when displayed on the front-end page. Both can be协同 used according to your operational strategy.

By simply applying in the templateurlizeorurlizetruncFilter, the CMS can greatly enhance the readability and interactivity of content, while freeing content operators from tedious manual link work, making website content management more efficient and intelligent.


Common Questions (FAQ)

1.urlizeDoes the filter automatically recognize all types of links? urlizeThe filter is mainly used to recognize common URL formats (such as)http:///https:///www.Websites that start with, as well as domain names without a protocol, such asanqicms.com)and the standard email address format. It can cover the vast majority of the link types you want to convert automatically, and it will automatically add.rel="nofollow"properties.

2. I can be in the converted<a>Add custom attributes to tags, for exampletarget="_blank"Should the link open in a new window?CurrentlyurlizeandurlizetruncThe filter mainly focuses on converting text to clickable links and adding them automaticallyrel="nofollow"Properties, but they do not directly provide custom other HTML attributes (such astarget="_blank"The options. in English.If you need more fine-grained control, you can consider combining front-end JavaScript code to handle this, or extending this feature on the back-end side in Go language, but this will involve more in-depth development work.

3. Besides the main content of the article, where else is it suitable to useurlizeFilter?In addition to the main text of the article, any text field that may contain a website or email address is suitable for useurlizeFilter, such as product details, the "About Us" page content on the website, comment sections (if allowed to display external links), and even some custom content model fields.Any plain text content that is expected to have URLs automatically recognized and converted into clickable links can try applying this filter.