How to automatically convert a plain text URL into a clickable hyperlink in content display?

Calendar 👁️ 101

It is crucial to provide users with a smooth and convenient browsing experience in website operation.When our articles, pages, or product descriptions contain URLs in plain text form, and users need to manually copy and paste to access them, it will undoubtedly greatly reduce their user experience.AnQi CMS understands this well, providing a simple and efficient method to automatically convert these plain text URLs into clickable hyperlinks, thereby bringing the content to life.

Make plain text URLs automatically 'come alive': The hyperlink conversion technique in AnQi CMS

Imagine you are browsing a fascinating article, and it mentions a useful external resource link, but it is just plain text.At this point, you need to manually select, copy, and paste it into the browser address bar, which is not only麻烦(troublesome), but also prone to errors.If this link could jump directly, wouldn't it be more convenient?In Anqi CMS, we can fully achieve this, making the professionalism and user-friendliness of the website content even better.

The AnQi CMS template system is very flexible, it utilizes powerful Filter features, among whichurlizeandurlizetruncThese filters are the 'secret weapon' we use to implement automatic conversion of plain text URLs.

The core feature revelation:urlizeFilter

urlizeThe principle of the filter is very simple: it intelligently scans the specified text, automatically identifying the part that conforms to the URL format (such ashttp:///https://even the websites starting withwww.The domain at the beginning, as well as the email address) string, then wrap it in standard HTML<a>Tags to make it clickable.

To ensure that the converted hyperlink is displayed correctly as a clickable HTML element and is not escaped as plain text by the browser, we need to use it in conjunction with|safefilter.|safeTell the browser that this content is safe HTML, which can be directly parsed and rendered.

Basic usage example:

Assume that the content of your article is stored in a folder namedarchiveContentIn the variable, you want all plain text URLs to become clickable links, then in the template file, you can write it like this:

<div>
    {{ archiveContent|urlize|safe }}
</div>

Through such processing, the original http://www.anqicms.comwill be automatically converted to<a href="http://www.anqicms.com" rel="nofollow">http://www.anqicms.com</a>and will be increased by defaultrel="nofollow"attribute, which is also a friendly practice for SEO.

Fine control:urlizetruncFilter

Sometimes, a very long URL may appear in the article, which can make the page look bulky and affect its aesthetics. At this time,urlizetruncThe filter comes into play. It can not only convert plain text URLs to hyperlinks, but also truncate the display text of the hyperlinks to the specified length and display them as...But the actual address of the link itself remains intact.

Example of usage:

If you want the display text of the link to be no longer than 30 characters, you can use it like this:

<div>
    {{ archiveContent|urlizetrunc:30|safe }}
</div>

Such, a long URL likehttps://en.anqicms.com/long-article-title-with-many-words-and-parametersmay be displayed as<a href="https://en.anqicms.com/long-article-title-with-many-words-and-parameters" rel="nofollow">https://en.anqicms.com/long-article...</a>while maintaining the link function and optimizing the page layout.

in applications of different content types

This feature is very flexible, almost applicable to any scenario in AnQi CMS that requires displaying text content:

  • Article detail page:This is the most common application scenario, used when displaying article content(archive.Content)
  • Single page content:For example, on the “About Us” or “Contact Us” page, if it contains a website, the single page content (page.Content) can be filtered using this filter.
  • Category description or Tag description:If you include a URL in the description of a category or tag, you can alsocategory.Descriptionortag.DescriptionApply this filter to enhance the interactivity of these pages.

Operation steps and precautions

To apply these filters, you need to modify the template file used by the website.

  1. Confirm the template file that needs to be modified:通常,文章详情页是template/你的模板目录/模型table/detail.htmlfor exampletemplate/default/article/detail.html),单页面是template/你的模板目录/page/detail.htmlIf you are not sure which file it is, you can check the template file structure in the background template design.

  2. Find the corresponding variable for the displayed content:In the template file, locate the code line responsible for displaying the main content (such as the article body, single page content). It will usually be like{{ archiveContent|safe }}/{{ pageContent|safe }}or{{ categoryContent|safe }}in this form.

  3. addurlizeorurlizetruncFilter:Modify the content variable to{{ archiveContent|urlize|safe }}or{{ archiveContent|urlizetrunc:30|safe }}.

    Special reminder: The processing order of Markdown contentIf your content has enabled the Markdown editor, Anqi CMS will first render the Markdown syntax into HTML. In this case,urlizeThe filter should be applied after Markdown rendering. For example, ifarchiveDetailtags are used to render Markdown content, you can organize the filters like this:

    {%- archiveDetail articleContent with name="Content" render=true %}
    {{articleContent|urlize|safe}}
    

    Hererender=trueFirst, convert Markdown to HTML, thenurlizeThen process the plain text URLs within, finallysafeEnsure that the HTML displays correctly.

  4. Save and update the cache:After modifying the template file, remember to save it. If the website has enabled caching, you may also need to clear the "Update Cache" function in the AnQi CMS backend to ensure that the changes take effect.

By following these simple steps, you can automatically convert plain text URLs in the website to clickable hyperlinks, greatly enhancing user experience and making the website content more vivid and practical.


Frequently Asked Questions (FAQ)

