How to automatically parse a URL string into a clickable HTML link in AnQiCMS template?

Calendar 👁️ 64

In AnQiCMS template development, we often encounter such needs: content containing URL strings should be automatically converted into clickable HTML links to enhance user experience and page interactivity.Manually adding links is cumbersome and unrealistic, fortunately, AnQiCMS provides powerful template filters that can help us easily achieve this function.

The AnQiCMS template engine supports Django-like syntax, which includes a series of practical filters that can process variable content. For the automatic parsing of URL strings,urlizeandurlizetruncThese filters are our powerful assistants.

Let the URL automatically come to life:urlizeThe wonder of filters

urlizeThe filter is specifically used to identify URLs and email addresses in text and automatically wrap them in<a href="...">...</a>tags. It can intelligently recognize multiple URL formats, such as those starting withhttp://orhttps://A complete link starting withwww.A URL starting with, even just a domain name string, can be accurately captured and converted.

UseurlizeThe filter is very simple, just add it after the variable you need to process|urlizeYou can. For example, if the content of your article is stored inarchive.ContentIn the variable, if you want all the URLs to be clickable links, you can write it like this:

{{ archive.Content|urlize|safe }}

Pay special attention here|safethe use. In AnQiCMS (and many template engines), to prevent cross-site scripting attacks (XSS), the default is to escape the output HTML content, by<Translate special characters to&lt;. BecauseurlizeThe filter generates HTML tags, if it is missing|safeThese tags themselves will be escaped, causing the original HTML code to be displayed on the page instead of the actual link. Therefore, when you know that the output content is safe HTML, be sure to add|safeIndicate the template engine to parse as HTML.

urlizeThe filter will also automatically add to external links when generating links.rel="nofollow"Property. This is a beneficial practice for SEO, which can prevent unnecessarily passing the weight of this site to external sites and at the same time indicate to search engines that these links are not recommended or guaranteed by this site.

Let us feel through some examples.urlizeThe power of:

