In AnQiCMS template development, handling links in content is a common and important aspect.urlizeThe filter is born to meet this need, it can greatly simplify the identification and formatting work of links in templates, while also taking into account user experience and search engine optimization.
urlizeThe filter is the core feature of the AnQiCMS template
urlizeThe core function of the filter is that it can intelligently identify URLs (URLs) and email addresses contained in a piece of plain text and automatically convert them into clickable HTML hyperlinks (<a>Tags). This is very useful 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 as a clickable link.
What's more worth mentioning is,urlizeThe filter will also automatically add these links when generating hyperlinks.rel="nofollow"The attribute is crucial for the website's search engine optimization (SEO), as it tells search engines not to follow these links, thus avoiding passing the page's 'weight' to external sites, which helps protect the website's own SEO effect.This means that even if the content contains a large number of external links, the website can still passurlizeThe filter effectively manages link relationships, maintaining good SEO health.
due tourlizeThe filter outputs 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, does not require further escaping, so hyperlinks can be displayed as clickable.
urlizeIt supports an optional boolean parameter to control whether the output link text is HTML entity escaped. For example, if the link contains special characters like"It will be escaped by default"By setting this parameter tofalseCan retain the original characters, but in most web development scenarios, keeping the default escaping behavior is safer.
urlizetruncA convenient option for truncating links.
excepturlize,AnQiCMS also provides another filter with similar functionality——urlizetrunc. Its core function is the same asurlize, that is, to identify and convert it into a clickable hyperlink and automatically add itrel="nofollow"Property. However,urlizetruncWhat is unique about it is that it allows you to specify the maximum display length of the link text. When the original URL is too long, it will be truncated after reaching the specified length and use...To truncate the displayed 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 the aesthetics. ByurlizetruncYou can display the long link as something like “http://www.anqicms.com/long-pa...” which maintains the functionality of the link and optimizes page display.
Example of practical application scenarios
urlizeandurlizetruncFilters play an important role in many scenarios:
- User comments and message board:Automatically convert URLs in the text published by users into clickable links to enhance interactivity.
- Article main content processing:For plain text articles imported from other channels, or links manually entered by users without formatting, quickly convert them into hyperlinks without the need for editors to manually add HTML tags.
- News aggregation or content collection:Batch process articles 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 templates
Using these filters is very simple. You just need to pass the variables you need to process through the pipe|to the filter.
BasicurlizeUsage:
<p>我的网站地址是:{{ "https://en.anqicms.com"|urlize|safe }},欢迎访问!</p>
<p>你也可以发邮件给我:{{ "[email protected]"|urlize|safe }}</p>
This code will output clickable website links and email links. Please note|safeto 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 default has been considered, but here we demonstrate its explicit control capability:
<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 very 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 visithttp://www.anqicms…. Learn more.” effect.
In summary, AnQiCMS'surlizeandurlizetruncThe filter is an indispensable tool in website content operation. It not only automates link formatting, enhances user experience, but also automatically addsnofollowProperty, helps you better manage the website's SEO, making your website content more professional, more user-friendly, and more competitive.
Frequently Asked Questions (FAQ)
Why did I use
urlizeAfter the filter, the content displayed directly on the page is<a>Label code, not a clickable link?This is usually because you forget tourlizeAdd after the filter|safefilter.urlizeThe filter generates HTML code (<a>Tags), while AnQiCMS templates, for safety reasons, default to escaping all output content to HTML entities. If you do not use|safefilters, the template will transform<a>The tag itself is treated as plain text, not as clickable links by the browser. The correct usage should be{{ 变量|urlize|safe }}.urlizeDoes the filter automatically recognize what types of links? Does it support Chinese links?urlizeThe filter primarily identifies links that start withhttp:///https:///www.with URLs that start with, as well as standard email addresses (such as[email protected])。For Chinese links, if they follow the above URL format,urlizeIt usually can be recognized correctly. However, in some complex cases, especially when links contain Chinese or other non-ASCII characters, there may be differences in how the browser displays and parses the links, and it is recommended to test.If the URL itself is recognized after URL encoding, it is usually handled correctly.urlizeFilter automatically addedrel="nofollow"What is the role of the attribute? Where can I see its effect?rel="nofollow"The attribute is used to inform the search engine that the current page does not want to pass the "trustworthiness" or "weight" to the page linked by this link.This is very useful when dealing with user-generated content (such as comments, messages) or pointing to external websites that are not fully trusted, it can prevent bad links from negatively affecting the SEO of your website, and avoid being misidentified by search engines as participating in link buying and other违规 behaviors.You can view it by using the browser's developer tools (Inspect Element)urlizeGenerated<a>Tags, and you will findrel="nofollow"The attribute was automatically added.