AnQiCMSurlizeFilter: The core of automatically linking text content
In AnQiCMS template engine,urlizeThe filter is a key feature for automatically linking and converting text content. This intelligent filter can automatically identify strings in the text that match the URL format (such ashttp://example.com/www.example.com)and email addresses(for example[email protected]),and wrap it in standard HTML<a>tags, making it clickable on the front-end page.
For example, assuming there is a sentence in your article content: https://en.anqicms.com, or send an email to[email protected] Enquire.If the text is output directly, it will just be a piece of ordinary text.urlizeWhen the filter is applied, AnQiCMS will automatically convert it to:
了解更多请访问我们的官方网站:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,或发送邮件至 <a href="mailto:[email protected]" rel="nofollow">[email protected]</a> 进行咨询。
Thus, users can directly click on these links or email addresses without any additional operations. It is worth noting that, in order to comply with SEO **practices and avoid unnecessary link weight transfer,urlizeThe filter will automatically add to these automatically generated external linksrel="nofollow"properties.
Use in AnQiCMS template filesurlizeThe filter is very simple. You just need to attach it to the variable you need to process, and be sure to combine|safeFilter.|safeThe purpose of [auto] is to inform the template engine that the output content has been processed with HTML and does not require further escaping; otherwise, the generated<a>The label code may be displayed directly as text instead of an actual link.
You can use it like this for single-line variables or small content blocks:
<p>最新动态请访问:{{ archive.Link | urlize | safe }}</p>
<p>联系方式:{{ contact.Email | urlize | safe }}</p>
If you need to link a field containing a large amount of text, such as an article,Contentyou can use the link conversion for the entirefiltertag block to wrap:
{% filter urlize:true|safe %}
{{ archive.Content }}
{% endfilter %}
Here are theurlize:trueThe parameter allows retaining the original HTML entity escaping state when processing link content.|safeThis ensures that the final rendered HTML structure is correct and free of errors.
urlizetruncFilter: Optimize the display of long URLs
In some cases, very long URL addresses may appear in the article content, and these long URLs may take up too much space when displayed completely on the page, even affecting the overall layout and aesthetics of the page. In order to solve this problem, AnQiCMS providesurlizetruncFilter. This filter functions asurlizesimilar, it can also automatically recognize and convert URLs and email addresses into clickable links, but on this basis, it allows you to specify a character length limit. When the text length of the converted URL exceeds this limit, the extra part will be automatically omitted by an ellipsis (...)Replaced to make the page display more tidy.
For example, if you have a very long URL and you want it to display no more than 30 characters:
{% filter urlizetrunc:30|safe %}
<p>以下是一个截断显示的长链接:https://en.anqicms.com/documentation/how-to-automatically-convert-urls-and-emails-to-clickable-links-in-anqicms.html</p>
{% endfilter %}
Afterurlizetrunc:30Processed, the overly long link in this text will be presented in a more concise form to the user, while maintaining its clickable nature,rel="nofollow"Properties. This is particularly useful in scenarios such as news summaries, product parameter lists, etc., which can effectively improve the reading experience of the content.
Actual application scenarios and content operation value
In the daily operation of AnQiCMS,urlizeandurlizetruncThe filter has wide application value. You can use them on detail pages of various content types, such asarchiveDetailarticles or product content obtained through tagsContentfields, throughpageDetailLabel to obtain the single-page content ofContentfield, even category descriptions (categoryDetailofDescriptionEnglish. Any text that needs to be dynamically output and includes a URL or email address can be handled by these filters.
They ensure that users of the website can access external resources, download files, or send emails while browsing your content instantly and conveniently, without the need to manually copy and paste. This not only significantly improves user experience but also simplifies the process of content editing and publishing, reducing the manual addition of HTML.<a>Label may introduce syntax errors.Through the automated link conversion mechanism, AnQiCMS empowers website operators to focus on high-quality content creation, while ensuring the performance of website content in terms of interactivity, readability, and SEO friendliness.
Common Questions (FAQ)
Q1: I used it in the AnQiCMS templateurlizeorurlizetruncBut why is the URL and email address still displayed as plain text on the page, and cannot be clicked?A1: The most common reason is that you may have forgotten to add at the end of the filter chain|safeFilter.AnQiCMS's template engine, for security reasons, defaults to escaping all content output through variables as HTML entities to prevent potential cross-site scripting (XSS) attacks.|safeThe filter is a clear declaration that tells the template engine that the content you output is safe HTML code, which can be rendered as-is, thus makingurlizeorurlizetruncgenerated<a>Labels can be parsed and displayed as clickable links by the browser. Please check your code to ensure its format is similar.{{ your_content_variable | urlize | safe }}.
Q2:urlizeFilters will automatically add to all external links converted.rel="nofollow"What is the property? Can I customize or remove this property?A2: Yes