In website content operation, we often need to present URL links within limited display space.To maintain the cleanliness and consistency of the page, AnQiCMS (AnQiCMS) provides various template filters to help us process these links. Among them,urlizeandurlizetruncThese are two very practical tools that can automatically convert URLs in text to clickable links.

Get to knowurlizeFilter: The starting point for automatic links

Further understandurlizetruncBefore that, let's take a look at first.urlizeFilter. Its main function is to automatically identify URLs (such ashttp://www.example.com/www.example.comevenexample.com) and email addresses (such as[email protected]) and convert them into standard HTML hyperlinks (<a href="...">...</a>Label). It also automatically adds these links for SEO considerationrel="nofollow"The attribute prevents search engine weight transfer. This filter is very convenient, ensuring that all links in the article are clickable, enhancing user experience.

urlizeAn optional parameter was provided to control whether the link text is escaped when converted to HTML. For example, if you have a URL that contains special characters, you can choose whether to leturlizeDisplay as raw characters or escaped characters.

urlizetruncFilter: a utility for link truncation

urlizetruncThe functionality of the filter is tourlizeSimilarly, it also recognizes URLs and email addresses in text and converts them into clickable links. However,urlizetruncAn important feature has been added to this:Text truncation of the link.

When you specify a numeric parameter tourlizetruncIt will truncate the displayed link text based on this number. If the original link text exceeds the number you set, the part beyond will be indicated by an ellipsis (...This feature is particularly important in situations where display length needs to be strictly controlled, such as in article summaries, card views, or mobile layouts.

urlizetruncShould the domain part be prioritized?

Regarding your question: urlizetruncDoes the filter provide options, and does it prioritize retaining the domain part of the URL when truncating? According to the template filter document provided by Anqi CMS, the answer is: CurrentlyurlizetruncThe filter does not provide a direct option to prioritize retaining the domain part of the URL.

This means that whenurlizetruncWhen truncating a URL, it will count from the beginning of the link text according to the character length you set and truncate, without intelligently judging and prioritizing the domain part of the URL. For example, if you have a long URL:https://www.florian-schlachter.de/blog/post/long-article-title.htmlTruncated to 15 characters, it might display ashttps://www.f...instead ofwww.florian-schlachter.de/...Orflorian-schlachter.de/....

in the documenturlizetruncThis example also verifies this

{% filter urlizetrunc:15|safe %}
Please mail me at [email protected] or visit mit on:
- lorem ipsum http://www.florian-schlachter.de lorem ipsum
{% endfilter %}

The output is similar to:- lorem ipsum <a href="http://www.florian-schlachter.de" rel="nofollow">http://www.f...</a> lorem ipsum

It can be seen from this example that truncation occurs at the front end of the entire link text and does not intelligently bypass the protocol header or path to retain the core domain name.

Why is this important and what can we do?

For website operators, it is crucial to retain the domain part of the URL in some scenarios.A domain name is part of a brand identifier, helping users quickly identify the source of the link and enhance trust.If the domain itself becomes unrecognizable after truncation, it may affect the user's judgment and willingness to click on the link.

due tourlizetruncThere is no built-in domain priority retention feature, and we need to pay attention to the following points when using it:

  1. Set the truncation length reasonably:According to the URL structure and page design of your website, try to set a sufficiently long truncation number to ensure that the domain name can be displayed in most cases.
  2. Considerurlize:If the display space of the link allows, and you want to display all the URLs in full without truncation, use it directlyurlizeThe filter will be a better choice.
  3. Considerations from the design perspective:When designing the content display area, allocate sufficient space for URLs to reduce the need forurlizetruncexcessive truncation.

The Anqi CMS template system is known for its flexibility, although currentlyurlizetruncNo direct domain preference option is provided, but understanding its existing behavior can help us better plan content display and choose the most appropriate link handling method according to actual needs.


Frequently Asked Questions (FAQ)

  1. Q:How can I make sure that the domain name is always displayed completely?A:The most direct method is to increaseurlizetruncTruncate the length parameter so that it is greater than the length of the longest domain name on your website (usually between 20-30 characters) or use it directlyurlizeThe filter will display the full URL without truncation.
  2. Q: urlizeandurlizetruncWhat are the main differences between them?A: urlizeThe filter is only responsible for converting URLs in the text into clickable hyperlinks without truncation.urlizetruncInurlizeBased on that, a function to truncate link text to a specified number of characters has been added, with the excess part displayed with an ellipsis.
  3. Q: urlizetruncIs the truncation length parameter calculated by character count or byte count?A: urlizetruncThe truncation length parameter is calculated by the number of characters, including the URL protocol (such ashttp://orhttps://) and path. For example, if set to 15, the truncation will start from the 15th character of the URL.