How does AnQiCMS automatically convert URLs and email addresses in plain text to clickable HTML links?

Calendar 👁️ 75

In website content creation, we often need to mention URLs and email addresses in articles, descriptions, or comments.If this information is only plain text, users will not be able to click through directly, which undoubtedly affects the user experience and the efficiency of information transmission.AnQiCMS as an efficient content management system fully considers this requirement and built-in intelligent functions that can automatically convert URLs and email addresses in plain text to clickable HTML links.

AnQiCMS automatically converts link functionality:urlizeFilter

AnQiCMS uses a template engine syntax similar to Django, whereurlizeThe filter is the key to implementing this function. It can intelligently scan text content and identify URLs (such ashttp:///https:///www.addresses starting with the prefix, even some naked domain names likeanqicms.comAnd email address format such as[email protected]The string and automatically wraps it in<a>tags to make them clickable links.

The convenience of this feature lies in the fact that you do not need to manually write HTML code to create links. Simply pass the plain text content tourlizeThe filter, the system will automatically handle it, greatly improving the efficiency of content editing. In addition,urlizeThe filter will also automatically add it kindly when converting links.rel="nofollow"Attribute, this is very helpful for the SEO optimization strategy of the website, especially in managing the weight transmission of external links.

Usage:

In the AnQiCMS template,urlizeFilter through the pipe symbol.|Apply to any variable containing text. SinceurlizeThe conversion will generate HTML tags, in order to ensure that these HTML tags can be parsed correctly by the browser and not displayed as plain text, we usually need to use in conjunction withsafeFilter. Otherwise, you might see something similar<a href=\"...\">...</a>Such original HTML code.

For example, if you have a variable namedarchive.ContentThe variable stores the article content, which includes plain text URLs and email addresses, and you can use it like this:

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

This code will convertarchive.ContentConvert all URLs and email addresses to clickable links and render them safely on the page.

Optimize the display of long links:urlizetruncFilter

For very long URLs, if displayed in full on the page, it may cause layout confusion and affect the overall aesthetics. AnQiCMS also providesurlizetruncFilter, it isurlizeand it adds the function of truncating link text.

urlizetruncYou can specify the maximum length of the link text displayed. If the text length of the original URL exceeds this limit, it will be abbreviated after the specified length....Replace the overflow part while maintaining the validity of the link.

Usage:

urlizetruncThe filter accepts a numeric parameter indicating the maximum display length of the link text (including an ellipsis).

<p>{{ long_description|urlizetrunc:50|safe }}</p>

In the above example,long_descriptionIf any link text exceeds 50 characters, it will be truncated and displayed...However, the underlying link address is still complete. This is very practical for scenarios such as news summaries and product descriptions that require a clean page.

Application scenarios and value

These filters can be used in any position of the AnQiCMS template, especially suitable for the following scenarios, bringing practical value to your content operation:

  1. Article detail pageEnsure that the links and email addresses in the displayed article content, abstract, keywords, etc. are clickable to enhance the convenience of users in obtaining relevant information.
  2. Category or single page descriptionIn the category introduction, about us and other single-page description text, it is convenient for users to quickly access related resources or contact information.
  3. User messages or commentsAutomatically process links and email addresses submitted by users to enhance interactivity (Please note that for user-generated content, additional security filtering measures may also be required).
  4. Custom fieldIf the content model contains custom text fields for storing URLs or email addresses, these filters can also be used to ensure that the frontend display and functionality are consistent and complete.

ByurlizeandurlizetruncFilter, AnQiCMS helps you easily achieve:

  • Enhancing user experience: Users do not need to copy and paste, just click to access, reducing the threshold of operation.
  • Optimize content display: EspeciallyurlizetruncMake long links not destroy the page layout, keep the interface neat.
  • Indirectly assist SEO: Automatically added.nofollowThe attribute helps website administrators better control the impact of external links on the site's weight, which is one of the standard SEO practices.
  • Improve operational efficiency: The content publisher does not need to pay attention to the HTML format of the link, focusing on content creation itself.

Frequently Asked Questions (FAQ)

1.urlizeandurlizetruncWhat are the main differences of the filter? urlizeThe filter will completely convert URLs and email addresses in the text into clickable links, with the link text being the original URL or email address. AndurlizetruncAt the same time, you can specify a maximum length, and if the original link text exceeds this length, it will be truncated and an ellipsis will be added at the end....To keep the page neat.

2. Why do you need to addurlizeorurlizetruncAfter that, the displayed is HTML code instead of a clickable link?This is usually because you forgot to add at the end of the filter chain.safeFilter. AnQiCMS template engine, for security reasons, defaults to escaping all output content to prevent cross-site scripting attacks (XSS).urlizeandurlizetruncThe generated is an HTML tag, if it is not appliedsafeFilter, these tags themselves are escaped to plain text, causing the displayed on the page is<a href="...">such code.

3.urlizeAutomatically addedrel="nofollow"Can the attribute be removed or modified?According to the default design of AnQiCMS,urlizeThe filter will automatically add to the converted linkrel="nofollow"Property. This is a built-in behavior designed to help websites better manage the SEO impact of external links.The option to directly remove or modify this property through the template filter itself is not publicly provided at present.If you have specific requirements, you may need to consider manually inserting links HTML at the time of content publication, or through other frontend JavaScript methods for processing.

Related articles

How to control the automatic escaping of HTML content with the `autoescape` tag in AnQiCMS templates?

In AnQiCMS template development, for the safety of the website, the system defaults to automatically escaping all HTML content output to the page. This means that when you directly output a variable containing special HTML characters in the template, for example, `<script>alert('XSS')</script>`, AnQiCMS will convert it to `&lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;`

2025-11-08

How to handle JavaScript code output containing special characters (such as `&lt;script&gt;`) in AnQiCMS?

In website operation, we sometimes need to output custom JavaScript code on the page, which may be to implement specific interactive functions, integrate third-party service scripts (such as statistical codes, advertising codes), or add some dynamic effects to the page.However, when these JavaScript codes themselves contain some special characters, especially HTML tags (such as `<script>`), if not handled correctly, it may cause the page to display abnormally, disable functions, or even bring serious security vulnerabilities.

2025-11-08

What are the differences and usage scenarios between the `escape` filter and the `e` filter in AnQiCMS?

In the development of Anqi CMS templates, we often encounter the need to handle the security of content display, especially when the content may contain user input or be obtained from external sources.It is particularly important to escape special characters at this time to prevent potential cross-site scripting attacks (XSS).AnQi CMS provides the `escape` and `e` filters to help us deal with such issues, they have the same function, and `e` is just an abbreviation alias of `escape`.

2025-11-08

How does AnQiCMS template escape HTML to prevent XSS attacks when displaying user submitted content?

In today's network environment, website security is of great concern to operators, among which cross-site scripting attacks (XSS) are one of the common security threats.XSS attacks inject malicious scripts into web pages, steal user data, alter page content, and even control user sessions.AnQiCMS as a content management system that focuses on security, built a series of powerful HTML escaping mechanisms to effectively prevent such attacks when processing user submitted content and displaying it in templates.

2025-11-08

What are the differences between the `urlize` and `urlizetrunc` filters in converting URLs to links?

In Anqi CMS, when processing text content, we often need to automatically convert the URLs or email addresses contained within into clickable links.This not only improves the user experience, but also helps search engines better understand the content of the page.Therefore, AnQi CMS provides two very practical filters: `urlize` and `urlizetrunc`.Their core function is to intelligently convert URLs and email addresses in text to the HTML `<a>` tag, but there are key differences in specific application scenarios and effects.

2025-11-08

How to convert a newline character (`\n`) in the AnQiCMS template to the HTML `<br/>` tag or `<p>` tag?

In website content operation, we often encounter such situations: when the text content extracted from the database, or the text entered in the background plain text editor, is displayed on the front-end template, the originally clear line breaks become long sentences stacked together.This is because the browser defaults to not parsing newline characters (`\n`) as line breaks in HTML.To solve this problem in the AnQiCMS template, we need to use the powerful template filter function of AnQiCMS to display text content as expected in line.

2025-11-08

What are the differences between the `linebreaks` and `linebreaksbr` filters in handling text line breaks?

In AnQi CMS template development, handling newline characters in text content is a common requirement.Sometimes we want to display the text entered by the user in a structured paragraph format, and sometimes we only want to simply convert each newline into an HTML line break tag.At this point, the `linebreaks` and `linebreaksbr` filters come into play.They can all handle line breaks, but in actual applications, the generated HTML structures and semantics are quite different.### `linebreaks` filter

2025-11-08

How does AnQiCMS's 'Anti-Capture Interference Code' feature achieve content protection through HTML level processing?

In the era of explosive digital content, the value of original content is becoming increasingly prominent, but the problems of content collection and plagiarism that come with it also cause headaches for countless content creators and enterprises.The dilution of SEO (Search Engine Optimization) effects, damage to brand influence, and even legal disputes are all possible negative impacts of content being maliciously collected.

2025-11-08