In content management and website operations, automatically converting URLs and email addresses in the text to clickable hyperlinks can greatly enhance the user experience and optimize the internal link structure of the website to a certain extent.For AnQiCMS users, achieving this function is not through simple backend settings, but through the specific "filter" provided by its powerful template engine.This design approach gives the website owner great flexibility and control, allowing for fine-grained management of the link generation method according to the needs of different content areas.
Understanding AnQiCMS's automatic link mechanism
One of the core advantages of AnQiCMS is its high-performance template engine, which draws on the syntax of Django templates.Under this structure, the feature of automatically adding hyperlinks to URLs and email addresses is not executed immediately when you save the article content.In contrast, it is dynamically completed through the application of specific template filters when the content is 'rendered' (i.e., displayed) on the frontend page.
This means that when you edit articles in the background, even if you directly inputwww.anqicms.comor[email protected]Such plain text, AnQiCMS will not store it in the database as<a>Label.The benefits of doing so are maintaining the purity of the content, and delegating the rendering logic of the links to the front-end template, thus allowing you to apply different link handling rules to different pages or content blocks, for example, some places need links, while others do not.
Core Function:urlizeFilter
To automatically hyperlink URLs and email addresses on the frontend page, AnQiCMS provides a function namedurlizeThe built-in filter. This filter is specifically used to identify URLs (including those without protocols) and email addresses in text and wrap them automatically.http://orhttps://URLs likewww.example.comandexample.com) and email addresses, and wrap them automatically.<a>Label in. To comply with SEO**practices,urlizeThe filter will also automatically add these generated links.rel="nofollow"Properties, this helps inform search engines that these links do not pass weight, avoiding unnecessary SEO risks.
For example, if you are in the article content field (usually,archive.Content)中包含了以下文本:
欢迎访问 AnQiCMS 官方网站:https://en.anqicms.com,
或者通过邮箱联系我们:[email protected],
我们还提供文档:www.kandaoni.com。
在您的模板文件中,如果直接输出{{ archive.Content }},这些网址和邮箱仍然会是纯文本。但如果对内容应用urlize过滤器,像这样:
{{ archive.Content|urlize|safe }}
Then when the page loads, this content will be rendered as:
欢迎访问 AnQiCMS 官方网站:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,
或者通过邮箱联系我们:<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>,
我们还提供文档:<a href="http://www.kandaoni.com" rel="nofollow">www.kandaoni.com</a>。
Please note that an additional feature is used here:|safethe filter. This is becauseurlizeThe filter will convert plain text to text containing HTML tags (<a>)的文本,而AnQiCMS的模板引擎为了安全起见,默认会对所有输出内容进行HTML实体转义,以防止XSS攻击。如果没有|safeEnglish, Your link may be displayed as plain text.<a href="...Therefore, when you confirmurlizewhen the content generated by the filter is safe and needs to display HTML tags, be sure to cooperate with|safefilters.
urlizeThe filter also supports an optional parameter to control whether the link text is escaped. By default, the link text is kept as is. If it is set totrueIf set, special characters in the link text will be escaped; if not set,false(or not set), it will not perform additional escaping on the link text. Usually when used in article content, you would want to retain the original text display, so it is usually not necessary to set this parameter or set it tofalse. For example:
{# 链接文本不会被转义,显示为原始URL或邮箱 #}
{{ archive.Content|urlize:false|safe }}
Advanced Application:urlizetruncFilter
For scenarios that may contain very long URLs, AnQiCMS also providesurlizetruncfilter. Its function is withurlizeSimilar, but an additional feature is added: you can specify a numeric parameter to truncate the text of the hyperlink display. When the length of the URL exceeds this specified value, the excess part will be...Replace it, thus maintaining the neat page layout.
For example, if you have a very long URL:https://en.anqicms.com/this-is-a-very-very-long-article-title-that-results-in-a-very-long-url-example.htmlIf you want to limit the display length to 20 characters or less, you can use it like thisurlizetrunc:
{{ archive.Content|urlizetrunc:20|safe }}
This code may render the above long URL as:
<a href="https://en.anqicms.com/this-is-a-very-very-long-article-title-that-results-in-a-very-long-url-example.html" rel="nofollow">https://www.anqic...</a>
This is especially useful in limited space areas such as news lists, article summaries, or comment sections, where it can provide link functionality while avoiding破坏页面的视觉平衡.
How to effectively use in content
In order to effectively utilize these automatic hyperlink features on your AnQiCMS website, you need to edit the template files of the website. Usually, the article detail page (such asarchive/detail.html)and single page detail page(such aspage/detail.html)of the content output is the location where these filters are applied**.
Find the tags showing the content of the article or page in your template(such as{{ archive.Content|safe }}or{{ page.Content|safe }}),then modify it to:
{# 为文章内容自动添加超链接 #}
{% archiveDetail articleContent with name="Content" %}
{{ articleContent|urlize|safe }}
{% endarchiveDetail %}
{# 或者为单页内容自动添加超链接 #}
{% pageDetail pageContent with name="Content" %}
{{ pageContent|urlize|safe }}
{% endpageDetail %}
If you want to truncate the link text:
{# 为文章内容自动添加超链接并截断链接文本至40个字符 #}
{% archiveDetail articleContent with name="Content" %}
{{ articleContent|urlizetrunc:40|safe }}
{% endarchiveDetail %}
This way, all articles and page content on your website, as long as they contain recognizable URLs or email addresses, will be automatically converted into clickable hyperlinks on the frontend, greatly enhancing the user experience and the accessibility of information.
Precautions
- Template-level changesPlease remember, these are template-level operations.If you are not familiar with AnQiCMS template file structure or GoLang template engine, it is recommended to seek professional help or carefully read the AnQiCMS template development documentation to avoid unnecessary errors.
|safeFilters are crucialWhen outputting content that may contain HTML tags (such as byurlizeorurlizetruncgenerated<a>tags), be sure to use|safeFilter, to ensure that HTML code is parsed correctly by the browser and not displayed as plain text.- Flexibility and controlThis dynamic rendering method allows you to precisely control which content areas need to be automatically linked, as well as the display style of the links, thus better serving your website's operational strategy.
By reasonably utilizing the AnQiCMS providedurlizeandurlizetruncThe filter allows you to make your website content more interactive, while maintaining SEO-friendliness and providing users with a smoother browsing experience.
Common Questions (FAQ)
1. Why does the URL I enter in the background article editing become an automatic hyperlink on the front end? Where is this set?AnQiCMS does not set a global 'auto hyperlink' switch in the background.This feature is implemented through a specific "filter" in the front-end template file.urlizeorurlizetruncFilter that dynamically recognizes and converts URLs and email addresses in text to clickable hyperlinks.
2. Why did the URL display after following the tutorial?<a href="...">Is this plain text, rather than a clickable link?This is usually because you applied in the templateurlizeorurlizetruncAfter the filter, and forgot to add it after|safeFilter. AnQiCMS template engine defaults to escaping all output HTML tags to prevent potential security risks (such as XSS attacks).|safeThe filter informs the template engine that this content is safe and does not require escaping, allowing the browser to correctly parse and display it as a clickable hyperlink.
3. Will automatically added hyperlinks affect the website's SEO?AnQiCMSurlizeandurlizetruncThe filter will automatically add links to the links generatedrel="nofollow"Property.This attribute tells the search engine that these links should not pass 'link juice', that is, they should not be counted in the search engine's ranking weight.This is often considered good SEO practice, especially when generating links automatically or when the link targets are not the core content of the website, which can avoid unnecessary SEO risks and maintain the overall health of the website's link structure.