How to automatically identify URLs in text and convert them into clickable links in AnQiCMS`` tag?

Calendar 👁️ 65

In content management and website operation, we often need to convert URLs in text into clickable hyperlinks so that users can directly jump to access.Manual operation is not only inefficient, but also prone to omissions, especially on large-scale websites.AnQi CMS fully understands this pain point and provides an elegant and efficient solution that allows us to easily identify and convert URLs in text.

Make AnQiCMS automatically identify URLs in text and convert them into clickable links<a>The tag, we mainly use its powerful template filter function. Specifically,urlizeandurlizetruncThese filters are the reliable assistants for completing this task. They can intelligently scan text during content output, automatically identify URLs and email addresses, and wrap them in HTML.<a>in the tag.

Core Tool:urlizeandurlizetruncFilter

Imagine you have a piece of content that includes something like "Please visit"https://en.anqicms.comFor more details, or send an email to[email protected]Seek help. Such text. If displayed directly, these URLs and email addresses will just be plain text. But through AnQi CMS, they will automatically become:urlizeFiltered, they will automatically become:

请访问 <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 for you when converting links,rel="nofollow"Property, this is a good default setting for the website's SEO optimization, 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.Assuming the article content is stored inarchive.ContentIn this variable, you can call it like this in the template:

{{ archive.Content|urlize|safe }}

It needs to be emphasized here|safeThis filter. The Anqi CMS template engine, for website security, defaults to escaping all output content to prevent cross-site scripting attacks (XSS).This means if your content includes HTML code (likeurlizeGenerated<a>Tags), they are displayed as plain text.|safeThe purpose is to tell the template engine: "This content is safe, do not escape, parse and display it directly as HTML code." Therefore, remember to add it when usingurlizeorurlizetrunc.|safe.

Sometimes, a URL may be very long and it may not look good displayed directly on the page, affecting the layout. At this point,urlizetruncThe filter comes into play.urlizetruncfunction withurlizeSimilar, but it allows you to specify the maximum display length of the link text.This part, if it exceeds the length, will be automatically replaced with “…”, while maintaining the integrity of the link.For example, if you want the link text to display a maximum of 15 characters:

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

This way, even if the original link is very long, only the truncated text will be displayed on the page, for examplehttps://www.anqi...and clicking on it will still allow you to jump to the full link.

Application scenarios and operation points

These two filters are most commonly used in the following scenarios:

  • Article detail page: Convert the article text (archive.Content) to clickable links for all URLs and email addresses.
  • Product Description Page: Handle product details description (if it includes website or contact email).
  • Custom text field: If 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 variables that need to be automatically converted to URLs, and then add|urlize|safe(or|urlizetrunc:长度|safe)when outputting the variable in the template. Usually, this code is placeddetail.htmlorlist.htmlThis is the corresponding content area of this template file.

It is noteworthy that Anqi CMS has a feature that allows you to select whether to automatically filter external links in the "content settings" panel. This feature is related tourlizeThe filter is different, it is designed to handle 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 in coordination with your operational strategy.

By simply applying in the template.urlizeorurlizetruncFilter, the Anqi 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.


Frequently Asked Questions (FAQ)

1.urlizeDoes the filter automatically recognize all types of links? urlizeThe filter is mainly used to identify common URL formats such ashttp:///https:///www.domain names starting with, as well as domain names without a protocol, such asanqicms.com)and standard email address format. It can cover the vast majority of link types you want to automatically convert and will automatically addrel="nofollow"Property.

2. I can convert the following<a>Add custom attributes to the tag, for exampletarget="_blank"Should the link open in a new window?CurrentlyurlizeandurlizetruncThe filter mainly focuses on converting text into clickable links and adding them automaticallyrel="nofollow"attributes, but they themselves do not directly provide custom other HTML attributes such astarget="_blank"The option. If you need more fine-grained control, you can consider combining it with frontend JavaScript code or extending the functionality on the backend in Go language, but this will involve more in-depth development work.