{# 假设变量text_content中包含以下内容: #}
{# "欢迎访问AnQiCMS官网:https://en.anqicms.com,或搜索www.anqicms.com,有问题请发邮件到[email protected]" #}

<p>{{ text_content|urlize|safe }}</p>
{# 输出可能类似: #}
{# <p>欢迎访问AnQiCMS官网:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,或搜索<a href="http://www.anqicms.com" rel="nofollow">www.anqicms.com</a>,有问题请发邮件到<a href="mailto:[email protected]">[email protected]</a></p> #}

Optimize the display of long links:urlizetruncAdvanced applications of the filter.

Sometimes, a very long URL may appear on a page, which not only occupies a lot of space but may also destroy the page layout and affect the aesthetics. At this time,urlizetruncthe filter comes into play. It is inurlizeBased on that, a feature to truncate link text has been added, allowing long URLs to be displayed in a more concise form while still retaining full clickability.

urlizetruncThe filter accepts a numeric parameter to specify the maximum display length of the link text. The part exceeding this length will be replaced with an ellipsis ("...")....)

For example, if you want the link text to display a maximum of 30 characters:

{{ archive.Content|urlizetrunc:30|safe }}

urlizetruncIn practical applications, it is particularly suitable for handling user submitted comments, messages, or some custom fields that may contain long links, ensuring that the content is both readable and can be normally accessed.

{# 假设变量comment_text中包含以下内容: #}
{# "这是一条用户评论,包含一个非常长的链接:https://www.example.com/very/long/path/to/specific/page?param1=value1&param2=value2" #}

<p>{{ comment_text|urlizetrunc:50|safe }}</p>
{# 输出可能类似: #}
{# <p>这是一条用户评论,包含一个非常长的链接:<a href="https://www.example.com/very/long/path/to/specific/page?param1=value1&param2=value2" rel="nofollow">https://www.example.com/very/long/path/to/...</a></p> #}

Application scenarios and **practice

These filters are most commonly used in the following scenarios:

  1. Content of articles/products detail pages: Displayarchive.ContentMake sure all embedded URLs are clickable.
  2. User comments/messagesThe user may paste URLs when submitting comments or messages. The filter can automatically convert them to links while avoiding unnecessary layout issues.
  3. Custom text fieldIf your content model contains custom text fields, such as 'Reference Link' or 'Source Address', these filters can also be used to process them.

Tip:

  • Always remember to useurlizeorurlizetruncadded afterwards|safeunless you explicitly need HTML to be escaped.
  • Choose whether to truncate the link based on the page design and content type.It may be better not to truncate important reference links in the main content;And truncating for user comments or sidebar lists can keep the page tidy.
  • In the AnQiCMS backend, you can set custom templates for different content models (such as articles, products) or single pages. In these template files, make full use ofurlizeandurlizetruncIt will greatly enhance the professionalism and user-friendliness of content presentation.

MasteredurlizeandurlizetruncThese powerful filters will enable your AnQiCMS website to handle URL strings more intelligently and elegantly, providing visitors with a more smooth and convenient browsing experience.


Frequently Asked Questions (FAQ)

Q1: Why did I use in the templateurlizeFilter, but the URL on the page is still plain text and not clickable?A1: The most common reason is that you may have forgotten tourlizeAdd after the filter|safeFilter. The AnQiCMS template engine, for security considerations, defaults to escaping all output HTML content.urlizeIt will generate HTML.<a>tags, if missing|safeThese tags themselves are also escaped into character entities, causing the browser to not recognize them as links. Make sure your code is similar to{{ your_variable|urlize|safe }}.

Q2: UseurlizeWhy does the filter automatically add the link, whyrel="nofollow"Property? What is the role of this property?A2:urlizeThe filter does indeed automatically add to external links.rel="nofollow"Property, this is a practice friendly to search engines. It tells the search engine spiders that the link should not be considered as a 'trust vote' for the target page, i.e., it does not pass 'link weight'.This helps:

*   **控制SEO权重流失**:避免将本站的SEO权重无意中传递给大量外部站点。
*   **防范垃圾链接**:在处理用户生成内容(如评论、论坛帖子)时,减少垃圾链接对本站SEO的负面影响。
*   **保持内容独立性**:声明本站对所链接内容不作担保或推荐。

Q3: Besides the content of the article or product details page, where else can I use the AnQiCMS template?urlizeorurlizetruncFilter?A3: These filters are very flexible and can be applied to any variable in the output text. Common application scenarios include:

*   **网站留言板**:自动转换用户留下的联系方式或网站链接。
*   **用户评论区**:确保用户在评论中粘贴的链接能正常点击。
*   **自定义内容模型字段**:如果您的自定义内容模型中有用于存储网址或社交媒体链接的文本字段。
*   **侧边栏的公告/友情链接描述**:在后台输入纯文本,前端自动转换。
总之,任何你希望文本中的URL自动变成链接的地方,都可以尝试使用它们。

Related articles

How to automatically wrap long text to a specified length in AnQiCMS template?

In the presentation of website content, the way long texts are displayed is often a key factor affecting user experience and the beauty of the page.When we need to display a long text content, such as an article summary, product description, or detailed introduction, in a more readable and layout-adaptive way, AnQiCMS's template function provides a powerful and flexible solution.AnQiCMS uses syntax similar to Django template engine, with various built-in filters (Filters) that can help us easily format text.

2025-11-08

How to safely output HTML code in the template without escaping in AnQiCMS?

When using AnQiCMS for website template development or content display, you may encounter situations where you need to output HTML code directly on the page, only to find that the code is automatically escaped, for example, the `<p>` tag becomes `&lt;This often confuses those who are new to it.In fact, this is the default measure taken by the AnQiCMS template engine for website security.### Understanding the default security mechanism of AnQiCMS templates The AnQiCMS template engine design has drawn inspiration from

2025-11-08

How to truncate a string and add an ellipsis (...) to limit the display length in AnQiCMS template?

In AnQiCMS template development, we often need to refine the content displayed on the website, such as article titles, summaries, or abstracts of a text.If the content is too long, it not only affects the beauty of the page, but may also reduce the user's reading experience.AnQiCMS's powerful template engine provides a simple and efficient method to solve this problem, allowing us to easily extract strings and automatically add ellipses (...) to elegantly limit the display length of content.

2025-11-08

How to format a timestamp (timestamp) into a readable date format for display in AnQiCMS?

In the process of website operation, the effective display time of content publishing or updating is crucial for improving user experience and the readability of content.If time information is presented in the original timestamp format, it is often difficult for ordinary users to understand.AnQiCMS provides a straightforward and flexible way to help you convert these digital timestamps into easily readable date and time formats.### How AnQiCMS handles timestamps AnQiCMS typically uses standard Unix Timestamp format for storing content time information

2025-11-08

How to define and use temporary variables in AnQiCMS templates to simplify content display?

When developing templates in AnQiCMS, we often encounter situations where we need to process some complex data or reuse a certain calculation result.To make the template code more concise, readable, and maintainable, AnQiCMS provides a powerful mechanism for defining and using temporary variables, mainly through the `with` and `set` tags.Reasonably utilizing them can greatly enhance our content display efficiency and flexibility. ### Simplified Content Display: Why do we need temporary variables?

2025-11-08

How to implement the link display for multi-language site switching in AnQiCMS?

In today's globalized digital world, multilingual support for websites has become crucial for reaching a wider audience and expanding market boundaries.AnQiCMS (AnQiCMS) fully understands this need, therefore, it takes multilingual support as one of its core features, aiming to help users efficiently build and manage multilingual websites.This article will focus on how to implement the display of language switch links for multi-language sites in AnQiCMS, supplemented by necessary SEO practices, and provide you with a comprehensive guide.

2025-11-08

How to add `hreflang` tags in AnQiCMS template for multilingual pages to optimize SEO?

In today's globalized internet environment, many websites need to provide content for audiences of different languages or regions.To ensure that these multilingual pages can be correctly understood and displayed by search engines, the `hreflang` tag plays a crucial role.It can tell search engines that your website has multiple language versions and which version should be displayed to which user.

2025-11-08

How does AnQiCMS call and display data for specific sites based on different site IDs?

AnQiCMS multi-site data call: easily achieve content sharing and linkage display AnQiCMS is a content management system designed specifically for small and medium-sized enterprises and content operation teams, and its multi-site management function is one of its core highlights.In actual operation, we often encounter such needs: the main station needs to display the latest products of various sub-sites, or all sub-sites need to uniformly display the contact information of the company's headquarters, or multiple content platforms hope to aggregate and display articles on a specific theme.In this case, how to efficiently call and display data from a specific site is particularly important

2025-11-08