In website content operation, we often need to include some URLs or email addresses in the articles or descriptions.Under the traditional method, this information is often just plain text, and users need to manually copy and paste to access it, which undoubtedly increases the user's operational cost and affects the interactivity of the content.urlizeFilter.

urlizeFilter: Make text automatically 'come alive'

In simple terms,urlizeis an integrated template filter in AnQiCMS, its core function is to intelligently scan text content, automatically identify the URLs and email addresses within it, and convert them into clickable HTML<a>Link label. This means that when you edit content in the background, even if it's just simply enteringhttps://en.anqicms.comor[email protected], they will magically become clickable links that can be accessed directly when displayed on the front page.

This feature greatly enhances the user experience.Imagine, when visitors browse your website content, whether they see a reference link or a contact email, they can directly click to jump without interrupting their reading to manually copy.This smooth interaction will undoubtedly increase the amount of time users spend on your website, and encourage them to further explore or interact.

Behind the scenes considerations: SEO and link management

AnQiCMS in implementationurlizeThe filter also takes into account the needs of content management and search engine optimization. An interesting detail is,urlizeFilter defaults to adding these links during automatic link generationrel="nofollow"properties.

rel="nofollow"It is an important SEO identifier that tells search engines not to track or pass weight to this link.This is very useful for website administrators, especially when your content may include user comments, forum posts, or links to non-core partner websites.nofollowYou can effectively control the flow of 'link weight' to avoid negative impacts on your website's SEO due to excessive external links or links to low-quality websites, and it can also reduce the number of malicious links contained in spam comments to some extent.

How to easily use in the templateurlize

Apply in AnQiCMS templateurlizeThe filter is very simple and intuitive. Since AnQiCMS adopts a template engine syntax similar to Django, you can use it just like other filters.urlizeApply to any text variable that includes a URL or email address.

The most common usage is:

{{ content_variable|urlize|safe }}

Here are thecontent_variableIt can be your article details, description, or any other text field. It is especially important to emphasize that,|safeThe filter is indispensable here. BecauseurlizeThe filter generates the actual HTML<a>Labels, while AnQiCMS templates default to escaping all output content for safety (that is, HTML encoding)<Converted to&lt;are missing). If missing|safeThe page will directly display<a>The original text of the label, rather than a clickable link.|safeThe filter tells the template engine that this part of the content is processed and safe HTML that can be rendered directly.

If you need to process a large block of texturlizeProcess, rather than a single variable, can also be usedfilterTags:

{% filter urlize|safe %}
    这里是您的长文本内容,其中包含网址如 https://example.com 和邮箱 [email protected]。
    您也可以访问 www.anotherlink.net 了解更多信息。
{% endfilter %}

Sometimes, you may want to keep the original HTML entities of the linked display text when converting (for example)&amp;still displays as&amp;). At this point,urlizeThe filter also supports a boolean parameter:

{# 如果您希望链接文本内容进行HTML转义,可以使用 true (默认行为) #}
{{ my_text|urlize:true|safe }}

{# 如果您不希望链接文本内容进行HTML转义,可以使用 false #}
{{ my_text|urlize:false|safe }}

In most cases, unless there is a special requirement, using it directly{{ content_variable|urlize|safe }}can meet most scenarios.

urlizetrunc: to slim down the long link

In some cases, the URL address may be very long, and directly displaying it on the page may destroy the beauty of the layout. AnQiCMS also takes this into consideration, and providesurlizetruncFilter asurlizethe advanced version of.

urlizetruncHow to use the filter.urlizeIt is similar, but it allows you to specify a numeric parameter to control the maximum length of the displayed link text. If the converted link text exceeds this length, the extra part will be truncated and indicated with an ellipsis (...(…) instead.

{# 链接显示文本最多显示25个字符,超出部分用...代替 #}
{{ long_url_text|urlizetrunc:25|safe }}

This is very useful when displaying links (such as sidebars, list summaries) in limited space, it can keep the page tidy without losing the accessibility of the links.

Actual application example

Assume your article contentarticle.ContentAs follows:

欢迎访问安企CMS官网:https://en.anqicms.com,这是一个非常棒的CMS。
如果您有任何问题,可以发送邮件到 [email protected] 或者访问我们的GitHub仓库:github.com/fesiong/goblog。
我们还有一个演示网站:www.kandaoni.com/demo,欢迎体验!

Used in the templateurlizeFilter:

<div class="article-body">
    {% filter urlize|safe %}
        {{ article.Content }}
    {% endfilter %}
</div>

The final rendered HTML effect of the page will be:

<div class="article-body">
    欢迎访问安企CMS官网:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,这是一个非常棒的CMS。
    如果您有任何问题,可以发送邮件到 <a href="mailto:[email protected]">[email protected]</a> 或者访问我们的GitHub仓库:<a href="http://github.com/fesiong/goblog" rel="nofollow">github.com/fesiong/goblog</a>。
    我们还有一个演示网站:<a href="http://www.kandaoni.com/demo" rel="nofollow">www.kandaoni.com/demo</a>,欢迎体验!
</div>

It can be seen that all URLs and email addresses have been automatically converted into clickable links and automatically added.rel="nofollow"properties. If appliedurlizetrunc:20, the display text of longer links will be shortened.

PassurlizeandurlizetruncThese powerful and convenient filters greatly simplify the handling of links in the content of AnQiCMS, not only improving the readability and user experience of the content, but also providing strong support for the website's SEO strategy in the background.In daily content operation, making full use of these tools will make your work twice as effective.


Common Questions (FAQ)

  1. Q: urlizeThe filter will automatically add to all linksrel="nofollow"attributes?A:Yes, by default,urlizeThe filter will automatically identify and convert URLs and email addresses in the text to clickable links, and automatically add these links withrel="nofollow"Property. This is an important SEO practice, which helps manage the external link weight of the site and prevent spam comments and other issues.