3. Besides the main article content, where else is it suitable to useurlizeFilter?Any text field that may contain URLs or email addresses is suitable for use, except for the main content of the article.urlizeFilter, such as product details, the "About Us" page content on the website, the comment area (if external links are allowed to be displayed), and even some custom content model fields.Any text that you hope to be automatically recognized and converted into clickable links can be applied to this filter.

Related articles

How to remove HTML tags in AnQiCMS template and display only plain text content?

In AnQiCMS content management practice, sometimes we encounter the need: to extract pure text information from documents containing rich formats (such as bold, italic, images, links, etc.).This may sound contradictory, content management systems are inherently committed to the diverse display of content, why would one still 'strip' these formats?

2025-11-09

How does AnQiCMS's `urlizetrunc` filter truncate long URLs and display an ellipsis in the link?

The AnQiCMS `urlizetrunc` filter: Allows long URLs to elegantly transform into ellipses In daily website operations, we often need to reference external links in articles, comments, or product descriptions.These links may be intended to provide references, guide users to access related pages, or display product videos, etc.However, sometimes these URLs can be very long, not only occupying a large amount of page space, affecting the overall layout aesthetics, but also reducing the reading experience of the content.

2025-11-09

How to implement custom JSON-LD structured data in AnQiCMS templates to optimize search results display?

In today's digital marketing environment, having high-quality content is not enough. How to make search engines better understand and display this content is becoming increasingly important.Structured data, especially JSON-LD, is a powerful tool at our disposal that can help our website present more rich and attractive forms in search results, which is what we often call 'rich snippets'.

2025-11-09

How to format a `time.Time` type variable into a readable date string in GoLang in AnQiCMS?

In AnQiCMS, managing website content often involves the need to display dates and times, such as the publication time of articles, update time, or the last login time of users, etc.These time data are typically stored as `time.Time` type in the GoLang backend.When they are passed to the front-end template, if not properly formatted, they may be displayed as a string of hard-to-understand timestamps or the default Go language format, which is obviously not in line with the needs of the visitors we show.

2025-11-09

What is the core function of the `urlize` filter in the AnQiCMS template?

In AnQiCMS template development, handling links in content is a common and important link.The `urlize` filter was born to meet this need, it can greatly simplify the recognition and formatting of links in templates, while also taking into account user experience and search engine optimization.### `urlize` filter is the core function in AnQiCMS templates The core function of the `urlize` filter is to intelligently identify URLs and email addresses contained in a piece of plain text content

2025-11-09

How to parse a URL string into a clickable link in the article detail content of AnQiCMS?

In daily website content operations, we often need to display text content containing URL addresses on article detail pages, such as reference sources, recommended links, and so on.If these URL strings cannot be automatically parsed into clickable hyperlinks, users will not be able to jump directly, which not only affects the user experience but also reduces the readability and convenience of the content.

2025-11-09

Does the `urlize` filter support converting email addresses (such as `[email protected]`) into `mailto:` links?

When managing website content, we often need to convert URLs or email addresses in text to clickable links.Manually adding HTML tags one by one is time-consuming and prone to errors, especially when dealing with large amounts of content.Therefore, whether an efficient content management system can provide intelligent link parsing functionality is crucial for content operation efficiency.AnQiCMS as a content management system designed specifically for small and medium-sized enterprises and content operation teams, understands the importance of such needs.It has built-in many utility tools in the template engine to simplify this work, including `urlize`

2025-11-09

Does AnQiCMS's `urlize` filter automatically add the `rel="nofollow"` attribute to generated links?

In content operation, link management and search engine optimization (SEO) are inseparable important links.For those of us who use excellent content management systems like AnQiCMS, understanding the internal mechanism for processing links, especially whether filters like `urlize` automatically add the `rel="nofollow"` attribute, is crucial for precise control of the website's SEO performance.### AnQiCMS's `urlize` filter

2025-11-09