In website content operation, we often encounter such situations: articles, summaries, or comments contain some URL strings that can be seen by users but cannot be directly clicked to jump, which not only affects user experience but also reduces the readability and practicality of the content.As an AnQiCMS user, you will find that the system provides us with a set of elegant and efficient solutions that can easily convert these ordinary URL strings into clickable links.
The template engine of AnQiCMS is powerful and flexible, it adopts the concise syntax of Django templates and built-in many practical filters (filters).Today, let's talk about how to use these filters to make the URL string in your template come to life.
urlizeFilter: Automatically generate links
AnQiCMS provides us with a namedurlizeThe powerful filter, which intelligently identifies URLs in text (such as those starting withhttp:///https:///www.), even common email addresses), and automatically wraps them up with<a href="...">Label it to make it clickable.
This filter not only converts links to clickable format, but also in the generated<a>label automatically.rel="nofollow"Property.This is very important for the SEO strategy of the website, it can help you control the flow of the website's 'weight' and avoid unnecessarily passing weight to external links, thereby focusing better on the optimization of your own content.
Let's take a simple example, suppose your article content includes a website address and email:
{# 假设原始文本中包含URL和邮箱地址 #}
{% set content_string = "欢迎访问安企CMS官网:https://en.anqicms.com,您也可以发送邮件到 [email protected] 联系我们。" %}
<p>原始文本:</p>
<p>{{ content_string }}</p>
<p>应用urlize过滤器后:</p>
<p>{{ content_string|urlize|safe }}</p>
In the above code,{{ content_string }}When output directly, both the URL and email are plain text. But when you apply the|urlizefilterer after,https://en.anqicms.comand[email protected]they will be automatically recognized and converted into clickable links, for example:
<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>
<a href="mailto:[email protected]">[email protected]</a>
urlizetruncFilter: Graceful truncation of long URLs
Sometimes, some URLs may be very long, and displaying them completely may affect the beauty and layout of the page, even causing layout confusion. At this time,urlizetruncThe filter comes into play. IturlizeBased on this, the function of truncating link text has been added, you can specify a length, and the link text that exceeds this length will be truncated and...End, while maintaining the validity of the link.
urlizetruncThe filter requires a parameter to specify the truncation length. For example, if you want the link text to display a maximum of 30 characters, you can use it like this:
{# 假设有一个非常长的URL #}
{% set long_url_content = "这里有一个非常长的链接,如果您感兴趣可以点击查看:https://en.anqicms.com/category/1/this-is-a-very-long-article-title-with-many-words-and-some-special-characters-to-demonstrate-truncation" %}
<p>原始长链接:</p>
<p>{{ long_url_content }}</p>
<p>应用urlizetrunc过滤器并截断为30个字符后:</p>
<p>{{ long_url_content|urlizetrunc:30|safe }}</p>
After|urlizetrunc:30The content displayed in the browser after processing may look like this:
这里有一个非常长的链接,如果您感兴趣可以点击查看:<a href="https://en.anqicms.com/category/1/this-is-a-very-long-article-title-with-many-words-and-some-special-characters-to-demonstrate-truncation" rel="nofollow">https://en.anqicms.com/c...</a>
The link is still complete, but the text displayed on the page has been cleverly shortened, saving space and improving visual neatness.
About|safeImportant tips for the filter
In the above example, you may have noticed that I haveurlizeandurlizetruncadded after that|safeFilter.This is an important security mechanism of the AnQiCMS template engine.<will become<,>will become>etc.
urlizeandurlizetruncThe filter generates HTML formatted<a>Label. If missing|safeFilter, these<a>Label will be escaped to plain text, thus not clickable links for the browser.
Use|safeA filter is telling the template engine: "This content is safe, please do not escape, and output it as HTML directly." However, this also means that you need to ensure that the|safeThe content source is trusted, otherwise it may pose a security risk. It is usually safe when handling AnQiCMS system content or strictly reviewed user content.
Useful Scenarios and Additional Considerations
These filters are very useful in various content operation scenarios:
- User comments and messages:Users can automatically turn the URLs or email addresses pasted in comments into clickable links, which is convenient for interaction.
- Product or service description:If your product or service description needs to guide users to access an external resource or download link.
- Custom field content:The URL is stored in some custom fields, and it is expected to automatically become a link when displayed on the front end.
- Article summary or list:When displaying URLs in article lists or summaries, they can be brief and functional.
It should be noted that if your content is itself written using a Markdown editor, and Markdown rendering is already configured on the front end, then Markdown syntax (such as[文本](链接)Or paste directlyhttp://Links) are usually automatically converted to clickable links, in which case you do not need to use anything extraurlizeorurlizetruncThe filter. These filters are mainly used to process URL strings that exist in plain text form.
By making flexible use ofurlizeandurlizetruncFilter, you can significantly enhance the interactivity and user experience of your website content, and also perform better in SEO.With the help of AnQiCMS, make your website content more attractive and easier to operate.
Common Questions (FAQ)
1. Why did I useurlizeFilter, but the link is still plain text and cannot be clicked?This is likely because you forgot tourlizeadd after the filter|safeFilter. The AnQiCMS template engine defaults to escaping all HTML tags to ensure safety.urlizeandurlizetruncThe one generated is HTML.<a>tags, if not using|safeTell the template engine that this HTML is safe to render directly, it will be escaped and displayed as plain text.
2.urlizeWhat types of URLs and email addresses will the filter automatically recognize?
urlizeThe filter can usually identify URLs that start withhttp:///https:///ftp:///ftps://, as well as URLs that start with but do not have a protocol part. For email addresses, it can identify standard email address formats (such aswww..[email protected])and convert it tomailto:Link.
3. My article content is published using Markdown editor, do I need to useurlizea filter?It is usually not necessary. If your AnQiCMS backend has enabled the Markdown editor and the front-end is also configured with the corresponding Markdown rendering (for example,archiveDetailLabel used inrender=trueparameters), then the Markdown syntax itself supports converting links and email addresses to clickable forms (such as[AnQiCMS官网](https://en.anqicms.com)or directly pastedhttps://en.anqicms.com}]urlizeandurlizetruncThe filter is mainly used to process those URL strings stored in the database in plain text format without Markdown parsing.