How to make `urlize` output HTML links correctly without using the `safe` filter?

Calendar 👁️ 60

AnQiCMS's template system, with its powerful features and flexible customization, provides a solid foundation for content operations.In the content display, we often encounter the need to automatically convert URLs in plain text to clickable HTML links.urlizeThe filter is designed for this, it can intelligently identify URLs or email addresses in text and wrap them in<a>in the tag.

However, when usingurlizeWhen using a filter, a common practice is to|safeThe filter is used because AnQiCMS (and most modern template engines) default to escaping HTML entities for all content output through variables to prevent cross-site scripting (XSS) attacks and other security issues.This means, if there is not|safewith explicit instructions,urlizeGenerated<a href="...目标的URL...">链接文本</a>will be rendered as&lt;a href="..."&gt;链接文本&lt;/a&gt;The user sees a string of source code instead of a clickable link.|safeThe role of the filter is to inform the template engine: "This content has been confirmed as safe HTML, please output it directly without escaping."

But if for certain development specifications or special scenarios, we want to use without directly|safeleturlizeCorrectly output HTML links, has AnQiCMS provided the corresponding solution? The answer is yes, we can achieve this by using the template engine'sautoescapetag to achieve this goal.

AnQiCMS template engine provides a namedautoescapepowerful tag that allows us to precisely control the HTML escaping behavior of specific areas in the template.autoescapetag to receiveonoroffAs a parameter, it indicates whether the code block it wraps enables automatic escaping.

  • When using{% autoescape on %}When, it will force the automatic HTML escaping of all its internal content, even if some content may be marked as safe.
  • While we use{% autoescape off %}When, it will temporarily disable the automatic HTML escaping of its internal content. This means that any variable output or filter processing result within the scope of the tag pair will be treated as safe HTML output directly, without the need to use an extra `

Related articles

Does the `urlize` filter-generated link include the `title` or `alt` attributes of the original URL?

In Anqi CMS templates, the `urlize` filter can effectively convert URLs in text content into clickable HTML links.For many users, while using this convenient feature, they are also concerned about whether the generated link will automatically include `title` or `alt` attributes to enhance user experience or optimize SEO.

2025-11-09

Is there a global option in the AnQiCMS background settings that affects the behavior of `urlize` and `urlizetrunc`?

When using AnQiCMS to manage website content, we often encounter the need to automatically convert URLs or email addresses in text to clickable links.`urlize` and `urlizetrunc` are powerful tools to implement this function.They can not only automatically identify links in text but also automatically add the `rel="nofollow"` attribute to links after conversion, which is very practical for SEO optimization.However, many friends may be curious when using these filters

2025-11-09

Does the `urlize` filter support recognizing and converting IP address formatted URLs (e.g., `http://192.168.1.1`)?

In the daily use of AnQi content management system (AnQiCMS), template filters are an important tool for processing and displaying data.Among them, the `urlize` filter is often used for its convenience, to automatically convert URLs and email addresses in text content into clickable hyperlinks, and to automatically add the `rel="nofollow"` attribute to these links. This is very useful for handling user-generated content (UGC) or automatically identifying links in article text.### `urlize`

2025-11-09

How to implement different URL parsing strategies for different content types (articles, products, pages) in AnQiCMS?

In AnQiCMS, applying different URL parsing strategies for different content types such as articles, products, and pages is a key step to optimizing website structure, improving search engine optimization (SEO) and enhancing user experience.AnQiCMS powerful pseudo-static rule management and flexible content model design, making this requirement easy to implement. ### The Basics of Content Model and URL Structure One of the core strengths of AnQiCMS lies in its 'flexible content model'.

2025-11-09

The `urlize` filter does it attempt to verify the validity or accessibility of the URL?

In AnQiCMS (AnQiCMS) content operation, we often need to automatically convert text links or email addresses into clickable hyperlinks in the article text.To achieve this goal, the `urlize` filter is a very convenient and commonly used tool.However, many users might be curious whether this `urlize` filter, while converting links, would also try to verify the validity or accessibility of these URLs?In short, the answer is: **No**. `urlize` filter is in AnQiCMS

2025-11-09

How to flexibly adjust the truncation length when using `urlizetrunc`, for example, based on the device type?

How to ensure that information is presented in a**way to every visitor on the corporate website, which is the key to improving user experience and the effectiveness of content marketing.Especially when website content contains a large number of external links or long URLs, how to cleverly handle the display of these links so that they are both aesthetically pleasing and complete, while also adapting to different screen sizes of devices, has become a task that requires meticulous management.The `urlizetrunc` filter provided by AnQiCMS is the tool to solve this problem.

2025-11-09

Does the `urlize` filter support custom attributes of the generated `<a>` tag, such as `data-*`?

AnQiCMS (AnQiCMS) plays an important role in website construction and content operation with its efficient and flexible content management features.In daily content display and template development, we often take advantage of the built-in various filters to conveniently handle data.Among them, the `urlize` filter is a very practical tool that can automatically identify links in text and convert them into clickable HTML `<a>` tags.However, does the `urlize` filter support adding custom properties like `data-*` to these automatically generated `<a>` tags

2025-11-09

If the text contains duplicate URLs, will the `urlize` filter generate the same `<a>` tags repeatedly?

In daily website content operation, we often need to include various links in articles or pages, whether they point to other pages within the site or refer to external resources.AnQiCMS (AnQiCMS) provides many convenient tools to simplify this process, among which the `urlize` filter is a very practical feature.It can automatically identify URLs or email addresses in text and convert them into clickable HTML links.However, when using this convenient feature, a common question may arise in one's mind: if our text contains multiple identical URLs

2025-11-09