1. I added in the template|urlizeBut the plain text link still can't be clicked, what's wrong?This is likely because you forgot to add|safefilter.urlizeThe filter will convert plain text URLs to HTML<a>tags, but if there are none|safeThe cooperation, the template engine may escape the HTML special characters for security reasons, resulting in the final display still being plain text format and not clickable. Make sure your code is{{ 变量名|urlize|safe }}.

2.urlizeandurlizetruncWhat are the specific differences between the filters, and which one should I choose? urlizeThe text will be fully converted into clickable hyperlinks, with the displayed text being the original URL.urlizetruncWhile performing the same conversion, it will also truncate the hyperlinks according to the length you specify (for exampleurlizetrunc:30), and addDisplay textto them..., but the actual link address does not change.

  • If the URLs in your article are usually short, or you want users to see the full URL path, you can chooseurlize.
  • If your article often has long URLs, in order to make the page beautiful and tidy, to avoid the long URL from stretching the page layout, thenurlizetruncWould be a better choice.

3. My content is published through a Markdown editor,urlizeWhere should the filter be placed to be effective?For content published in Markdown editors, Anqi CMS will first convert Markdown syntax to HTML. Therefore,urlizeThe filter should be applied after this transformation process. If you passarchiveDetailcontent displayed through tags and usedrender=trueParameters to process Markdown, then the correct order should be{{articleContent|render=true|urlize|safe}}This ensures thaturlizeAfter Markdown has been converted to HTML, identify and convert the plain text URLs within it.

Related articles

How to truncate the text or HTML content that is too long in the template and add an ellipsis to keep the page tidy?

It is crucial to maintain the neat and consistent layout of the page content in website operation.Especially when it is necessary to display abstracts of articles, product descriptions, or news titles, the length of the original content is often uneven, which is likely to cause page layout to be out of order and affect the reading experience of users.AnQiCMS provides very practical template filters that can help us easily truncate long text or HTML content and automatically add ellipses, keeping the page always elegant.### The Importance of Understanding Text Truncation Imagine, in a list of articles, some of the summaries are very short

2025-11-08

How to safely display user-submitted HTML content or JS code in a template to prevent XSS attacks?

In website operation, we often need to handle various types of content submitted by users, such as comments, messages, article submissions, etc.This content may contain HTML tags and even JavaScript code. If displayed directly on a webpage, it may introduce cross-site scripting attacks (XSS), posing a security risk to the website.AnQiCMS was designed with comprehensive consideration of content security and provides corresponding mechanisms at the template level to help us effectively prevent such risks.AnQi CMS uses a template engine syntax similar to Django

2025-11-08

Does AnQi CMS support direct arithmetic operations on strings or numbers within templates to affect display?

When building a website, we often need to handle various data, not just simply display it.For example, we may need to calculate the total price of goods, inventory surplus, or dynamically adjust the style of elements based on numerical size.These scenarios naturally lead to a core question: Does Anqi CMS support direct arithmetic operations on strings or numbers in templates, thereby flexibly affecting the display of content?The answer is affirmative. The AnQi CMS template engine cleverly adopts many advantages of the Django template, including direct support for arithmetic operations.

2025-11-08

How to use the structured data (JSON-LD) feature of Anqi CMS to enhance the search engine results display?

In today's highly competitive online environment, making a website's content stand out in search engine results pages (SERP) and attract more target users is the goal of every website operator.In addition to keyword optimization, high-quality content, and a good user experience, using structured data (JSON-LD) to "tell" search engines exactly what your content is becoming a key factor in improving website visibility and obtaining rich search results (Rich Snippets).

2025-11-08

How to define temporary variables in AnQiCMS templates to control display according to requirements?

In website operation and content management, we often need to flexibly display content based on specific conditions, or temporarily process and store some data at a certain stage of the template.AnQiCMS provides a powerful and easy-to-use template engine, which draws on the syntax of Django templates, allowing us to conveniently define and use temporary variables, thereby achieving fine-grained control over page content.AnQiCMS template files use the `.html` suffix and support Django template engine tag syntax.

2025-11-08

How to display the current year or a custom formatted datetime in the template?

In website operation and content management, displaying accurate date and time is a basic and important requirement.Whether it is the copyright year in the footer, the publication or update date of the article, or the countdown of a specific event, flexible control of the date and time format can greatly enhance the user experience and professionalism of the website.In AnQiCMS, you can easily implement the display of the current year or a custom date and time format using several simple and practical methods.--- ### One: Display the current system year or date and time: Use `{% now %}`

2025-11-08

How to use the AnQiCMS random text generation feature to fill placeholder content during the development stage?

In the process of website project development, we often face a common challenge: how to effectively build and test the layout and style of a website in the absence of actual content?From design prototypes to frontend development, filling in real content is time-consuming and impractical, and simple placeholder text is difficult to simulate the visual effects of real content.

2025-11-08

How to solve the problem of page garbled display caused by incorrect encoding of AnQiCMS template files?

During the process of building a website with AnQiCMS, you may encounter situations where the page display appears garbled, which undoubtedly affects user experience and reduces the value of the carefully crafted page.Generally, such problems are caused by incorrect encoding settings in the template file.This article will detail how to troubleshoot and resolve the problem of garbled display caused by encoding issues in AnQiCMS template files, ensuring that your website content is clear and accurate.### Understanding the Root Cause of Garbled Characters Garbled characters, simply put, are the result of computers using the wrong encoding method to interpret data

2025-11-08