In content management and website operation, we often need to convert URLs in text into clickable hyperlinks so that users can directly jump to access.Manual operation is not only inefficient, but also prone to omissions, especially on large-scale websites.AnQi CMS fully understands this pain point and provides an elegant and efficient solution that allows us to easily identify and convert URLs in text.
Make AnQiCMS automatically identify URLs in text and convert them into clickable links<a>The tag, we mainly use its powerful template filter function. Specifically,urlizeandurlizetruncThese filters are the reliable assistants for completing this task. They can intelligently scan text during content output, automatically identify URLs and email addresses, and wrap them in HTML.<a>in the tag.
Core Tool:urlizeandurlizetruncFilter
Imagine you have a piece of content that includes something like "Please visit"https://en.anqicms.comFor more details, or send an email to[email protected]Seek help. Such text. If displayed directly, these URLs and email addresses will just be plain text. But through AnQi CMS, they will automatically become:urlizeFiltered, they will automatically become:
请访问 <a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a> 了解更多详情,或者发送邮件到 <a href="mailto:[email protected]">[email protected]</a> 寻求帮助。
Is it not very convenient? And,urlizeThe filter will also automatically add external links for you when converting links,rel="nofollow"Property, this is a good default setting for the website's SEO optimization, which can effectively control the flow of page weight.
UseurlizeThe filter is very simple, it is usually applied to the text content variable you want to process.For example, on the article detail page, we usually need to handle the main content of the article.Assuming the article content is stored inarchive.ContentIn this variable, you can call it like this in the template:
{{ archive.Content|urlize|safe }}
It needs to be emphasized here|safeThis filter. The Anqi CMS template engine, for website security, defaults to escaping all output content to prevent cross-site scripting attacks (XSS).This means if your content includes HTML code (likeurlizeGenerated<a>Tags), they are displayed as plain text.|safeThe purpose is to tell the template engine: "This content is safe, do not escape, parse and display it directly as HTML code." Therefore, remember to add it when usingurlizeorurlizetrunc.|safe.
Sometimes, a URL may be very long and it may not look good displayed directly on the page, affecting the layout. At this point,urlizetruncThe filter comes into play.urlizetruncfunction withurlizeSimilar, but it allows you to specify the maximum display length of the link text.This part, if it exceeds the length, will be automatically replaced with “…”, while maintaining the integrity of the link.For example, if you want the link text to display a maximum of 15 characters:
{{ archive.Content|urlizetrunc:15|safe }}
This way, even if the original link is very long, only the truncated text will be displayed on the page, for examplehttps://www.anqi...and clicking on it will still allow you to jump to the full link.
Application scenarios and operation points
These two filters are most commonly used in the following scenarios:
- Article detail page: Convert the article text (
archive.Content) to clickable links for all URLs and email addresses. - Product Description Page: Handle product details description (if it includes website or contact email).
- Custom text field: If your content model defines other text fields that may contain URLs, you can also handle them in the same way.
In practice, you just need to find the text variables that need to be automatically converted to URLs, and then add|urlize|safe(or|urlizetrunc:长度|safe)when outputting the variable in the template. Usually, this code is placeddetail.htmlorlist.htmlThis is the corresponding content area of this template file.
It is noteworthy that Anqi CMS has a feature that allows you to select whether to automatically filter external links in the "content settings" panel. This feature is related tourlizeThe filter is different, it is designed to handle external links at the time of content publishing (such as removing or addingrel="nofollow"),whileurlizeThe content is dynamically converted when displayed on the front-end page. Both can be used in coordination with your operational strategy.
By simply applying in the template.urlizeorurlizetruncFilter, the Anqi CMS can greatly enhance the readability and interactivity of content, while freeing content operators from tedious manual link work, making website content management more efficient and intelligent.
Frequently Asked Questions (FAQ)
1.urlizeDoes the filter automatically recognize all types of links?
urlizeThe filter is mainly used to identify common URL formats such ashttp:///https:///www.domain names starting with, as well as domain names without a protocol, such asanqicms.com)and standard email address format. It can cover the vast majority of link types you want to automatically convert and will automatically addrel="nofollow"Property.
2. I can convert the following<a>Add custom attributes to the tag, for exampletarget="_blank"Should the link open in a new window?CurrentlyurlizeandurlizetruncThe filter mainly focuses on converting text into clickable links and adding them automaticallyrel="nofollow"attributes, but they themselves do not directly provide custom other HTML attributes such astarget="_blank"The option. If you need more fine-grained control, you can consider combining it with frontend JavaScript code or extending the functionality on the backend in Go language, but this will involve more in-depth development work.
3. Besides the main article content, where else is it suitable to useurlizeFilter?Any text field that may contain URLs or email addresses is suitable for use, except for the main content of the article.urlizeFilter, such as product details, the "About Us" page content on the website, the comment area (if external links are allowed to be displayed), and even some custom content model fields.Any text that you hope to be automatically recognized and converted into clickable links can be applied to this filter.