In daily content creation and website operation, we often need to refer to external resources or provide links to more information.Converting these links into clickable hyperlinks manually is not only time-consuming but also prone to errors, especially when dealing with large amounts of content or Markdown-formatted text.urlizeFilter, it can automatically identify and beautify the URL links in Markdown content.

urlizeFilter: Smartly identify and conveniently beautify URL

urlizeThe filter is a very practical tool in the Anqi CMS template engine, its core function lies in its ability to intelligently identify potential URL strings in text content and automatically convert them into standard HTML hyperlinks (<a>Label)。Whether it is a completehttp://orhttps://URL starting with, without the protocolwww.URL starting with, even pure domain format likeexample.com, as well as email addresses[email protected],urlizecan be accurately captured and processed.

This automated capability greatly liberates the hands of content creators and operators.Imagine when you are writing a technical article in a Markdown editor, referencing multiple tool websites, reference links, or leaving a URL in user comments in the comments section.[文本](链接)English Markdown syntax or<a>tags, will undoubtedly add a lot of repetitive work.urlizeFilters are exactly designed to solve this problem, making content publishing more efficient and convenient.

It is worth mentioning that, for the consideration of search engine optimization (SEO),urlizeThe filter will also automatically add to these links when generating hyperlinks.rel="nofollow"Property.This attribute tells the search engine not to track these links, and will not pass the weight of the current page to these external links. This is crucial for managing the flow of external links on the website, preventing weight dispersion, and maintaining the SEO health of the website.

urlizeit also supports an optional boolean parameter to control whether the link content is escaped. When set totrueWhen, the link text content will be HTML entity escaped; when set tofalse, it will not. This provides additional flexibility for us to handle links containing special characters.

urlizetruncEnglish: Let long URLs become neat and orderly

Sometimes, a URL link may be very long, and directly displaying it on the page may look cluttered, affecting the overall aesthetics of the layout. Anqi CMS providesurlizetruncFilter, it isurlizea enhanced version, while retainingurlizeAll features are based on, with the added function of smartly truncating long text links.

UseurlizetruncWhen we are, we can specify a numeric parameter to limit the display length of the link text. When the displayed text of the original URL exceeds this specified length,urlizetruncIt will automatically truncate and add an ellipsis at the end....),Thus, the links on the page display more neatly. For example, a very long URL may be beautified ashttps://www.example.com/long/path/to/resource...This greatly enhances the visual experience of the page while maintaining the link function.

How to apply these filters in the AnQi CMS template.

Apply in the template files of AnQi CMS.urlizeorurlizetruncThe filter is very simple. They are usually used with variables, through the pipe symbol|Connect. Since these filters generate HTML tags, it is usually necessary to use them in conjunction with.|safeFilter to ensure that the browser can correctly parse and display the generated HTML code, rather than outputting it as plain text.

Here are some usage examples:

UseurlizeFilter:

{# 假设 content 变量包含了 Markdown 格式的文本,其中有URL链接 #}
<p>{{ content|urlize|safe }}</p>

{# 也可以通过 filter 标签块来应用,并指定不转义链接内容 #}
{% filter urlize:false|safe %}
  <p>请访问我的网站:www.anqicms.com 或发送邮件至 [email protected]。</p>
{% endfilter %}

UseurlizetruncFilter (for example, truncated to display 15 characters):

{# 假设 description 变量包含长URL,希望截断显示 #}
<p>{{ description|urlizetrunc:15|safe }}</p>

{# 通过 filter 标签块应用,截断显示为20个字符 #}
{% filter urlizetrunc:20|safe %}
  <p>您可以在这里找到更多信息:https://en.anqicms.com/documentation/detail/long-url-example-document.html</p>
{% endfilter %}

In these examples,|safeThe function of the filter should not be ignored. Without it,<a href="...">...</a>such HTML code will be automatically escaped by the template engine as&lt;a href="..."&gt;...&lt;/a&gt;which causes the link to not display correctly.

Actual application scenarios and content operation value

In the content operation strategy of Anqi CMS,urlizeandurlizetruncFilters are a powerful tool for improving user experience and optimizing SEO.

  • Enhance user experience: Whether it is in blog posts, product descriptions, or user comments, users are accustomed to seeing clickable links.urlizeEnglish will automatically convert plain text URLs to hyperlinks, saving the trouble of copying and pasting, making information acquisition more smooth.
  • Optimize SEO: By automatically addingrel="nofollow"attributes,urlizeHelp website operators better control the SEO signals of external links, avoid unnecessary weight loss, and help maintain good rankings in search engines.
  • Keep the content tidy:urlizetruncEspecially suitable for content areas that may contain long links, such as sidebar recommendations, footer friend links, or brief product descriptions.It can intelligently crop the displayed text of the link, making the page visually cleaner and information presentation more efficient.
  • Efficiently handle Markdown content:For content that extensively uses Markdown format, these two filters can seamlessly integrate, automatically process URLs, greatly reduce the workload of content editing, and make the publishing process smoother.

In summary, the Anqi CMS'surlizeandurlizetruncThe filter is an invaluable auxiliary tool in content operation.They provide powerful automation features with concise syntax, helping us take a solid step forward in beautifying content, optimizing user experience, and improving SEO performance.


Common Questions and Answers (FAQ)

1.urlizeThe filter handles which types of links? urlizeThe filter is very intelligent, it can identify various forms of URLs, including:

  • Byhttp://orhttps://Complete URLs starting with, for example:https://en.anqicms.com.
  • Without a protocol,www.Starting URL, such aswww.anqicms.com.
  • Domain name format URL, such asanqicms.com(Contextual judgment required).
  • Email address, such as[email protected].

2.urlizeDoes the filter automatically add the nofollow attribute? What is the use of this attribute?Yes,urlizeThe filter will automatically convert URLs in the text to hyperlinks by default.<a>Label addrel="nofollow"properties.nofollowThe purpose of the attribute is to inform the search engine not to follow this link and not to pass the weight of the current page to the target page linked to by this link. This is very useful in content operations, for example:

  • Prevent comment spam links from affecting the website's SEO.
  • Control the flow of external links on the website to avoid unnecessary weight loss.
  • When linking to some unofficial or uncontrollable external resources, maintain the SEO health of the website.

3.urlizeandurlizetruncWhat should be noted when using the filter?The following two points should be paid attention to:

  • |safe[en]The use of filtersDue tourlizeandurlizetruncThe filter will generate HTML code (<a>Label),in the template output of the AnQi CMS, be sure to add them after|safefilters (for example{{ content|urlize|safe }})。If missing|safeThe template engine defaults to escaping HTML code for security reasons, resulting in the display of the original HTML tag text on the page rather than clickable links.
  • urlizetrunc truncation length: When usingurlizetruncWhen, it is reasonable to set the truncation length parameter.A too short length may cause the link content to lose readability, while a too long length may not achieve a neat layout effect.Adjust according to the page design and actual requirements.