In website content operation, we often need to display links within limited space, especially when the link address is too long. It will look long and unattractive if displayed directly. AnQiCMS (AnQiCMS) providesurlizetruncThe filter is designed to solve this problem. It can optimize the display of the link while ensuring the integrity of its function, enhancing the readability and aesthetics of the page.

urlizetruncThe filter is mainly used for automatically identifying URL strings in text (including common web addresses and email addresses), and converting them into clickable hyperlinks (<a>Label). Its unique feature is that it allows you to specify a truncation length. When the displayed text of the original link exceeds this length,urlizetruncit will shorten it and add an ellipsis at the end,...),Thus, the page layout becomes more tidy.

Then, how can we ensure the integrity and accessibility of the link in the case where the displayed text is truncated? Anqi CMS is designedurlizetruncFiltering has already fully considered this point:

Firstly, and most importantly,urlizetruncFilter is generated in<a>then,It will only truncate the display text of the link, but not the internalhrefThe property (i.e., the actual target link address) always remains complete and unmodified.This means that no matter how long the display text is that the user sees on the page, they will be redirected to the original, complete target address without any deviation when they click on the link.The functionality and accuracy of the link have been fundamentally guaranteed.

Secondly, from the perspective of user accessibility, modern browsers provide various methods to compensate for the information loss caused by text truncation displayed:

  • Mouse hover tooltip:When the user hovers the mouse cursor over a truncated link, the vast majority of browsers will display the full link address in the status bar (usually in the lower left corner of the browser window).This is a very intuitive and commonly used method that allows users to preview the complete URL before clicking.
  • Copy link feature:Users can right-click on truncated links and select "Copy link address."The content copied to the clipboard will be the complete, unshortened original URL, not the displayed text.This provides convenience for users who need to share or save the link.

In addition,urlizetruncThe filter will default to generating links when<a>Label addrel="nofollow"Property.This is very important for the website's SEO strategy, it tells search engines not to track or pass weight to these external links, which helps control the website's link structure and weight distribution.

applying in the templateurlizetruncFiltering, there is a small detail to note. If you use it to process variables containing HTML content, and place it in{% filter %}the tag block, then it usually also needs to be inurlizetruncimmediately use after|safeFilter. This is because the template engine of AnQi CMS defaults to HTML escaping for all outputs for security reasons. If not|safe,urlizetruncgenerated<a href="...">This HTML structure may be escaped again, causing the link to display or click normally.

For example, you might use it like thisurlizetrunc:

<p>以下是一些相关资源链接:</p>
{% filter urlizetrunc:30|safe %}
我们很高兴宣布 AnQiCMS 官方网站已上线,地址是 https://en.anqicms.com/,欢迎大家访问!
如果您有任何疑问,请发送邮件至 [email protected]。
{% endfilter %}

The above code ensures that the text inhttps://en.anqicms.com/and[email protected]Converted into clickable links, and their display text (if over 30 characters) will be truncated, but the actual link target will not be affected.

In general, the safety CMS ofurlizetruncfilter optimizes the display of page content while retaining the completehrefProperties and utilizing the interactive features built into the browser effectively ensure the integrity and accessibility of links.This makes it a powerful tool that balances beauty and functionality in content operations.


Common Questions (FAQ)

Q1:urlizetruncWhat methods can I use to see the full link address after the filter truncates it?A1: Even if the displayed text of the link is truncated, the user can still obtain the full original URL by hovering over the link with the mouse (most browsers will display the full URL in the status bar), or by right-clicking the link and selecting 'Copy Link Address'. The actual target address of the link is (hrefThe property is always complete.

Q2: Why do I need to addurlizetruncAfter the filter, the links on my page did not become clickable, but instead displayed<a href="...">such original HTML code?A2: This is very likely because you are usingurlizetruncAfter that, you did not add|safeFilter. The template engine of AnQi CMS will default to escaping output content to prevent security issues. In order tourlizetruncGenerated HTML tags should be parsed correctly by the browser and not displayed as plain text. You need to makeurlizetruncimmediately use after|safeFilter.

Q3:urlizetruncThe filter able to control whichrelproperties, such as removing,nofollow?A3:urlizetruncThe filter will automatically add to the generated linksrel="nofollow"Properties, this is for SEO optimization considerations, to prevent unnecessary weight transfer. According to the current document, the behavior of this filter is fixed, and it does not provide direct parameters to modify or remove thisnofollowProperties. If the website operation has special requirements, it may be necessary to consider custom templates or other processing methods.