How to precisely set the truncation length of the URL link text for the `urlizetrunc` filter (e.g., 15 characters)?

Calendar 👁️ 83

In website content operation, we often encounter situations where it is necessary to display external links.These links may sometimes be very long, and displaying them directly can not only affect the overall aesthetics of the page, but may also destroy the layout, especially on mobile devices.Aqiq CMS understands this pain point and provides us with a very practical template filter——urlizetruncIt helps us elegantly handle these long URLs, keeping them clickable while also having a neat display effect.

Get to knowurlizetruncFilter: The combination of intelligent links and length restrictions

In simple terms,urlizetruncIt is a powerful content processing tool. Its core function is to intelligently recognize URLs in text (includinghttp:///https://URLs starting with,www.Domain names at the beginning, even email addresses, and then automatically convert them to clickable HTML<a>Link. What's more, it allows us to precisely set the display length of the link text. Once the link text exceeds the length we set,urlizetruncIt will automatically truncate and add an ellipsis (...To ensure that the link clearly conveys information without being too long.

This is similar to its "brother" filter.urlizeIt is somewhat similar,.urlizeIt can also convert URLs to links, but it does not truncate the link text, suitable for those who do not mind the complete display of links.urlizetruncIt focuses more on visually optimizing the page, providing a more refined display of content.

Why to useurlizetrunc?

Imagine that your article references a very long research report link, or a user submits a URL with complex query parameters. If noturlizetruncThese long links may stretch your article layout, causing paragraphs to be uneven or not fit in list items.Especially in responsive design, long URLs can easily cause text overflow, affecting the user's reading experience.

urlizetruncThe value lies here. By setting an appropriate truncation length, we can:

  • Keep the page tidy:Make the link length uniform, enhance visual consistency.
  • Optimize the reading experience:Avoid users from being distracted by a long URL and clearly knowing that there is a link.
  • Prevent layout destruction:Especially in card layouts or containers with fixed widths, long URLs are no longer a hassle.
  • Enhance mobile-friendliness:On small screen devices, concise link text can better adapt to limited display space.

How to precisely set the truncation length of URL link text.

urlizetruncThe filter is very intuitive to use, it is the same as the template filter of other safe CMS, through the pipe character|Apply it. The most common use is to apply it to a variable and pass a numeric parameter to specify the truncation length.

Basic syntax:

{{ 变量 | urlizetrunc:长度 | safe }}

Here, the 'length' is an integer representing the maximum number of characters you want the link text to display, this lengthContainsEllipsis at the end (...)

For example, to truncate a URL to display up to 15 characters:

Assuming we have a variabledocumentUrlIts value is"https://en.anqicms.com/documentation/template/filters/urlizetrunc-usage-guide".

We can apply it like this:urlizetrunc:

<p>原始链接: {{ documentUrl }}</p>
<p>截断为15个字符显示: {{ documentUrl | urlizetrunc:15 | safe }}</p>

The output of this code might look like this:

原始链接: https://en.anqicms.com/documentation/template/filters/urlizetrunc-usage-guide 截断为15个字符显示: <a href="https://en.anqicms.com/documentation/template/filters/urlizetrunc-usage-guide" rel="nofollow">https://www.an...</a>

Please note that we have additionally used|safefilter was used. This is becauseurlizetruncConverts the URL in the text to HTML<a>Label, in order for the browser to correctly parse and display this link instead of treating it as plain text, we need to explicitly tell the template engine that this content is 'safe' HTML code. If not|safeYou may see it displayed directly on the page<a href="...">...</a>Such original HTML string.

Process multiple URLs in a large block of text:

If you need to handle a complex text containing multiple URLs rather than a single variable, you can usefilterLabel collaborationurlizetruncto wrap the entire text block

Related articles

How to convert a URL to a clickable link while limiting the display text length?

In website content operation, we often need to refer to external links in articles or descriptions, which not only need to be convenient for users to click, but sometimes also need to control the length of the displayed text to maintain the neat layout and good reading experience of the page.Long URLs displayed directly not only affect aesthetics but may also distract users' attention.AnQiCMS provides a flexible and powerful template filter that can elegantly solve this problem.Why do we need to limit the length of displayed link text?When we paste the complete URL directly in the content

2025-11-09

What are the main differences in functionality between the `urlizetrunc` filter and the `urlize` filter?

In AnQi CMS, we often need to display some URLs or email addresses in the content, and we hope that they can be automatically turned into clickable links for visitors to jump directly.To achieve this function, Anqi CMS provides two very practical template filters: `urlize` and `urlizetrunc`.Although their purpose is to convert text to links, there are significant differences in their specific presentation.Understanding these differences can help us better manage website content and improve user experience.### `urlize` filter

2025-11-09

How to ensure that the URL in the original text is not escaped by the `urlize` filter?

In AnQi CMS template development, the `urlize` filter is undoubtedly a very practical tool, which can intelligently recognize URLs or email addresses in text and automatically convert them into clickable `<a>` tags.This is very beneficial for handling user submitted content or importing plain text content from external sources to provide a better user experience and SEO friendliness.However, when using `urlize`, some developers may encounter a confusing issue: why do URLs in the original text, especially those with query parameters or special characters, cause problems

2025-11-09

Does AnQiCMS's `urlize` filter automatically add the `rel="nofollow"` attribute to generated links?

In content operation, link management and search engine optimization (SEO) are inseparable important links.For those of us who use excellent content management systems like AnQiCMS, understanding the internal mechanism for processing links, especially whether filters like `urlize` automatically add the `rel="nofollow"` attribute, is crucial for precise control of the website's SEO performance.### AnQiCMS's `urlize` filter

2025-11-09

What are the potential impacts and benefits of AnQiCMS's `urlize` filter on the website's SEO (Search Engine Optimization)?

The `urlize` filter of AnQiCMS: More than convenient, it is a hidden pusher for website SEO In daily website operations, we often need to reference external or internal web page links.If these links are only in plain text form, users cannot directly click to access them, and search engines also find it difficult to effectively recognize their value as links.At this time, the `urlize` filter provided by AnQiCMS is particularly important.It is not just a convenient content processing tool, but also plays an unexpectedly positive role in the SEO of the website

2025-11-09

How to safely convert URLs to clickable links in the user comments or messages of AnQiCMS?

In website operation, we often encounter situations where users share links in the comment area or message board.These links that cannot be automatically converted into clickable forms not only affect user experience, but may also reduce the efficiency of information dissemination.However, directly converting user input text to HTML links poses potential security risks, such as malicious script injection.AnQiCMS (AnQiCMS) is a content management system that focuses on security and efficiency, providing a secure and convenient way to solve this problem.Why do we need to automatically convert URLs to clickable links

2025-11-09

Can the `urlize` filter recognize and convert URLs in the form of `www.example.com` or bare domain `example.com`?

In the daily content operation of AnQi CMS, we often need to convert links in text into clickable hyperlinks to enhance user experience and the interactivity of website content.AnQi CMS provides a very practical template filter——`urlize`.Many operators may be curious, can the `urlize` filter intelligently recognize and correctly convert domains like `www.example.com` or bare domains like `example.com`?The answer is affirmative.

2025-11-09

How would the `urlize` filter handle URLs that contain both HTTP and HTTPS protocols?

In the daily content publishing work, we often need to insert various URLs in the articles, some starting with HTTP, and some with HTTPS.Manually adding the `<a href="...">` tag to each URL is inefficient and prone to errors, especially when dealing with large amounts of content, where such repetitive work is particularly time-consuming and labor-intensive.AnQi CMS deeply understands these pain points of content creators, and therefore built-in a powerful and intelligent `urlize` filter in the template engine, aiming to make the automation processing of website links easy and simple.###

2025-11-09