Make the URLs in the text come to life: A practical guide to automatic link conversion in AnQi CMS

In the daily operation of website content, we often encounter such situations: A large number of URL links are included in the text content copied and pasted from various sources.These links, if only existing in plain text form, not only cannot be clicked to jump, but also affect user experience, and even let the potential traffic and information transmission opportunities flow away for nothing.For a website that values user interaction and content dissemination, how to efficiently convert these plain text URLs into clickable HTML links is a crucial step to enhance the practicality and professionalism of the website.

Anqi CMS is an efficient content management system that fully considers the actual needs of content operators, providing powerful and easy-to-use template filters, which can help us easily achieve this function.By skillfully using these built-in tools, you do not need to manually edit each link, and can make your website content vibrant.

Core Solution: Automatic URL Recognition and Conversion

The template filter built into AnQi CMS is the core of automatic URL conversion. Among them,urlizeandurlizetruncThese filters play a major role. They can intelligently identify URL strings in text and wrap them in<a>tags, making them clickable links.

urlizeThe wonder of filters

urlizeThe filter is the most basic and commonly used one. Its main function is to extract strings that match the URL format from the text (whetherhttp:///https://starting with orwww.The beginning, even pure domain names, are automatically converted to standard clickable links. What's more impressive is that in order to comply with SEO**practices,urlizeThe default setting will automatically addrel="nofollow"Attribute, this helps to avoid unnecessary weight loss and indicates to search engines that these links are not used to pass page weight.

While usingurlizeWhen filtering, there is a very important step to note, that is, it must be coordinated with|safeThe filter is used together. The template engine of Anqi CMS, to prevent XSS attacks, defaults to escaping all output content with HTML entities. This means that if used directly,urlizeIt generates<a>Tags will be escaped into&lt;a&gt;Such plain text cannot be displayed as a link.|safeThe filter's role is to inform the template engine: 'This content is safe HTML, please render it directly without escaping.'

The followingurlizeBasic usage example:

{# 假设 archive.Content 包含了纯文本 URL #}
<div class="article-content">
    {{ archive.Content|urlize|safe }}
</div>

{# 如果需要更精细地控制链接文本的转义,例如 URL 本身包含特殊字符,但您希望它显示为原始形式,可以使用参数: #}
{# urlize:false 表示链接的显示文本(不是 HTML 标签本身)不进行额外转义 #}
<p>访问我们的官网:{{ "https://en.anqicms.com/?param1=a&param2=b"|urlize:false|safe }} 了解更多。</p>

In the above example,archive.ContentAll URLs in the text will be recognized and converted into clickable links, and the generated HTML will be rendered correctly.

urlizetruncA combination of beauty and practicality.

When text contains very long URLs, directly converting them to links may cause page layout to become chaotic, affecting visual aesthetics.urlizetruncThe filter is designed to solve this problem. It alsourlizeincreases the feature of displaying the text length of the specified link, and the part exceeding the specified length will be automatically omitted with an ellipsis (...Replaced with ").

UseurlizetruncSimilarly, it needs to be配合|safeFilter. You just need tourlizetruncAdd a number at the end to specify the maximum display length of the link text.

The followingurlizetruncUsage examples:

{# 假设 content.Description 包含一个很长的 URL #}
<p class="summary">
    {{ content.Description|urlizetrunc:30|safe }}
</p>

{# 如果需要更细致的控制,比如 URL 文本本身包含特殊字符,但不希望它被转义显示: #}
<p>相关资源:{{ "https://long-and-complex.example.com/some/path/with/many/parameters?query=value&another=param"|urlizetrunc:30:false|safe }}</p>

In this example, anyurlizetruncconverted link, whose displayed text length will not exceed 30 characters, thus maintaining the page's neatness.

Where can these filters be applied in Anqi CMS?

The AnQi CMS template filter is very flexible, you can use it in any scenario where you need to display user input text or import plain text content from external sourcesurlizeorurlizetrunc. Common application scenarios include:

  • Article detail page (archiveDetail) content field:Forarchive.Contentorarchive.DescriptionApply these filters to make the citation links in the article work immediately.
  • Single page detail page (pageDetail) content field:Similar article content, such as the text on the "About Us" or "Contact Us" pages.
  • Category details page (categoryDetailSummary field:Some categories may include related resource links in the summary.
  • Custom field:If you define a text type custom field for articles, products, or categories, such as "References" or "External links", you can also use these filters.

For example, to turn the article content in the article detail pagearchive.Contentinto clickable links, your template code snippet might look like this:

{% archiveDetail articleContent with name="Content" %}
    <div class="post-body">
        {{ articleContent|urlize|safe }}
    </div>
{% endarchiveDetail %}

Practical skills and precautions

  • Safety first, always use|safe:This rule needs to be emphasized repeatedly. Forget|safeIt will cause the link to be unclickable because it has been escaped into plain text.
  • External link strategy:nofollowAutomatic addition of: urlizeandurlizetruncAutomatically addedrel="nofollow"It is usually good practice for external links. But if your internal links also go through these filters and you do not want to pass the internal links.nofollowThe attribute requires more complex custom processing logic, such as preprocessing with regular expressions when saving content, or dynamically modifying it with JavaScript on the front end (but this is usually not recommended because SEO primarily depends on server-side rendered HTML).
  • The impact of content editor:If your content is edited through the AnQi CMS rich text editor (such as the Markdown editor), then in most cases, the editor has already automatically converted the URL you enter into an HTML link when saving. In this case, it is no longer necessary to useurlizeThe filter may not be necessary, and may even cause unexpected issues (such as attempting to parse existing<a>tags).urlizeSeries filters are best suited for handlingplain text fieldsContent imported from the outside, unformatted in HTML.
  • Test and verification:Before deploying to the production environment, be sure to test the normal operation of the converted links on different browsers and devices, and check whether the page layout is affected by the long URL.

Aqin CMS greatly simplifies the work of content operators through these powerful and concise template filters, significantly improving the interactivity and information transmission efficiency of website content.Master these skills and you will be able to better utilize the potential of Anqicms, building a website with a better user experience and more attractive content.


Frequently Asked Questions (FAQ)

  1. Q: Why did I useurlizea filter, but the link still cannot be clicked, instead it displays a string of strange characters, such as<a href="...">?A: This is likely because you forgot tourlizeadd after the filter|safeThe filter. The AnQi CMS template engine defaults to safely escaping all HTML content to prevent