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 a variety of template filters to help us handle these links.urlizeandurlizetruncIt is two very practical tools, which can automatically convert URLs in text to clickable links.

KnowurlizeFilter: Starting point of automatic links

Before getting a deeper understandingurlizetruncBefore that, let's take a look aturlizeFilter. Its main function is to identify and convert URLs (such ashttp://www.example.com/www.example.comevenexample.com) and email addresses (such as[email protected]) automatically, and convert them into standard HTML hyperlinks (<a href="...">...</a>Label). For SEO considerations, it will also automatically addrel="nofollow"Attributes, prevent search engine weight transmission. This filter is very convenient, ensuring that all links in the article are clickable, enhancing user experience.

urlizeThe optional parameter provides control over whether the link text is escaped when converted to HTML. For example, if you have a URL containing special characters, you can choose whether to allowurlizeDisplay it as raw characters or escaped characters.

urlizetruncFilter: a utility for link truncation

urlizetruncThe function of the filter isurlizeSimilar, it will also recognize URLs and email addresses in the text and convert them into clickable links. However,urlizetruncAn important feature has been added on this basis:Link text truncation.

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 when displaying content with strict length control, such as in article summaries, card views, or mobile layouts.

urlizetruncDo you want to prioritize retaining the domain part?

Regarding your question:“urlizetruncFilter provides options, does it prioritize retaining the domain part of the URL when truncating?CurrentlyurlizetruncThe filter does not provide direct options to prioritize retaining the domain part of the URL.

This means that,urlizetruncWhen truncating a URL, it will count from the beginning of the link text based on the character length you set, without intelligently judging and prioritizing the domain part of the URL. For example, if you have a very long URL:https://www.florian-schlachter.de/blog/post/long-article-title.html,and it may be truncated to 15 charactershttps://www.f...,instead ofwww.florian-schlachter.de/...orflorian-schlachter.de/....

in the documenturlizetruncThe example also proves 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:- 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 beginning of the entire link text, without intelligently bypassing the protocol header or path to retain the core domain name.

Why is this important, and what can we do?

For website operators, retaining the domain part of the URL is crucial in certain scenarios.The domain is part of the brand identity, which helps users quickly identify the source of the link and enhance trust.If the domain name 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 reservation feature. We need to pay attention to the following points when using it:

  1. Reasonable setting of truncation length:According to the URL structure and page design of your website, try to set a sufficiently long truncation number to ensure that most of the domain names can be displayed completely.
  2. Considerurlize:If the display space of the link allows, and you wish to display all the URLs completely without truncation, use it directly.urlizeFilters would be a better choice.
  3. Considerations on the design level:When designing the content display area, reserve sufficient space for URLs to reduce the need forurlizetruncexcessive truncation.

The template system of Anqi CMS is renowned for its flexibility, although currentlyurlizetruncThe domain priority retention option is not directly provided, but understanding its existing behavior can help us better plan content display and choose the most suitable link handling method according to actual needs.


Common Questions (FAQ)

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