In AnQiCMS template development, handling links in the content is a common and important step.urlizeThe filter is born to meet this need, it can greatly simplify the recognition and formatting work of links in templates, while also taking into account user experience and search engine optimization.
urlizeThe filter is a core feature in the AnQiCMS template
urlizeThe core function of the filter is to intelligently recognize URLs and email addresses contained in a block of plain text content and automatically convert them into clickable HTML hyperlinks (<a>Label). This is very practical in many scenarios, such as when users enter URLs in comments or messages, the website does not need to manually review or force the addition of HTML tags,urlizeIt can automatically format it into a clickable link.
What's more worth mentioning is,urlizeThe filter will also automatically add to these links when generating hyperlinks.rel="nofollow"Property.This attribute is crucial for the website's search engine optimization (SEO), as it informs search engines not to track these links, thereby avoiding passing the page's 'weight' to external sites. This helps protect the SEO effect of the website itself.urlizeThe filter effectively manages link relationships, maintaining good SEO health.
Due tourlizeThe output of the filter is HTML code, in order to ensure that the browser can correctly parse and render these hyperlinks, it usually needs to be used in conjunction with the AnQiCMS template.|safeFilter.|safeThe filter tells the template engine that this content is safe HTML and does not need to be escaped twice, so hyperlinks can be displayed as clickable on the page.
urlizeIt supports an optional boolean parameter to control whether the output link text is HTML entity encoded. For example, if the link contains special characters like"In default, it will be escaped as 【en】:"By setting this parameter to 【en】:falseCan retain the original characters, but in most web development scenarios, maintaining the default escaping behavior is safer.
urlizetruncA convenient option for truncating links
Excepturlize,AnQiCMS also provides another filter with a similar function——urlizetrunc. Its core function is similar to that of——urlize, that is, to identify and convert it into a clickable hyperlink, and automatically addrel="nofollow"Properties. However,urlizetruncunique feature is that it allows you to specify the maximum display length of the link text. When the original URL is too long, it will use...To truncate the display text, making the page layout more tidy and enhancing the visual experience.
For example, a very long URL on the page may stretch the layout, affecting its appearance. ByurlizetruncYou can display the long link as, for example,http://www.anqicms.com/long-pa...such as this format, which maintains the link function and optimizes page display.
Example of practical application scenarios
urlizeandurlizetruncThe filter plays an important role in various scenarios:
- User comments and message boards:Automatically converts URLs in the text published by users into clickable links, enhancing interactivity.
- Article content processing:For plain text articles imported from other channels or unformatted links entered manually by users, quickly convert them to hyperlinks without the need for editors to manually add HTML tags.
- News aggregation or content collection:Batch process article content containing links, ensuring the availability of links and SEO compliance.
- Blogs and personal websites:Automatically beautify links in content while maintaining the SEO health of the website.
How to use these filters in AnQiCMS template
It is very simple to use these filters. You just need to pass the variables to be processed through the pipe|and connect to the filter.
BasicurlizeUsage:
<p>我的网站地址是:{{ "https://en.anqicms.com"|urlize|safe }},欢迎访问!</p>
<p>你也可以发邮件给我:{{ "[email protected]"|urlize|safe }}</p>
The code will output clickable website links and email links. Please note.|safeUse it to ensure that HTML tags are rendered correctly.
With escape control.urlize:If your content contains HTML characters that need to be escaped, and you wanturlizealso escape the content displayed in the link (usually,urlizeThe explicit control ability is already considered, but it is displayed here for demonstration purposes:)
<p>这是一个包含特殊字符的链接:{% filter urlize:true|safe %}</p>
<p>详细信息请访问 www.example.com/search?query="test" lorem ipsum</p>
<p>{% endfilter %}</p>
urlizetruncImplement link truncation:When the link is long, you can useurlizetruncto truncate the displayed text, for example, truncating to 15 characters:
<p>这是一个很长的链接,我希望它能被截断显示:{% filter urlizetrunc:15|safe %}</p>
<p>请访问 http://www.anqicms.com/some/very/long/path/to/a/page.html 了解更多。</p>
<p>{% endfilter %}</p>
This code will output something like "Please visit"http://www.anqicms.... Learn more." effect.
In summary, AnQiCMS'surlizeandurlizetruncFilters are an indispensable tool in website content operations. They not only automate link formatting and enhance user experience, but also add automatically through their functionality.nofollowAttributes, help you better manage the SEO of your website, making your website content more professional, more user-friendly, and more competitive.
Common Questions (FAQ)
Why did I use
urlizeAfter the filter, the content displayed directly on the page is<a>Is this a label code, rather than a clickable link?This is usually because you forget tourlizeadd after the filter|safeFilter.urlizeThe filter will generate HTML code (<a>Tag), while AnQiCMS templates, for safety reasons, default to escaping all output content with HTML entities. If you do not use|safethe filter, the template will<a>The tag itself is also treated as escaped plain text for display, rather than being parsed by the browser as a clickable link. The correct usage should be{{ 变量|urlize|safe }}.urlizeWhat types of links does the filter automatically recognize? Does it support Chinese links?urlizeThe filter mainly identifies links that start withhttp:///https:///www., as well as standard email addresses (such as[email protected])。For Chinese links, if they follow the above URL format,urlizeGenerally able to identify correctly.But in some complex situations, especially when the link contains Chinese or other non-ASCII characters, there may be differences in how the browser displays and parses the link, and it is recommended to test.If the URL itself is recognized after URL encoding, it is usually also handled correctly.urlizeautomatically added by the filterrel="nofollow"What is the role of this attribute? Where can I see its effect?rel="nofollow"The function of the attribute is to inform the search engine that the current page does not want to pass the 'reliability' or 'weight' to the page linked to by this link.This is very useful when dealing with user-generated content (such as comments, messages) or linking to external websites that are not fully trusted, it can prevent negative impacts on your website's SEO from malicious links, and avoid being mistakenly regarded by search engines as participating in link buying and other illegal activities.urlizegenerated<a>tags, and you will findrel="nofollow"The property was automatically added.