How to automatically convert URLs and email addresses in the text content into clickable links?

Calendar 👁️ 62

On the day when digital content is becoming increasingly abundant and frequently updated, website operators must not only pay attention to the quality of the content, but also to the presentation and user interaction experience.When we publish articles, product descriptions, or any text information on AnQiCMS (AnQiCMS), we often include some URL links and email addresses pointing to external resources or contact information.If this information is only displayed in plain text, users will have to manually copy and paste it, which undoubtedly increases the complexity of the operation and reduces the user experience.AnQiCMS as an efficient content management system provides a concise and powerful way to automatically solve this problem, ensuring that URLs and email addresses in your text content can be automatically converted into clickable links, thereby greatly enhancing user-friendliness and the professionalism of the content.

AnQiCMS'urlizeFilter: The core of automatically linking text content

In the AnQiCMS template engine,urlizeThe filter is a key feature for automatically converting text content to links. This intelligent filter can automatically identify strings in the text that match the URL format, such ashttp://example.com/www.example.comAnd email address (for example[email protected]) and enclose it in standard HTML<a>tags to make it clickable on the frontend page.

For example, suppose your article content contains a sentence: "Learn more by visiting our official website: "}https://en.anqicms.com, or send an email to[email protected] inquire.If this text is directly output, it will just be plain text.But when you apply the variable containing this text in the templateurlizeWhen filtering, AnQiCMS will automatically convert it to:

了解更多请访问我们的官方网站:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,或发送邮件至 <a href="mailto:[email protected]" rel="nofollow">[email protected]</a> 进行咨询。

This allows users to click on these links or email addresses directly without any additional operations. It is worth noting that in order to comply with SEO practices and avoid unnecessary link weight transfer,urlizeThe filter will automatically add to these automatically generated external linksrel="nofollow"Property.

Used in AnQiCMS template filesurlizeThe filter is very simple. You just need to attach it to the variable that needs to be processed, and be sure to combine|safefilter.|safeThe purpose is to inform the template engine that the output content has been processed by HTML and does not require further escaping, otherwise, the generated<a>Label code may be displayed directly as text instead of an actual link.

You can use it like this for single-line variables or small content blocks.

<p>最新动态请访问:{{ archive.Link | urlize | safe }}</p>
<p>联系方式:{{ contact.Email | urlize | safe }}</p>

If you need to link a field containing a lot of text, such as an article'sContentyou can use a tag block to wrap it:filterTag block to wrap:

{% filter urlize:true|safe %}
    {{ archive.Content }}
{% endfilter %}

Hereurlize:trueThe parameter allows retaining the original HTML entity escaping state when processing link content,|safewhich ensures that the final rendered HTML structure is correct and free of errors.

urlizetruncFilter: Optimize the display of long URLs

