In daily content creation and website operation, we often need to insert various links in articles, whether pointing to external resources or referencing other content within the site.Manually converting these plain text URL addresses into clickable hyperlinks is undoubtedly a repetitive and error-prone task.Imagine how much time and effort it would take if the website had a large amount of content?

Fortunately, AnQiCMS (AnQiCMS) understands these 'pain points' of content operators and provides us with a very practical and intelligent solution -urlizeFilter.This is a compact and powerful tool that can automatically identify URLs and email addresses in text and convert them into clickable links, greatly enhancing the efficiency and user experience of content editing.

urlize: Intelligent text link converter

In the AnQi CMS template system,urlizeIt is an indispensable template filter. Its core function is to intelligently scan the text content you provide, identify strings that match the URL or email address format, and then automatically wrap them up.<a>The tag makes it clickable on the web page.

This is like installing a 'link detection radar' on your text content. Whether it is a commonhttp:///https://URL starting with a protocol, orwww.A domain name at the beginning, even an email address,urlizeCould accurately capture. Once identified, it will immediately convert this plain text information into a convenient entry that users can directly click to access.

It is worth mentioning,urlizeWhen converting links, it will also automatically add these external links for **SEO (search engine optimization) practical considerationsrel="nofollow"

How to use in the templateurlizeFilter?

UseurlizeThe filter is very simple, just apply it to the text variable you need to process.The most common usage is to display article content, comments, or any text area that may contain a URL.

For example, you have a link namedarticleContentThe variable contains the detailed content of the article, and among this content may be scattered some plain text URLs. If you want these URLs to automatically become clickable links, you can use them like this:

{{ articleContent|urlize|safe }}

It should be particularly noted that|safeThis filter.In Anqi CMS template engine, to prevent potential cross-site scripting attacks (XSS), all output variable content is automatically escaped by default.urlizeThe filter converts the URL to<a href="...">...</a>Such HTML code, if not added|safeThe browser will treat these as<a>The tag itself is displayed as plain text, rather than a clickable link. So, inurlizethen add|safeThis tells the template engine that these HTML codes are safe and can be directly parsed and displayed.

In addition to acting directly on a single variable,urlizeFilters can also be used as a "block filter" to process a section of enclosed text. This method is particularly suitable for templates that contain a large amount of dynamic or static text and require overall link transformation:

{% filter urlize:true|safe %}
  这是一段可能包含各种链接的文本内容。
  您可以访问我们的官网:https://en.anqicms.com
  或者发送邮件至 [email protected] 获取帮助。
  另一个有趣的网站是 www.example.com
{% endfilter %}

In the above block-level usage, you may noticeurlize:truethis parameter. Thistruethe parameter is usually used to controlurlizeWhether to escape the text content of the link itself with HTML entities. In most cases, keep the default value (usuallytrue), or explicitly set totrueIt ensures that the link displays correctly.

Extend the application:urlizetruncMake the link more concise.

Sometimes, the converted URL in the article may be very long, occupying a lot of page space and affecting the overall layout aesthetics. At this time, Anqi CMS also providesurlizetrunca filter, which isurlizeAn enhanced version that can automatically convert links, and can truncate the display text of the link to the specified length, and add an ellipsis in the over-length part....

For example, if you want the link to display only 15 characters, you can use it like this:

<p>访问我们的项目主页:{% filter urlizetrunc:15|safe %}https://github.com/fesiong/goblog{% endfilter %}</p>

This code will generate a clickable link, but the displayed text will behttps://github.c...It maintains the link function while making the content look more tidy.

urlizeBrings value to content operation

ByurlizeThe filter, AnQi CMS greatly simplifies the content publishing process.Content editors no longer need to manually insert links; they just need to focus on the content itself, and the system will automatically complete the link conversion work.This not only improves work efficiency, but also ensures the uniformity and accuracy of website links, reducing the risk of broken or missing links.nofollowThe attribute also helps the website better manage the SEO external link strategy.

For website visitors, all URLs are directly converted into clickable links, greatly enhancing the reading experience and the convenience of information acquisition, allowing them to navigate smoothly within and outside your website, enjoying a more efficient browsing process.

Frequently Asked Questions (FAQ)

1.urlizeWhat types of links can the filter recognize? urlizeThe filter is very intelligent and can recognize common URLs starting with HTTP/HTTPS protocols (such ashttp://www.example.com/https://example.com), as well aswww.domain names starting withwww.example.org),even able to convert standard email addresses like[email protected])tomailto:links.

2. Why do you need to addurlizeafter|safe?The AnQi CMS template engine, to enhance security, will default to escaping all output variable content to prevent malicious script injection (XSS attack).urlizeThe filter will convert plain text URLs to HTML.<a>Tags. If not used|safe, the template engine will process these.<a>Tags are also treated as plain text and escaped, causing them to be displayed on the page.<a href="xxx">xxx</a>as strings, not clickable links. Therefore, adding|safetells the template engine,urlizeThe generated HTML code is safe and can be parsed and displayed directly.

3.urlizeThe generated link can be customizedrelProperties? For example, do not add.nofollow? urlizeThe filter is designed to automatically add to the converted external links in accordance with the general SEO**practicesrel="nofollow"Property, there is no direct parameter to disable or modify this default behavior. If you indeed need to customize the link'srelAttribute, may require manually writing HTML code or considering more advanced custom processing methods rather than relying entirely onurlizefilter.