In website content creation, we often need to mention URLs and email addresses in articles, descriptions, or comments.If this information is only plain text, users will not be able to click through directly, which undoubtedly affects the user experience and the efficiency of information transmission.AnQiCMS as an efficient content management system fully considers this requirement and built-in intelligent functions that can automatically convert URLs and email addresses in plain text to clickable HTML links.
AnQiCMS automatically converts link functionality:urlizeFilter
AnQiCMS uses a template engine syntax similar to Django, whereurlizeThe filter is the key to implementing this function. It can intelligently scan text content and identify URLs (such ashttp:///https:///www.addresses starting with the prefix, even some naked domain names likeanqicms.comAnd email address format such as[email protected]The string and automatically wraps it in<a>tags to make them clickable links.
The convenience of this feature lies in the fact that you do not need to manually write HTML code to create links. Simply pass the plain text content tourlizeThe filter, the system will automatically handle it, greatly improving the efficiency of content editing. In addition,urlizeThe filter will also automatically add it kindly when converting links.rel="nofollow"Attribute, this is very helpful for the SEO optimization strategy of the website, especially in managing the weight transmission of external links.
Usage:
In the AnQiCMS template,urlizeFilter through the pipe symbol.|Apply to any variable containing text. SinceurlizeThe conversion will generate HTML tags, in order to ensure that these HTML tags can be parsed correctly by the browser and not displayed as plain text, we usually need to use in conjunction withsafeFilter. Otherwise, you might see something similar<a href=\"...\">...</a>Such original HTML code.
For example, if you have a variable namedarchive.ContentThe variable stores the article content, which includes plain text URLs and email addresses, and you can use it like this:
<p>{{ archive.Content|urlize|safe }}</p>
This code will convertarchive.ContentConvert all URLs and email addresses to clickable links and render them safely on the page.
Optimize the display of long links:urlizetruncFilter
For very long URLs, if displayed in full on the page, it may cause layout confusion and affect the overall aesthetics. AnQiCMS also providesurlizetruncFilter, it isurlizeand it adds the function of truncating link text.
urlizetruncYou can specify the maximum length of the link text displayed. If the text length of the original URL exceeds this limit, it will be abbreviated after the specified length....Replace the overflow part while maintaining the validity of the link.
Usage:
urlizetruncThe filter accepts a numeric parameter indicating the maximum display length of the link text (including an ellipsis).
<p>{{ long_description|urlizetrunc:50|safe }}</p>
In the above example,long_descriptionIf any link text exceeds 50 characters, it will be truncated and displayed...However, the underlying link address is still complete. This is very practical for scenarios such as news summaries and product descriptions that require a clean page.
Application scenarios and value
These filters can be used in any position of the AnQiCMS template, especially suitable for the following scenarios, bringing practical value to your content operation:
- Article detail pageEnsure that the links and email addresses in the displayed article content, abstract, keywords, etc. are clickable to enhance the convenience of users in obtaining relevant information.
- Category or single page descriptionIn the category introduction, about us and other single-page description text, it is convenient for users to quickly access related resources or contact information.
- User messages or commentsAutomatically process links and email addresses submitted by users to enhance interactivity (Please note that for user-generated content, additional security filtering measures may also be required).
- Custom fieldIf the content model contains custom text fields for storing URLs or email addresses, these filters can also be used to ensure that the frontend display and functionality are consistent and complete.
ByurlizeandurlizetruncFilter, AnQiCMS helps you easily achieve:
- Enhancing user experience: Users do not need to copy and paste, just click to access, reducing the threshold of operation.
- Optimize content display: Especially
urlizetruncMake long links not destroy the page layout, keep the interface neat. - Indirectly assist SEO: Automatically added.
nofollowThe attribute helps website administrators better control the impact of external links on the site's weight, which is one of the standard SEO practices. - Improve operational efficiency: The content publisher does not need to pay attention to the HTML format of the link, focusing on content creation itself.
Frequently Asked Questions (FAQ)
1.urlizeandurlizetruncWhat are the main differences of the filter?
urlizeThe filter will completely convert URLs and email addresses in the text into clickable links, with the link text being the original URL or email address. AndurlizetruncAt the same time, you can specify a maximum length, and if the original link text exceeds this length, it will be truncated and an ellipsis will be added at the end....To keep the page neat.
2. Why do you need to addurlizeorurlizetruncAfter that, the displayed is HTML code instead of a clickable link?This is usually because you forgot to add at the end of the filter chain.safeFilter. AnQiCMS template engine, for security reasons, defaults to escaping all output content to prevent cross-site scripting attacks (XSS).urlizeandurlizetruncThe generated is an HTML tag, if it is not appliedsafeFilter, these tags themselves are escaped to plain text, causing the displayed on the page is<a href="...">such code.
3.urlizeAutomatically addedrel="nofollow"Can the attribute be removed or modified?According to the default design of AnQiCMS,urlizeThe filter will automatically add to the converted linkrel="nofollow"Property. This is a built-in behavior designed to help websites better manage the SEO impact of external links.The option to directly remove or modify this property through the template filter itself is not publicly provided at present.If you have specific requirements, you may need to consider manually inserting links HTML at the time of content publication, or through other frontend JavaScript methods for processing.