How to automatically scan and convert ordinary text content in the AnQiCMS template into clickable URL links or email addresses?

Calendar 👁️ 57

In website content operation, we often need to display some URLs or email addresses in articles or pages. If these addresses are only in plain text, users cannot directly click to jump, which not only affects the user experience but may also make it difficult for search engines to identify these valuable link information.It is fortunate that AnQiCMS provides a very convenient built-in feature that can help us automatically convert this ordinary text content into clickable hyperlinks or email links, making the website content more interactive and professional.

To implement this feature, we mainly use several 'filters' provided by the AnQiCMS template engine.These filters are like text processing tools, capable of formatting, converting, or filtering data.

Core Tool:urlizeThe filter makes websites and email addresses 'alive'

AnQiCMS providedurlizeThe main function of the filter is to scan a text content and automatically identify the strings that match the URL format (for examplehttp://example.com/www.example.com/example.com) and email address (for example[email protected]), and convert them into HTML's<a>tags to make them clickable links.

For example, you may have entered such text in the article content field:欢迎访问安企CMS官网:https://en.anqicms.com,有疑问请发送邮件至 [email protected]

In the template, assume that your article content is output by{{ archive.Content }}Then you can use it like this:urlizeFilter:

<div>
    {{ archive.Content|urlize }}
</div>

However, when usingurlizeWhen filtering, there is a very important detail that needs to be paid attention to.AnQiCMS To ensure website security, it is default to escape all content output to the page with HTML entities.This means, if you use directly{{ archive.Content|urlize }}generated by the filter,<a href="...">Tags will be escaped into&lt;a href=&quot;...&quot;&gt;This format still causes the link to be unclickable.

To make the browser parse and display the link as clickable, we still need to add an extra one.|safefilter.safeThe filter tells the template engine that this content is safe, it does not need to be HTML-escaped, and can be output directly in its original HTML structure.

So, the correct usage should be:

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

This, when the user visits the page,https://en.anqicms.comand[email protected]will automatically become a clickable link, enhancing user experience.

Advanced usage:urlizetruncFilter, controls the display length of the link

Sometimes, some URLs may be very long, and displaying them directly on the page may seem redundant and even affect the layout aesthetics. In response to this, AnQiCMS providesurlizetruncFilter. It isurlizeBased on it, a truncation feature has been added, which can specify the maximum length of the link text display, and the parts exceeding it will be automatically abbreviated with an ellipsis....Replaced, but the actual link address is still complete.

urlizetruncThe filter requires a parameter indicating the maximum number of characters you want the link text to display.

For example, if you want the link text to display a maximum of 30 characters:

<div>
    {{ archive.Content|urlizetrunc:30|safe }}
</div>

so ifhttps://en.anqicms.com/long-page-path-with-many-wordsSuch links are recognized, and they may display as:https://en.anqicms.com/long-p...But when clicked, they will still correctly jump to the full long link.

In which content areas can these filters be used?

These powerful filters are not limited to article content, you can apply them to any text output that may contain URLs or email addresses in the AnQiCMS template. Common use cases include:

  • Document details page (archiveDetail):
    • archive.Content(article/product detail content)
    • archive.Description(article/product introduction)
    • ByarchiveParamsCustom text field obtained.
  • Category detail page (categoryDetail):
    • category.Content(category introduction content)
    • category.Description(category brief)
  • Single page detail page (pageDetail):
    • page.Content(single page content)
    • page.Description(single page summary)
  • system configuration or contact information (system,contact):
    • If you have customized a text field in the background that contains a URL or email, such as a social media homepage link or customer service email, you can also apply these fields tourlize|safe.

For example, you may havesystemDefined a custom "company homepage" field in theCompanyHomepage:

<p>
    访问公司主页:{% system companyHomepage with name="CompanyHomepage" %}{{ companyHomepage|urlize|safe }}
</p>

Summary

ByurlizeandurlizetruncThese two filters combined|safeThe use, AnQiCMS template can easily achieve the automatic conversion of URLs and email addresses in ordinary text, making your website content more interactive and readable, and more friendly to search engines.This not only optimizes the user experience, but also effectively enhances the professionalism of the website.


Frequently Asked Questions (FAQ)

  1. Q:urlizeThe filter will automatically add to external linksnofollowShould I add the attribute?A: Yes, according to AnQiCMS design,urlizeThe filter will automatically recognize and convert the text to<a>tags, and automatically add external links to the generated links.rel="nofollow"Property. This is a good default setting for SEO practices, as it can prevent passing the weight of your website to unrelated external links.

  2. Q: Why did I addurlizeAfter that, is the link still displayed as plain text on the page and not clickable?A: This is very likely because you forgot tourlizeAdd after the filter|safeFilter. AnQiCMS template engine, for security reasons, defaults to escaping all output HTML code, resulting in<a>tags being displayed as plain text. Adding|safeA filter can tell the template engine that the content is safe, allowing it to output the HTML structure unchanged, making the link clickable.

  3. Q:urlizeandurlizetruncWhat is the difference between these filters, and how should I choose?A:urlizeThe filter recognizes URLs and email addresses in the text and converts them into complete HTML links. AndurlizetruncInurlizeOn the basis of this, the function of controlling the display length of link text has been added. If you do not want the link text on the page to be too long and affect the layout beauty, you can useurlizetruncTruncate the display to a suitable character count. If the link length is not an issue, or you want to display the full link text, thenurlizeit is sufficient. Both need to be coordinated.|safeThe filter must be applied to correctly display clickable links.

Related articles

How does AnQiCMS handle automatic line breaks for long articles or description text to improve the readability of the front-end page?

In website content operation, the presentation effect of long articles or large sections of descriptive text directly affects the user's reading experience.If content is piled together without good layout and proper line breaks, even the most精彩 content will make readers reluctant.AnQiCMS is a content management system that focuses on user experience and provides various mechanisms to cleverly handle automatic line breaks in long texts, thereby greatly improving the readability of the front-end pages.### Basic Coverage: Markdown Editor and Natural Line Breaks Firstly, AnQiCMS is well-supported by the built-in Markdown editor.

2025-11-08

What are the common practical application scenarios for the `cut` filter when removing specified characters from any position in the AnQiCMS template string?

In AnQiCMS template design, in order to present the content effect that best meets expectations, we often need to process strings finely.Among the many built-in filters, the `cut` filter is a seemingly simple yet extremely practical tool.Its core function is to remove the specified characters from any position in the template string, which makes it have a unique application value in content cleaning, formatting, and enhancing the user reading experience.The `cut` filter works very directly: it traverses the target string and removes all segments that match the specified character

2025-11-08

How to batch remove leading, trailing spaces or specific characters from AnQiCMS template strings for data cleaning and formatting?

When using AnQiCMS for website content management, we often encounter situations where we need to fine-tune the text output in templates.Whether it is data obtained from a database or content entered in an editor, it may contain extraneous spaces, line breaks, or even specific characters that are not intended to be displayed.In order to ensure the tidiness, consistency of website content, and to enhance user experience and search engine friendliness, it is particularly important to clean and format the data.AnQiCMS provides a flexible and powerful template engine, its syntax is similar to Django

2025-11-08

How to calculate the total number of times a specific keyword appears in a line string or an array in the AnQiCMS template?

In AnQiCMS (AnQiCMS) template development, we often need to flexibly handle various content on the page.For example, you may need to analyze the frequency of a specific word in an article, or check how many times an element is mentioned in a list.The AnQi CMS powerful template engine provides a variety of practical filters (Filter) that can help you easily meet these needs.The function used to calculate the total number of times a specific keyword or element appears is exactly the focus of our discussion today.### Core Function: `count`

2025-11-08

How to control the display length of the link text and automatically add an ellipsis when the `urlizetrunc` filter converts URLs in the AnQiCMS template to links?

In website content management, we often need to display various links on the page, whether it is the cited URL in the article or the external links submitted by users.However, these links are sometimes very long, not only affecting the aesthetics of the page, but also possibly destroying the original layout, making the page look disorganized.AnQiCMS provides a very practical template filter——`urlizetrunc`, which can help us elegantly solve this problem, making long links clickable while presenting them in a concise and beautiful way.`urlizetrunc`

2025-11-08

How to safely display a string that may contain HTML tags in AnQiCMS template and prevent XSS injection attack?

In website operation, ensuring the security of content is a crucial link, especially when your website allows users to submit content or display data from different sources.Cross-site scripting (XSS) attacks are one of the common threats that can lead to data leakage of website users, session hijacking, or even website tampering.For those of us who use AnQiCMS to manage content, understanding how to safely display strings that may contain HTML tags is the foundation for preventing such attacks.The AnQiCMS template engine handles variable output when

2025-11-08

In AnQiCMS template design, in which cases do you need to explicitly use the `safe` filter to ensure that rich text content is rendered correctly as HTML?

During the template design process of AnQiCMS, it is crucial to understand when to explicitly use the `safe` filter to ensure that rich text content is rendered correctly while maintaining website security.AnQiCMS's template engine, similar to many modern CMSs, in order to prevent security vulnerabilities such as cross-site scripting attacks (XSS), it defaults to escaping all content output through the `{{ variable }}` method.This means, if you directly output a text containing HTML tags, for example `{{ article content }}`

2025-11-08

How to accurately remove all HTML tags from AnQiCMS template HTML content, leaving only plain text information?

In AnQiCMS template design, we often encounter situations where we need to display content but do not want to show the HTML tags contained in it.For example, we may need to extract the plain text summary of an article or display unformatted category descriptions on a list page.Directly outputting content that includes HTML may disrupt the page layout and even pose security risks.AnQiCMS's powerful template engine provides a concise and efficient solution, helping us accurately remove HTML tags and retain only plain text information.###

2025-11-08