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 not beautiful. AnQiCMS (AnQiCMS) providesurlizetruncThe filter is designed to solve this problem. It can optimize the display of links while ensuring the integrity of the link function, enhancing the readability and aesthetics of the page.
urlizetruncThe filter is mainly used to automatically identify URL strings in text (including common URLs and email addresses) and convert them into clickable hyperlinks (<a>Label). Its unique feature is that it allows you to specify a truncation length. When the display text of the original link exceeds this length,urlizetruncit will shorten it and add an ellipsis at the end(...Thus, making the page layout more tidy.
So, how can we ensure the integrity and accessibility of links when the displayed text is truncated? Anqi CMS is designedurlizetruncWhen filtering, this point has been fully considered:
First and most importantly,urlizetruncThe filter generates,<a>tags in the template,It will only truncate the displayed text of the link, while the internalhrefThe property (i.e., the actual target link address) must remain complete and unmodifiedThis means that no matter how long the display text is on the page, when users click the link, they will be redirected to the original, complete target address without any deviation.The functionality and accuracy of the link are fundamentally guaranteed.
Secondly, from the perspective of user accessibility, modern browsers provide various ways to compensate for the information loss caused by text truncation:
- Hover tooltip:When the user hovers the mouse cursor over a truncated link, most browsers will display the full link address in the status bar (usually at 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 function:Users can perform a right-click operation on the truncated link and select 'Copy Link Address'.At this time, the complete, untruncated original URL will be copied to the clipboard, not the displayed text.This provides convenience for users who need to share or save the link.
Furthermore,urlizetruncThe filter will default to generating a link when<a>tagsrel="nofollow"Properties. This is very important for the website's SEO strategy, it tells search engines not to follow or pass weight to these external links, which helps control the link structure and weight distribution of the website.
Applying in the templateurlizetruncWhen using the filter, there is a small detail to note. If you use it to process a variable containing HTML content and place it in{% filter %}the tag block, then you usually also need tourlizetruncthen use it immediately afterwards:|safeFilter. This is because the security template engine of AnQiCMS defaults to HTML escaping all outputs. If not|safe,urlizetruncGenerated<a href="...">Such HTML structure may be escaped again, causing links to not display or clickable.
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]The text is converted to a clickable link, and if it exceeds 30 characters, it will be truncated, but the actual link target will not be affected.
In general, the Anqi CMS'surlizetruncThe filter optimizes the display of page content while preserving the completehrefThe attribute, by utilizing the interactive features built into the browser, effectively ensures the integrity and accessibility of the link.This makes it a powerful tool that balances beauty and functionality in content operations.
Frequently Asked Questions (FAQ)
Q1:urlizetruncAfter the filter truncates, how can I still see the full link address?A1: Even if the displayed text of the link is truncated, users can still obtain the complete 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 is it used inurlizetruncAfter the filter, the links on my page did not become clickable, instead it showed.<a href="...">Such original HTML code?A2: This is very likely because you are usingurlizetruncAfter that, nothing was added|safeFilter. The Anqi CMS template engine 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 applyurlizetruncthen use it immediately afterwards:|safefilter.
Q3:urlizetruncwhether the filter can control the links it generatesrelproperties, such as removingnofollow?A3:urlizetruncThe filter defaults to automatically adding attributes to the generated linkrel="nofollow"Property, this is for SEO optimization considerations to prevent unnecessary weight transmission. According to the current document, the behavior of this filter is fixed and no direct parameters are provided to modify or remove thisnofollowProperty. If the website operation has special requirements, you may need to consider custom templates or other processing methods.