In website content operations, we often need to include some website URLs or email addresses in articles or descriptions.In traditional ways, this information is often just plain text, and users need to manually copy and paste to access it, which undoubtedly increases the user's operational cost and affects the interactivity of the content.luckyly, AnQiCMS cleverly goes through its powerful template filter mechanism, providing us with an elegant solution - that isurlizefilter.
urlizeFilter: Makes text automatically 'come to life'
In simple terms,urlizeIt is a template filter built into AnQiCMS, whose core function is to intelligently scan text content, automatically identify the URLs (URL) and email addresses within it, and convert them into clickable HTML<a>Link tags. This means that when you edit content in the background, even if it's just a simple inputhttps://en.anqicms.comor[email protected], they will magically turn into clickable links that can be accessed directly on the front page.
This feature greatly enhances the user experience. Imagine when visitors browse your website content, whether they see a reference link or a contact email, they can directly click to jump without interrupting their reading to manually copy.This smooth interaction will undoubtedly increase the user's stay time on your website and encourage them to further explore or interact.
Behind the scenes consideration: SEO and link management
AnQiCMS is in implementationurlizeAt the same time, it also fully considers the needs of content management and search engine optimization. An interesting detail is,urlizeThe filter will automatically add to these links when generating linksrel="nofollow"Property.
rel="nofollow"It is an important SEO identifier that tells search engines not to follow or pass weight to this link.This is very useful for website administrators, especially when your content may include user comments, forum posts, or links to non-core partner websites.Added automaticallynofollowYou can effectively control the flow of 'link weight', avoiding negative impacts on the SEO of your own website due to too many external links or pointing to low-quality websites, while also reducing the malicious links contained in spam comments to some extent.
How to easily use in the templateurlize
Apply in AnQiCMS templateurlizeThe filter is very simple and intuitive. Since AnQiCMS uses a template engine syntax similar to Django, you can use it like other filters, tourlizeApply to any text variable that contains a URL or email address.
The most common usage is:
{{ content_variable|urlize|safe }}
Herecontent_variableThis can be your article details, description, or any other text field. It is especially important to emphasize that,|safeThe filter is indispensable here. Because,urlizeThe filter generates the actual HTML<a>Labels, while the AnQiCMS template, for safety reasons, defaults to escaping all output content (that is,<to<etc.). If missing|safeThe content will be displayed directly on the page<a>The original text of the label, not a clickable link.|safeThe filter tells the template engine that this part of the content is processed and safe HTML, which can be rendered directly.
If you need to process a larger block of texturlizeProcess, rather than a single variable, can also be usedfilterTags:
{% filter urlize|safe %}
这里是您的长文本内容,其中包含网址如 https://example.com 和邮箱 [email protected]。
您也可以访问 www.anotherlink.net 了解更多信息。
{% endfilter %}
Sometimes, you may want to keep the original HTML entity of the displayed text in the link (for example&still displayed as&). At this point,urlizeThe filter also supports a boolean parameter:
{# 如果您希望链接文本内容进行HTML转义,可以使用 true (默认行为) #}
{{ my_text|urlize:true|safe }}
{# 如果您不希望链接文本内容进行HTML转义,可以使用 false #}
{{ my_text|urlize:false|safe }}
In most cases, unless there is a special requirement, use it directly{{ content_variable|urlize|safe }}It can meet most scenarios.
urlizetruncTo slim down the long link:
In some cases, the URL address may be very long, and displaying it directly on the page may破坏 the aesthetics of the layout. AnQiCMS also took this into consideration and providedurlizetruncFilter asurlizean advanced version.
urlizetruncHow to use the filter withurlizeSimilar, but it allows you to specify a numeric parameter to control the maximum length of the displayed link text. If the converted link text exceeds this length, the extra part will be truncated and an ellipsis (...)
{# 链接显示文本最多显示25个字符,超出部分用...代替 #}
{{ long_url_text|urlizetrunc:25|safe }}
This is very useful for displaying links in limited space (such as sidebars, list summaries), it keeps the page neat without losing the accessibility of the links.
Actual application example
Assuming your article contentarticle.ContentAs follows:
欢迎访问安企CMS官网:https://en.anqicms.com,这是一个非常棒的CMS。
如果您有任何问题,可以发送邮件到 [email protected] 或者访问我们的GitHub仓库:github.com/fesiong/goblog。
我们还有一个演示网站:www.kandaoni.com/demo,欢迎体验!
Using the templateurlizeFilter:
<div class="article-body">
{% filter urlize|safe %}
{{ article.Content }}
{% endfilter %}
</div>
The final HTML rendering effect of the page will be:
<div class="article-body">
欢迎访问安企CMS官网:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,这是一个非常棒的CMS。
如果您有任何问题,可以发送邮件到 <a href="mailto:[email protected]">[email protected]</a> 或者访问我们的GitHub仓库:<a href="http://github.com/fesiong/goblog" rel="nofollow">github.com/fesiong/goblog</a>。
我们还有一个演示网站:<a href="http://www.kandaoni.com/demo" rel="nofollow">www.kandaoni.com/demo</a>,欢迎体验!
</div>
You can see that all URLs and email addresses are automatically converted into clickable links and automatically added attributes. If appliedrel="nofollow"properties. If appliedurlizetrunc:20, then the display text of longer links will be shortened.
ByurlizeandurlizetruncThese powerful and convenient filters greatly simplify the processing of links in the content of AnQiCMS, not only improving the readability and user experience of the content, but also providing strong support for the SEO strategy of the website.In daily content operations, fully utilizing these tools will make your work twice as efficient.
Frequently Asked Questions (FAQ)
- Q:
urlizeThe filter will automatically add to all linksrel="nofollow"Should I add the attribute?A:Yes, by default,urlizeThe filter will automatically recognize and convert URLs and email addresses in the text into clickable links, and automatically add to these linksrel="nofollow"Property. This is an important SEO practice that helps manage the external link weight of the site and prevent spam comments and other issues.