In daily website operation, we often need to mention URLs or email addresses in the article content.If this information is just plain text, users cannot directly click to jump, and they need to manually copy and paste, which undoubtedly increases the complexity of the operation, and may also cause users to lose interest. 幸运的是,AnQiCMS provides a very simple and efficient method that allows the website to automatically identify URLs and email addresses in text and convert them into clickable hyperlinks, greatly enhancing the user experience and interactivity of the website.
Why is automatic recognition and conversion so important?
Imagine when the user browses your product introduction or service details, if there is a technical support email or 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 appear more professional and modern.From the perspective of search engine optimization, appropriate internal and external links (even those withnofollowThe link of the attribute) also helps search engines better understand the structure and external reference relationships of the website content, althoughurlizeThe filter will be added automaticallyrel="nofollow"Properties, but it still helps to improve user experience, thus indirectly having a positive impact on SEO.
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 text content (including thosehttp:///https://starting withwww.starting with, or even bare domain names likeexample.com) and email addresses (such as[email protected]),then they are automatically added with<a href="...">tags to make them clickable hyperlinks. In order to follow the practices of Search Engine Optimization (SEO),urlizeThe filter will also automatically add to these generated linksrel="nofollow"Property.
To useurlizeThe filter, you just need to add it to the template variable behind the text content you need to process.
For example, on your article detail page, there is usually such code to display the article content:
{{ archive.Content|safe }}
Herearchive.Contentrepresents 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 layout effect normally.
Now, to automatically convert URLs and email addresses in it to clickable links, we just need to add|safebefore the filter,|urlize:
{{ archive.Content|urlize|safe }}
It's that simple! After refreshing the page, you will find that all the URLs and email addresses in the article content have turned into blue, clickable hyperlinks.
Control the length of the link text:urlizetruncFilter
Sometimes, your article content may contain very long URLs, and if displayed as is, it may affect the aesthetics and layout of the page. AnQiCMS has also considered this point and providedurlizetruncFilter. This filter is different fromurlizeFunction similar, it can also automatically recognize and convert URLs and email addresses, but it additionally allows you to specify the maximum display length of the hyperlink text. If the original URL text exceeds this length, the part that exceeds will be...An ellipsis is used to replace, thus keeping the page tidy.
urlizetruncThe usage is also very intuitive, you need to pass a numeric parameter to it, indicating the maximum number of characters you want the link text to display.
For example, if you want the link text to display a maximum of 30 characters:
{{ archive.Content|urlizetrunc:30|safe }}
This way, 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 on this truncated link, it will still jump to the full original URL.
Where can these filters be used?
These filters are mainly suitable for fields that contain a large amount of text, such as:
- Article detail page(
archive.ContentThe most common application scenario makes it easier to use the reference links and contact information in your blog posts and news updates. - 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 your content model defines other long text fields and you expect them to contain URLs or email addresses, you can also apply these filters.
Please note when using, due tourlizeandurlizetruncGenerate HTML tags, therefore, they are usually followed by|safefilters to ensure that the browser can correctly parse and render the HTML code.
By making such a simple configuration, you can make the AnQiCMS website's content more intelligent and interactive, providing visitors with a smoother browsing experience while maintaining the professionalism of the website's content.
Frequently Asked Questions (FAQ)
1. How do I prevent certain paragraphs of text on my website from being automatically translated?
If you do not want a specific text paragraph's URL or email address to be automatically converted to a link, the most direct method is to avoid using the corresponding template variable in the paragraph.|urlizeor|urlizetruncFilter. You can choose to output this part of the content in a separate variable that does not need to be automatically converted.If the content is entered using a rich text editor, you can also manually remove the link from the editor, or ensure that it is not a standard URL or email format to prevent it from being identified by the filter.
How to choose to useurlizeOrurlizetrunc?
It mainly depends on your needs for the beauty of the page
- 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, more complete information. - If your content often contains long URLs, use
urlizetruncIt can make the page layout neater, avoiding long links from breaking the visual balance. You can adjust according to the actual effect.urlizetrunc:数字To find the display length that is most suitable for your website.
3. How does this feature affect SEO?
urlizeandurlizetruncThe filter will automatically add to the generated linkrel="nofollow"The attribute informs the search engine that this link should not be used to pass "weight" or as a recommendation.This means that these automatically generated links will not directly boost the search engine ranking of the target page.However, this feature has a significant positive impact on user experience (UX), making it easier for users to click and access relevant information.A good user experience is an indirect factor affecting SEO, because users are more willing to stay longer on a website with a good experience, which helps to reduce the bounce rate and increase the depth of page visits, all of which are important indicators for search engines to measure the quality of a website.Therefore, although it isnofollowLinks, they still have a positive impact on the overall SEO performance of the website.