Does the `urlizetrunc` filter provide an option to prioritize retaining the domain part of the URL when truncating?

Calendar 👁️ 73

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.

Related articles

How to ensure that the `urlize` filter only acts on text and does not affect the `src` or `href` attributes of images or other HTML elements?

In Anqi CMS template development, the `urlize` filter is a very practical tool that can intelligently identify URLs or email addresses in text and automatically convert them into clickable hyperlinks.This brings great convenience to us in processing user input or pure text content obtained from other sources, making the information more friendly and interactive when displayed on the front end.

2025-11-09

Does the `urlize` filter correctly encode URLs containing special characters (such as `&`, `=`)?

When using Anqi CMS for content operations, we often need to reference external links or email addresses in articles.To make these links clickable while also ensuring the page's neatness and security, Anq CMS has built-in many practical template filters, among which the `urlize` filter is a good helper for dealing with such needs.

2025-11-09

If the text contains duplicate URLs, will the `urlize` filter generate the same `<a>` tags repeatedly?

In daily website content operation, we often need to include various links in articles or pages, whether they point to other pages within the site or refer to external resources.AnQiCMS (AnQiCMS) provides many convenient tools to simplify this process, among which the `urlize` filter is a very practical feature.It can automatically identify URLs or email addresses in text and convert them into clickable HTML links.However, when using this convenient feature, a common question may arise in one's mind: if our text contains multiple identical URLs

2025-11-09

Does the `urlize` filter support custom attributes of the generated `<a>` tag, such as `data-*`?

AnQiCMS (AnQiCMS) plays an important role in website construction and content operation with its efficient and flexible content management features.In daily content display and template development, we often take advantage of the built-in various filters to conveniently handle data.Among them, the `urlize` filter is a very practical tool that can automatically identify links in text and convert them into clickable HTML `<a>` tags.However, does the `urlize` filter support adding custom properties like `data-*` to these automatically generated `<a>` tags

2025-11-09

How to use the `urlize` filter to reduce the workload of content editors manually adding links?

In the daily operation of websites, content editors often need to manually add hyperlinks to URLs or email addresses in articles or descriptions. This task is not only time-consuming but also prone to errors, affecting the efficiency and accuracy of content publication.Fortunately, AnQiCMS provides a very practical template filter `urlize`, which can help us automate this process and greatly reduce the workload of manually adding links.What is the `urlize` filter?

2025-11-09

Does the `urlize` filter work in all AnQiCMS built-in template tags (such as `system`, `contact`)?

In AnQiCMS template development, flexibly using built-in tags and filters is the key to improving content display efficiency.Many users often encounter a question when dealing with text output: Can the `urlize` filter be directly applied to the output of all built-in template tags (such as `system` and `contact`)?

2025-11-09

Does the `urlize` filter recognize and convert URLs with non-standard ports (such as `http://example.com:8080`)?

## Deeply analyze the `urlize` filter of Anqi CMS: Can it intelligently recognize and convert URLs with non-standard ports?In Anqi CMS template development and content creation, the `urlize` filter is a very practical tool that can automatically identify URLs and email addresses in text content and convert them into clickable HTML links. It can also automatically add the `rel="nofollow"` attribute according to the configuration, which is very beneficial for SEO optimization and user experience.However, a common problem in daily operations is

2025-11-09

How to apply the `urlize` filter to parse URLs in the output of a custom content model field?

In Anqi CMS, the flexibility of the content model is one of its core strengths, allowing us to create various custom fields to carry specific types of content based on different business needs.When we have customized some fields to store URL links, such as "External Reference Link" or "Download Address of Related Resources", you may find that these links are displayed as plain text on the front-end page by default and cannot be clicked to jump directly.This not only affects user experience, but also fails to fully utilize the value of the link.

2025-11-09