In AnQi CMS, we often need to display some URLs or email addresses in the content, and we hope they can be automatically turned into clickable links for visitors to directly jump.urlizeandurlizetrunc.Although their purpose is to convert text to links, there are crucial differences in their specific 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 isAutomatically convert recognized URL addresses (including those starting withhttp:///https://), as well aswww.), even email addresses, to HTML.<a>tags to make them clickable hyperlinks.
This filter is very suitable for those scenarios where you want visitors to see the full link address.For example, in the article body, comments, or forum posts, users may need to confirm the completeness and destination of the link.urlizeFiltered, it will display the entire link string (such ashttps://en.anqicms.com/) completely, and add arel="nofollow"attribute.nofollow属性是一个SEO的考量,它会告诉搜索引擎不要将“链接权重”传递给这个链接指向的外部网站,通常用于指向外部或用户生成内容(UGC)的链接。
Example Usage:
Assuming your article content contains a text segment with a URL:
<p>{{"欢迎访问安企CMS官网:https://en.anqicms.com,联系邮箱:[email protected] 获取帮助。"|urlize|safe }}</p>
AfterurlizeThe possible display effect on the page after filtering may be:
<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 URL and email have been completely converted into clickable links.
urlizetruncFilter: Smart truncation, keep the page tidy.
urlizetruncThe filter functions withurlizeIt can also convert URLs and email addresses in text to clickable links and automatically addrel="nofollow"properties. But its most significant difference is,urlizetruncCan truncate the display text of the link to the length you specify while converting the link.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 URL address is too long and may affect the aesthetics, or you only need to visually indicate that there is a link without displaying the full address. For example, in the sidebar, recommended content list, or user profile page, a long URL may destroy the page layout, at this timeurlizetruncCan be put to use, keeping the page tidy.
Example Usage:
We still use the text containing the URL mentioned above, but this time we useurlizetruncThe filter and specify the truncation length of20:
<p>{% filter urlizetrunc:20|safe %}
欢迎访问安企CMS官网:https://en.anqicms.com/document/templates/introduction.html,联系邮箱:[email protected] 获取帮助。
{% endfilter %}</p>
Afterurlizetrunc:20The possible display effect on the page after filtering may be:
<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, both the original full URL and email address have been shortened and appended at the end,...making the display more compact.
Core Differences and Selection Guide
In short,urlizeandurlizetruncThe core difference of the filter lies inThe integrity of the linked display text:
urlize:displayCompleteURL or email address.- Applicable scenarios:When it is necessary to display the complete path of the link clearly, such as article details, citation sources, and user submitted content, visitors expect to see the complete link to judge its credibility.
urlizetrunc:displayTruncated afterAn abbreviated URL or email address.- Applicable scenarios:When the page space is limited and long URLs affect the aesthetics, such as list items, sidebars, summaries, navigation menus, or any place that emphasizes a simple layout.
Whether you choose any filter, they will automatically add to the generated linkrel="nofollow"Properties, 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 according to the design style of the website, the type of content, and the expected user experience, making your CMS website content display more intelligent and beautiful.
Common Questions (FAQ)
Q:
urlizeandurlizetruncDoes the filter automatically recognize all types of website addresses? Answer:They mainly identify those that start withhttp:///https://a complete URL,www.The domain name at the beginning, as well as the standard email address format. For text that does not match these patterns (such as just wroteanqicms.combutwww.or protocol header), it may not be automatically converted.问:If I use
urlizetrunca filter, but the link itself is very short, what will happen? 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 ellipsis will only take effect when the link length exceeds the set value.问:我可以自定义
urlizeandurlizetrunc过滤器添加的rel="nofollow"attributes? Answer:在安企CMS的模板过滤器层面,urlizeandurlizetrunc默认都会为生成的链接添加rel="nofollow"Properties, this is to unify the management of the SEO impact of external links. If you need more fine-grained controlrelProperty, may require adjustment through custom template tags or deeper development, but this goes beyond the default capabilities of these two filters.