In the daily content operation of Anqi CMS, we often need to deal with various forms of URLs, especially in user comments, article references, or other dynamically generated content, converting plain text URLs into clickable links is a basic and important function. Anqi CMS provides powerfulurlizeFilter to meet this requirement.
urlizeThe core function and role of the filter
urlizeThe filter is a very practical tool in Anqi CMS template engine, which can intelligently identify URL strings in text and automatically wrap them in HTML's<a>Make it clickable in the tag. For example, a text that containshttp://www.anqicms.comorwww.anqicms.comeven justanqicms.comAfterurlizeAfter the filter is processed, they will all become similar<a href="http://www.anqicms.com" rel="nofollow">www.anqicms.com</a>such links. It is worth noting that,urlizeThe filter will also automatically add to these generated linksrel="nofollow"Property, this is a very beneficial default setting for search engine optimization (SEO) strategies, especially when dealing with user-generated content.
Support for complex URLs with query parameters
Surrounded by “urlizeDoes the filter support the recognition and conversion of complex URLs with query parameters?" This topic is affirmative. AnQi CMS."}urlizeThe filter can not only recognize standard URL formats, but also correctly identify and convert complex URLs containing query parameters (such as?param1=value1¶m2=value2).
What's more impressive is,urlizeThe filter also intelligently handles 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")urlizeThe filter generates,<a>label'shrefWhen an attribute is present, it will automatically encode these special characters in the URL (such as"to%22Make sure the generated link is valid and usable. However, these characters are usually kept as they are in the visible text part 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, filtering conditions, or specific identifiers, without manually writing complex regular expressions to match and convert these links.
Practical operation and code example
Using in Anqi CMS template,urlizeThe 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&filter=new&source=blog</p>
<p>或者查看这个带有特殊字符测试的链接:www.anqicms.com/search?q="CMS%20tools"</p>
<p>如果想直接测试带有未编码双引号的复杂URL,如:www.yourdomain.com/path?key="value"</p>
{% endfilter %}
afterurlizeThe filter has processed, the output of the above code 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&filter=new&source=blog" rel="nofollow">https://shop.anqicms.com/products?category=electronics&filter=new&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%22It 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¶m2=anotherlongvalue2¶m3=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&param2=anotherlongvalue2&param3=finalvalue3" rel="nofollow">https://en.anqicms.com/long/path/...</a></p>
urlizetruncOptimized the page layout and readability while ensuring the link function.
Cautionary notes and **practice
While usingurlizeWhen using the filter, there are several points to note:
|safeThe use of filters in conjunction: Due tourlizeFilters generate HTML tags, to prevent the template engine from escaping them twice (i.e., to<to<etc.), it is usually necessary to use them in conjunction|safeFilter. This tells the template engine,urlizeThe output is safe HTML content that can be rendered directly.rel="nofollow"property: Default additionnofollowIt is very beneficial for scenarios such as user comments, which can effectively avoid the negative impact of link spam on website SEO. If there is indeed a need to pass link weight in specific scenarios (that is,dofollowLink),urlizeThe filter may not be** selected, or it may need to be processed in conjunction with other logic.- Readability and length: For very long URLs, even
urlizeCan be correctly converted, may also affect the visual cleanliness of the page. At this time,urlizetruncFilter is a good alternative, which can shorten the displayed text while maintaining the link function.
In summary, it is about AnQi CMS'surlizeThe filter 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 operations.
Frequently Asked Questions (FAQ)
- Question:
urlizeThe filter-generated link defaults to includehttp://orhttps://right? Even if it's not explicitly written in the original text? Answer:Yes,urlize