In the daily content operation of AnQi CMS, we often need to handle various forms of URLs, especially in user comments, article citations, or other dynamically generated content. Automatically converting plain text URLs into clickable links is a basic and important feature. AnQi CMS provides powerfulurlizeFilter to meet this requirement.

urlizeThe core function and role of the filter

urlizeFilter is a very practical tool in the Aqie CMS template engine, which can intelligently recognize URL strings in text and automatically wrap them in HTML's<a>Label it to make it clickable. For example, a piece of text containing.http://www.anqicms.comorwww.anqicms.comEven justanqicms.comafterurlizeProcessed by the filter, will all become similar.<a href="http://www.anqicms.com" rel="nofollow">www.anqicms.com</a>Links. It is worth noting that,urlizeThe filter will also automatically add these generated links.rel="nofollow"Properties, this is a very beneficial default setting for search engine optimization (SEO) strategies, especially when handling user-generated content.

Support for complex URLs with query parameters

Surrounding “urlizeFilter whether it supports identifying and converting complex URLs with query parameters (query string)? This topic is affirmative. Anqi CMS'surlizeThe filter can not only identify standard URL formats, but also correctly identify and convert complex URLs that contain query parameters (such as?param1=value1&param2=value2).

What's more remarkable is,urlizeThe filter will also intelligently handle special characters in query parameters when processing these complex URLs. For example, if the URL contains unencoded double quote characters (such aswww.example.com/test="param")urlizeFilter is generated in<a>TagshrefProperties, it will automatically encode these special characters into URL encoding (such as"Converted to%22Ensure that the generated link is valid and accessible. However, in the visible text part of the link, these characters usually remain unchanged to enhance readability.

This greatly simplifies the development and content management process for scenarios that require handling various dynamic links, such as URLs with tracking parameters, filter conditions, or specific identifiers, without the need to manually write complex regular expressions to match and convert these links.

Actual operation and code examples

In the templates of Anqi CMS, useurlizeThe filter is very intuitive. Suppose we have a text that may contain complex URLs, we can use it like this:

<p>以下内容来自用户评论,其中可能包含带有查询参数的复杂URL:</p>
{% filter urlize:false|safe %}
<p>请访问我们的新产品页面:https://shop.anqicms.com/products?category=electronics&amp;filter=new&amp;source=blog</p>
<p>或者查看这个带有特殊字符测试的链接:www.anqicms.com/search?q="CMS%20tools"</p>
<p>如果想直接测试带有未编码双引号的复杂URL,如:www.yourdomain.com/path?key="value"</p>
{% endfilter %}

AfterurlizeThe output of the above code after the filter will be similar to this (some content may have been simplified or truncated for clarity):

<p>以下内容来自用户评论,其中可能包含带有查询参数的复杂URL:</p>
<p>请访问我们的新产品页面:<a href="https://shop.anqicms.com/products?category=electronics&amp;filter=new&amp;source=blog" rel="nofollow">https://shop.anqicms.com/products?category=electronics&amp;filter=new&amp;source=blog</a></p>
<p>或者查看这个带有特殊字符测试的链接:<a href="http://www.anqicms.com/search?q=%22CMS%2520tools%22" rel="nofollow">www.anqicms.com/search?q="CMS%20tools"</a></p>
<p>如果想直接测试带有未编码双引号的复杂URL,如:<a href="http://www.yourdomain.com/path?key=%22value%22" rel="nofollow">www.yourdomain.com/path?key="value"</a></p>

As you can see,urlizeThe filter successfully identified the URL containing query parameters and converted it into a clickable link. Forwww.yourdomain.com/path?key="value"Such a URL, it even automatically encodes double quotes in query parameters correctly.%22This ensures the validity of the link.

If you need to control the display length of the link text, you can useurlizetruncFilter. For example, truncate the link text to a specified length and add an ellipsis at the end:

<p>使用 urlizetrunc 截断显示长 URL:</p>
<p>{% filter urlizetrunc:40|safe %}</p>
<p>这是一个非常非常长的 URL,带有很多查询参数:https://en.anqicms.com/long/path/with/query?param1=verylongvalue1&param2=anotherlongvalue2&param3=finalvalue3</p>
{% endfilter %}

This code will output:

<p>使用 urlizetrunc 截断显示长 URL:</p>
<p>这是一个非常非常长的 URL,带有很多查询参数:<a href="https://en.anqicms.com/long/path/with/query?param1=verylongvalue1&amp;param2=anotherlongvalue2&amp;param3=finalvalue3" rel="nofollow">https://en.anqicms.com/long/path/...</a></p>

urlizetruncOptimized the page layout and readability while ensuring the link function.

Attention Points and **Practice

When usingurlizeThe filter has several points to note:

  • |safeThe cooperative use of filtersDue tourlizeThe filter will generate HTML tags, in order to prevent the template engine from escaping them twice (i.e., to<Converted to&lt;English),usually requires the use of|safeFilter. This tells the template engine,urlizeoutput is safe HTML content, which can be rendered directly.
  • rel="nofollow"Property:default addednofollowIt is very beneficial for scenarios such as user comments, which can effectively avoid the negative impact of link spam on website SEO. If it is indeed necessary to pass link weight in specific scenarios (i.e., dofollow链接),urlizeFilter may not be **selected, or it may need to be processed with other logic.
  • Readability and length: For very long URLs, evenurlizeCan be correctly converted, it may also affect the visual neatness of the page. At this time,urlizetruncThe filter is a good alternative, which can shorten the displayed text while maintaining the link function.

In summary, the security CMS ofurlizeFilter is a powerful and intelligent tool that can easily handle complex URLs containing query parameters, and automatically process related encoding issues, greatly enhancing the efficiency and user experience of content operation.


Common Questions (FAQ)

  1. Q:urlizeThe link generated by the filter is default withhttp://orhttps://is there? Even though it is not explicitly written in the original text? Answer:Yes,urlize