In daily website operations, we often need to mention website URLs or email addresses in the article content.If this information is only plain text, users cannot directly click to jump, they need to manually copy and paste, which undoubtedly increases the complexity of operations and may cause users to churn.幸运的是,AnQiCMS 提供了一种非常简便且高效的方法,能够让网站自动识别文本中的 URL 和邮箱,并将其智能转换为可点击的超链接,大大提升网站的用户体验和互动性。
Why is automatic recognition and conversion so important?
Imagine when a user is browsing your product description or service details, if there is a technical support email or a product purchase link on the page, and these links are live and clickable, the user can reach the required information immediately.This not only saves users' time, but also makes the website content look more professional and modern.nofollowProperties link), also helps search engines better understand the structure and external reference relationships of the website content, althoughurlizeThe filter will be automatically added.rel="nofollow"Properties, but it still helps to improve user experience, thereby indirectly having a positive impact on SEO.
The magic of intelligent link conversion in AnQiCMS:urlizeFilter
In AnQiCMS, the key to achieving this automatic conversion lies in using a namedurlizeThe template filter. This filter can intelligently detect URLs in the text content (including those starting withhttp:///https://, or those starting withwww., or even bare domain names likeexample.com) and email addresses (such as[email protected]),then automatically add them.<a href="...">Label them to make them clickable hyperlinks. To follow the practices of Search Engine Optimization (SEO),urlizeThe filter will also automatically add these generated links.rel="nofollow"properties.
to useurlizeFilter, you just need to add it to the end of the template variable when you need to output the text content to be processed.
For example, on your article detail page, there is usually such code to display the article content:
{{ archive.Content|safe }}
Here are thearchive.ContentThis represents the main content of the article.|safeThe filter is used to inform AnQiCMS that this part of the content is safe HTML, which does not require additional HTML entity escaping, so that the rich text editor can display the formatting effects normally.
Now, to make the URLs and email addresses in it automatically clickable, we just need to|safeadd before the filter,|urlize:
{{ archive.Content|urlize|safe }}
It's so simple! After refreshing the page, you will find that all the URLs and email addresses in the article content have become blue, clickable hyperlinks.
Control the length of the link text:urlizetruncFilter
Sometimes, the content of your articles may contain very long URLs, and displaying them unchanged may affect the aesthetics and layout of the page. AnQiCMS also takes this into consideration, providingurlizetrunca filter. This filter is different fromurlize功能类似,同样能自动识别并转换 URL 和邮箱,但它额外允许您指定超链接文本的最大显示长度。如果原始 URL 文本超过了这个长度,超出的部分就会被...The ellipsis is used to keep the page neat.
urlizetruncThe usage is also intuitive, you need to pass a numerical parameter to it, indicating the maximum number of characters you want the linked text to display.
For example, if you want the linked text to display a maximum of 30 characters:
{{ archive.Content|urlizetrunc:30|safe }}
Here, even if the original link is very long, only the first 30 characters will be displayed on the page, followed by an ellipsis.When the user clicks this truncated link, it will still jump to the complete original URL.
Where can these filters be used?
These filters are mainly suitable for fields containing a large amount of text, such as:
- Article details page(
archive.Content):The most common application scenarios, making the reference links and contact information in your blog articles and news updates more user-friendly. - Single page content(
page.ContentFor example, the official website link on the "About Us" page, or the email address on the "Contact Us" page. - Other rich text or long text custom fieldsIf you have defined other long text fields in your content model and expect them to contain URLs or email addresses, these filters can also be applied.
Please note when using, due tourlizeandurlizetruncIt will generate HTML tags, so they often need to be paired with|safefilters to ensure that the browser can correctly parse and render these HTML codes.
By making such simple configuration, you can make the content of AnQiCMS website more intelligent and interactive, providing visitors with a smoother browsing experience while maintaining the professionalism of the website content.
Common Questions (FAQ)
1. How can I prevent certain specific text paragraphs on my website from being automatically converted?
If you do not want a specific text paragraph's URLs or email addresses to be automatically converted to links, the most direct method is to avoid using the corresponding template variable in that paragraph.|urlizeor|urlizetruncFilter.You can choose to output this part of the content in a variable that does not need to be automatically converted.If the content is entered through a rich text editor, you can also manually remove the link from the editor, or make sure it is not a standard URL or email format, so that it is not recognized by the filter.
2. How to choose usageurlizeOrurlizetrunc?
This mainly depends on your needs for page aesthetics.
- If there are not many URLs in your content, or most URLs are of moderate length,
urlizeit is enough, it will display the full URL text, with more complete information. - if your content often contains long URLs, use
urlizetruncCan make the page layout neater, avoid long links from disrupting visual balance. You can adjust according to the actual effect,urlizetrunc:数字find the display length that is most suitable for your website.
3. What is the impact of this feature on SEO?
urlizeandurlizetruncThe filter will automatically add to the generated linksrel="nofollow"nofollowLinks, they still have a positive impact on the overall SEO performance of the website.