In Anq CMS, we often need to display some website URLs or email addresses in the content, and we want them to be automatically turned into clickable links for visitors to directly jump to. To achieve this function, Anq CMS provides two very practical template filters:urlizeandurlizetrunc. Although their purpose is to convert text into links, there are significant differences in their presentation.Understanding these differences can help us better manage website content and improve user experience.

urlizeFilter: Make the link 'alive'

urlizeThe main function of the filter is toAutomatically convert the recognized URL addresses (including those starting withhttp:///https://those starting with the website address, as well aswww.even email addresses) in the text to HTML.<a>Tags to make it clickable.

This filter is very suitable for those scenarios where you want visitors to see the full link address.For example, in the body of an article, in comments, or in forum posts, users may need to confirm the completeness and destination of the link.When you useurlizeAfter filtering, it will display the entire link string (such ashttps://en.anqicms.com/) completely, and add arel="nofollow"this attribute.nofollowThe attribute is a SEO consideration, it tells the search engine not to pass the "link weight" to the external website pointed to by this link, usually used for links pointing to external or user-generated content (UGC).

Usage example:

Assuming your article content contains a text with a URL:

<p>{{"欢迎访问安企CMS官网:https://en.anqicms.com,联系邮箱:[email protected] 获取帮助。"|urlize|safe }}</p>

afterurlizeThe possible effect on the page after filtering:

<p>欢迎访问安企CMS官网:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,联系邮箱:<a href="mailto:[email protected]" rel="nofollow">[email protected]</a> 获取帮助。</p>

You can see that the original URLs and email addresses have been fully converted into clickable links.

urlizetruncFilter: Smart truncation, keep the page tidy

urlizetruncThe filter functions withurlizeSimilarly, it can also convert URLs and email addresses in text to clickable links and automatically addrel="nofollow"properties. But its most significant difference lies in,urlizetruncYou can truncate the display text of the link while converting it to the length you specify.If the length of the original link exceeds this limit, it will add an ellipsis at the truncation point....)

This filter is very suitable for scenarios where the page space is limited, the long URL address may affect the aesthetics, or you only need to visually prompt that there is a link without displaying the full address. For example, in the sidebar, recommendation content list, or user profile page, a long URL may destroy the page layout, at this timeurlizetruncIt can be put to use, keeping the page neat.

Usage example:

We still use the text containing the URL mentioned above, but this time usingurlizetruncThe filter and specify the truncation length to20:

<p>{% filter urlizetrunc:20|safe %}
欢迎访问安企CMS官网:https://en.anqicms.com/document/templates/introduction.html,联系邮箱:[email protected] 获取帮助。
{% endfilter %}</p>

afterurlizetrunc:20The possible effect on the page after filtering:

<p>欢迎访问安企CMS官网:<a href="https://en.anqicms.com/document/templates/introduction.html" rel="nofollow">https://www.anqicms...</a>,联系邮箱:<a href="mailto:[email protected]" rel="nofollow">support@anqicms...</a> 获取帮助。</p>

It can be seen that due to the specified truncation length of 20, the original full URL and email address have been shortened and appended at the end...making the display more compact.

Guide to Core Differences and Selection

In short,urlizeandurlizetruncThe core difference lies in the filterThe integrity of the linked text:

  • urlize:DisplayCompleteURL or email address.
    • Application scenarios:When it is necessary to clearly display the complete path of the link, such as article details, reference sources, user submitted content, visitors expect to see the complete link to judge credibility.
  • urlizetrunc:DisplayTruncated afterURL or email address with an ellipsis.
    • Application scenarios:When page space is limited and URLs are too long, affecting aesthetics, such as list items, sidebars, summaries, navigation menus, or any emphasis on a simple layout.

No matter which filter you choose, it will automatically add to the generated linkrel="nofollow"Property, this is consistent and beneficial for the website's SEO optimization and external link management.In practical applications, you can flexibly choose these two filters based on the design style of the website, the type of content, and your expectations of user experience, making your Anqi CMS website content display more intelligent and beautiful.


Frequently Asked Questions (FAQ)

  1. Question:urlizeandurlizetruncDoes the filter automatically recognize all types of web addresses? Answer:They mainly recognize those that start withhttp:///https://full URLs starting with,www.A domain name at the beginning, as well as the standard email address format. Text that does not match these patterns (such as only writinganqicms.combutwww.or protocol headers) may not be automatically converted.

  2. Question: If I useurlizetruncFilter, but what if the link itself is very short? Answer:If the actual length of the link (includinghttp://orhttps://) is less than or equal to the truncation length you specify,urlizetruncThe filter will not truncate but will display the link in full and will not add an ellipsis. Truncation and an ellipsis will only take effect when the link length exceeds the set value.

  3. Ask: Can I customizeurlizeandurlizetruncFilter addedrel="nofollow"Should I add the attribute? Answer:At the Anqi CMS template filter level,urlizeandurlizetruncThe default will add to the generated linksrel="nofollow"Property, this is to unify the management of external link SEO impact. If you need more fine-grained controlrelProperty, it may require adjustment through custom template tags or deeper development, but this is beyond the scope of the default capabilities of these two filters.