When using AnQiCMS to manage website content, we often encounter the need to automatically convert URLs or email addresses in text to clickable links.urlizeandurlizetruncIt is a powerful tool to achieve this function. They can not only automatically recognize links in text but also automatically add links after conversionrel="nofollow"Property, this is very useful for SEO optimization.
However, when using these filters, many friends may be curious: is there a global option in the AnQiCMS backend settings that can control them uniformlyurlizeandurlizetruncWhat behavior? For example, can you turn them on or off with one key, or set them uniformly?urlizetruncHow long is the truncation? Today, let's delve deeper into this issue.
Get to know in-depthurlizeandurlizetrunc
First, let's briefly review.urlizeandurlizetruncThese two tools.
urlizeFilterIt mainly scans text content, automatically identifies it, and converts the URLs (such ashttp://www.example.com/www.example.comevenexample.com) and email addresses (such as[email protected]Convert to HTML's<a>tags. The converted links will be prefixed withrel="nofollow"The attribute helps tell search engines not to pass PR values to these external links, thereby better controlling the flow of our website's weight.urlizetruncFilter: withurlizeSimilar, it also has the function of automatically converting links. But on this basis,urlizetruncIt allows us to specify a number to truncate the text displayed for the link. If the original URL string exceeds the number we set, it will add an ellipsis at the truncation point....This makes the page display more tidy.
These filters are very flexible in use within the template, and we usually apply them to the variables that need to be processed like this:
{{ content|urlize|safe }}
{{ description|urlizetrunc:30|safe }}
Here|safeIs a feature of the Django template engine, used to inform the system that this content is safe and does not require HTML escaping to ensure that the link displays normally as a clickable HTML tag.
Explore global options: AnQiCMS backend settings analysis
With the question of 'Do we have global options?', we carefully reviewed the various settings of the AnQiCMS backend. AnQiCMS provides a rich set of configuration options, covering all aspects of the website, but justurlizeandurlizetruncIn terms of direct behavioral control, the situation is as follows:
- Global feature settings(
help-setting-system.mdThis part is mainly responsible for website name, logo, filing number, copyright information, website address, multilingual settings, and custom global parameters. Here, no direct control was found.urlizeorurlizetruncTurn on, off, or truncate the length option. - Content settings(
help-setting-content.mdThis area focuses on content processing methods, such as whether to download remote images, whether to automatically compress large images, and how to handle thumbnails. There is a very relevant option here is“Is automatic filtering of external links enabled”.- If you choose“Do not filter external links”External links contained in the document will be retained, and the system willautomatically add
rel="nofollow"Tag. - If you choose“Filter external links”The system will directly remove external links from the content.
- If you choose“Do not filter external links”External links contained in the document will be retained, and the system willautomatically add
This setting of 'Whether to automatically filter external links' does not directly controlurlizeorurlizetruncthe filter itself, but it does indeed affect all external links on the website (including those generated byurlizethe links) ofnofollowBehavior. It is worth noting that,urlizeThe filter is designed to automatically add,rel="nofollow"This means that even if "Content Settings" is selected to "Not filter external links" and external links are added at the same time,nofollow,urlizeIt will also ensure that the link it generates has this attribute. It can be said that,urlizeInrel="nofollow"It has a 'halo' on this point.
The conclusion is:At the AnQiCMS backend, there is currently not oneDirect Global OptionUnified ControlurlizeandurlizetruncThe specific behaviors such as 'whether to use', 'truncation length', etc. They are more often regarded as local tools for template rendering and need to be manually applied when used.
Why is there no direct global option?
This design is not accidental, it may hide AnQiCMS's considerations for flexibility and performance:
- Flexibility first:The different areas of the website may have different requirements for link processing.For example, the comment section of the article detail page may want all external links to be truncated for display, while the friendship link page does not require truncation.If there is a global switch, it may limit this fine-grained control.
- Context relevance: The truncation length of links, whether to automatically convert, etc., is often closely related to the specific context in which the content is located (such as the main text of an article, list description, comments section, etc.)Control at the template level, can better adapt to different scenarios.
- Performance considerationIf all text content needs to be scanned and processed globally on the server, it may have an impact on the performance of the website.This kind of processing can be delegated to the template layer, which can be triggered as needed, making it more efficient.
**Practice and Suggestions
Since there is no direct global option, we are usingurlizeandurlizetruncAt that time, the following strategies can be adopted to maintain consistency and efficiency:
- Use as needed, rather than blindly applying:According to the actual needs of the content, it should be applied only where automatic link conversion is required
urlizeorurlizetrunc. For example, they are very suitable to be used in places such as user comment sections, automatically fetched content blocks, etc. - Utilize
withLabel uniform truncation lengthIf you want certain types of pages within the website (such as all article list summaries)urlizetruncThe truncated length should be consistent, and it can be specified in the template header or a common code snippet (such aspartial/header.htmlUse it in{% with %}Define a variable to store this length, and then refer to this variable whereverurlizetruncit is used.
This way, when you need to modify the truncation length in the future, you only need to change one place.{% with link_truncate_length=30 %} {{ item.description|urlizetrunc:link_truncate_length|safe }} {% endwith %} - Understand the meaning of the background "Content Settings".: Remember what is in the background "Content Settings".