In some cases, very long URL addresses may appear in the article content, and these long URLs may take up too much space when displayed in full on the page, even affecting the overall layout and aesthetics of the page. To solve this problem, AnQiCMS providesurlizetruncFilter. This filter functions asurlizeSimilarly, it can automatically identify and convert URLs and email addresses into clickable links, but on this basis, it allows you to specify a maximum character length. When the displayed text of the converted URL exceeds this limit, the excess part will be automatically omitted by an ellipsis (...Replace it to make the page display more tidy.

For example, if you have a very long URL and you want it to display no more than 30 characters:

{% filter urlizetrunc:30|safe %}
    <p>以下是一个截断显示的长链接:https://en.anqicms.com/documentation/how-to-automatically-convert-urls-and-emails-to-clickable-links-in-anqicms.html</p>
{% endfilter %}

afterurlizetrunc:30After processing, the long link in this text will appear in a more concise form to the user, while maintaining its clickability andrel="nofollow"Properties. This is particularly useful in scenarios such as news summaries and product parameter lists, which can effectively improve the reading experience of the content.

Practical application scenarios and content operation value

In the daily operation of AnQiCMS,urlizeandurlizetruncThe filter has a wide range of applications. You can use them on detail pages of various content types, such as byarchiveDetailtags to get articles or product contentContentfield, bypageDetailThe content of the single-page label obtainedContentfields, even category descriptions(categoryDetailofDescriptionThese filters are useful for any text that needs to be dynamically output and includes URLs or email addresses.

They ensure that users of the website can access external resources, download files, or send emails while browsing your content instantly and conveniently, without having to manually copy and paste. This not only significantly improves user experience but also simplifies the process of content editing and publishing, reducing the need for manual addition of HTML.<a>Tags may introduce syntax errors. Through the automated link conversion mechanism, AnQiCMS empowers website operators to focus on high-quality content creation, while ensuring the performance of website content in terms of interactivity, readability, and SEO friendliness.


Frequently Asked Questions (FAQ)

Q1: I used in AnQiCMS templateurlizeorurlizetruncfilter, but why are the displayed URL and email address still plain text and not clickable?A1: The most common reason is that you may have forgotten to add at the end of the filter chain|safeFilter. The AnQiCMS template engine, for security reasons, defaults to escaping all content output through variables with HTML entities to prevent potential cross-site scripting (XSS) attacks.|safeThe filter is an explicit declaration that tells the template engine that the output content is safe HTML code that can be rendered as is, thus makingurlizeorurlizetruncGenerated<a>The tag can be correctly parsed by the browser and displayed as a clickable link. Please check your code to ensure its format is similar{{ your_content_variable | urlize | safe }}.

Q2:urlizeThe filter will automatically add to all converted external linksrel="nofollow"Do you have a property? Can I customize or remove this property?A2: Yes

Related articles

How to wrap long text content automatically at a specified length?

As an experienced CMS website operation personnel, I fully understand the core status of content in website operation.High-quality content not only requires careful creation, but also needs to be presented in **a way** to ensure the efficiency of user experience and information delivery.How to effectively handle automatic line breaks for long text content is the key to improving the readability and aesthetics of the page.The AnQi CMS maintains the integrity of content storage, but on the front-end display level, it provides flexible automatic line break solutions through its powerful template engine and rich filters.

2025-11-06

How to format a floating-point number to display with a specified number of decimal places?

As an experienced CMS website operation person in the security industry, I know that both the accuracy and beauty of data are equally important in content display.Especially for floating-point numbers, how to format them to display with a specified number of decimal places is a key element in improving user experience and ensuring clarity of information.The powerful template engine of AnQi CMS provides flexible filter (filters) functions, allowing us to easily achieve this requirement.

2025-11-06

How to get the actual length of a string, array, or key-value pair?

As an experienced website operator who is well-versed in AnQi CMS, I fully understand the importance of accurately obtaining data length in content management and template design.In order to optimize the display layout, implement conditional judgments, or ensure the integrity of the content, having a clear understanding and operational ability of the actual length of strings, arrays (or slices in Go language), and key-value pairs (maps or structures in Go language) is crucial for improving website user experience and operational efficiency.

2025-11-06

How to determine if a variable is empty in a template and set a default display value?

As an experienced CMS website operation person, I am well aware of the importance of template content robustness for website user experience and data display.In daily operation, we often encounter situations where variables may be empty. If not handled properly, it may result in incomplete page display, or even cause the page to report errors.AnQiCMS (AnQiCMS) offers a flexible template engine with multiple ways to determine if a variable is empty and set a default display value, which is crucial for building high-quality, high-fault-tolerant websites.

2025-11-06

How to get the list of related articles for the current article (based on category, keywords, or manual association)?

As a website operator who is deeply familiar with the operation of AnQiCMS, I know that high-quality content and user experience play a key role in the success of a website.Articles related can effectively extend the user's stay on the site, increase page views, and are also very beneficial for the internal link optimization (SEO) of the website.AnQiCMS provides a flexible way to obtain and display these related contents, meeting the operational needs of different scenarios.

2025-11-06

How to directly call the built-in methods of Go struct in the template?

As a professional who has been deeply engaged in the content operation of Anqi CMS for a long time, I know that the template plays a core role in the presentation of website content.A highly efficient and flexible template system that can greatly enhance the efficiency and richness of content publishing.AnQi CMS, with its powerful backend based on Go language, provides template developers with the ability to directly interact with the underlying data structure, one very practical feature being the ability to directly call built-in methods defined in Go structs in the template.

2025-11-06

How to unify the overall layout and structure of the website through the template inheritance mechanism?

As an experienced CMS website operation person, I know the importance of an efficient and unified template system for website operation.AnQi CMS, with its high-performance architecture based on the Go language and flexible template system, provides us with powerful tools to ensure the consistency of content display, the unity of brand image, and greatly improve the efficiency of content management and publication.The importance of unified website layout and structure Maintaining consistency in the overall layout and structure of a website is crucial for attracting and retaining users in website operations.a mess

2025-11-06

How to create reusable code snippets (macro functions) in templates?

As an experienced AnQiCMS website operator, I know that efficiency and consistency are very important in daily content management.Especially in template design and content publishing, if code can be effectively reused, it can not only greatly improve work efficiency, but also ensure the consistency of website visual and functional integrity.Today, I will discuss with everyone how to create and utilize reusable code snippets, also known as macro functions, which are one of the key tools for our efficient operation.### Template code reuse

2025-11-06