How to apply the `urlize` filter to parse URLs in the output of a custom content model field?

Calendar 👁️ 72

In Anqi CMS, the flexibility of the content model is one of its core strengths, allowing us to create various custom fields to carry specific types of content based on different business needs.When we customize some fields to store URL links, such as "external reference link" or "download address of related resources", you may find that these links are displayed as plain text on the front-end page by default and cannot be clicked to jump directly.This not only affects the user experience, but also fails to fully utilize the value of the link.

幸运的是,AnQi CMS内置了强大的模板过滤器,其中包括urlizeA filter that can automatically identify URLs or email addresses in text and convert them into clickable HTML links. This article will delve into how to cleverly apply it in the output of custom content model fieldsurlizeThe filter to parse URLs, making your website content more vivid and practical.

Understanding the Anqi CMS custom content model

The reason why Anqi CMS is favored by small and medium-sized enterprises and content operators is largely due to its highly flexible content model.It allows you to break out of the traditional 'article', 'product' framework, create a brand new content type according to your business logic, and define exclusive fields for it.

For example, you may add a single-line text field for the 'Project Website Address' model of 'Customer Case', or add a multi-line text field for the 'Report Download Link' model of 'Industry Report'.These fields are often stored as plain text URLs when entered in the background.When displaying on the front-end page, we naturally hope that these URLs can be correctly parsed, making it convenient for users to directly click and access.

urlizeThe filter: make the links dynamic.

urlizeIs a very practical filter provided by the Anqie CMS template engine. Its main function is to automatically detect URLs and email addresses in the input text and wrap them automatically.<a href="...">...</a>Make it clickable in the tag. Even better, it will automatically add properties for external links.rel="nofollow"This is a very useful default behavior for SEO optimization.

Basic usage example:

Suppose you have a variable namedexternal_linkA custom field with a value ofhttps://en.anqicms.comWhen output directly in the template, it may simply display as plain text:

<p>外部链接:{{ archive.ExternalLink }}</p>
{# 页面显示:外部链接:https://en.anqicms.com #}

To make it clickable, you just need to addurlizeFilter:

<p>外部链接:{{ archive.ExternalLink|urlize }}</p>

This is when the browser will render it:

<p>外部链接:<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a></p>

It should be noted that whenurlizeThe filter converts the URL to HTML tags, and by default, these HTML tags are automatically escaped by the template engine to prevent potential security issues (such as XSS attacks). In order for the browser to correctly parse and display these HTML links, we need tourlizeAfter the filter, add one moresafeThe filter clearly tells the template engine that these HTML codes are safe and do not need to be escaped.

<p>外部链接:{{ archive.ExternalLink|urlize|safe }}</p>

This ensures that the final link displayed on the page is normal and clickable.

Apply in custom fieldsurlize

In Anqi CMS, there are two main ways to get the value of a custom content model field: directly by accessing the field name, or byarchiveParamsiterating over the tags.

1. Directly access known fields

If you know the name of the custom field's 'call field' (for example, the name you entered when creating it in the background,external_link), and if you are sure that this field will store a URL, then you can directly access it like a built-in system field and applyurlizefilter.

Assuming your custom content model has a field namedexternal_linkThe single-line text field for storing external reference links. You can use it in the template like this:

<section class="external-resources">
    <h3>参考资源</h3>
    <p>访问我们的合作伙伴网站:
        {{- archive.ExternalLink|urlize|safe -}}
    </p>
</section>

Herearchive.ExternalLinkAssumeExternalLinkIt is your custom fieldexternal_linkThe calling method after AnQi CMS camel case naming transformation.

2. PassedarchiveParamsLoop through custom fields.

Sometimes, a content model may have multiple custom fields, or you may want to write a generic template code that can automatically handle all possible custom fields that may contain URLs. At this point,archiveParamsThe label comes into play. It allows you to iterate through all custom parameters of the current document (or specified document).

<section class="custom-fields-display">
    <h3>更多详情</h3>
    {% archiveParams params %}
        {% for item in params %}
            <p>
                <strong>{{ item.Name }}:</strong>
                {# 判断如果字段值包含常见的URL前缀,则应用urlize过滤器 #}
                {% if item.Value|contain:"http://" or item.Value|contain:"https://" or item.Value|contain:"www." or item.Value|contain:".com" %}
                    {{ item.Value|urlize|safe }}
                {% else %}
                    {{ item.Value }}
                {% endif %}
            </p>
        {% endfor %}
    {% endarchiveParams %}
</section>

In this example, we traversedparamsarray, eachitemRepresents a custom field. We check with simple conditional judgments,item.ValueIs it possible to be a URL (judging by whether it containshttp://、`https://

Related articles

Does the `urlize` filter recognize and convert URLs with non-standard ports (such as `http://example.com:8080`)?

## Deeply analyze the `urlize` filter of Anqi CMS: Can it intelligently recognize and convert URLs with non-standard ports?In Anqi CMS template development and content creation, the `urlize` filter is a very practical tool that can automatically identify URLs and email addresses in text content and convert them into clickable HTML links. It can also automatically add the `rel="nofollow"` attribute according to the configuration, which is very beneficial for SEO optimization and user experience.However, a common problem in daily operations is

2025-11-09

Does the `urlize` filter work in all AnQiCMS built-in template tags (such as `system`, `contact`)?

In AnQiCMS template development, flexibly using built-in tags and filters is the key to improving content display efficiency.Many users often encounter a question when dealing with text output: Can the `urlize` filter be directly applied to the output of all built-in template tags (such as `system` and `contact`)?

2025-11-09

How to use the `urlize` filter to reduce the workload of content editors manually adding links?

In the daily operation of websites, content editors often need to manually add hyperlinks to URLs or email addresses in articles or descriptions. This task is not only time-consuming but also prone to errors, affecting the efficiency and accuracy of content publication.Fortunately, AnQiCMS provides a very practical template filter `urlize`, which can help us automate this process and greatly reduce the workload of manually adding links.What is the `urlize` filter?

2025-11-09

Does the `urlizetrunc` filter provide an option to prioritize retaining the domain part of the URL when truncating?

In website content operation, we often need to present URL links within limited display space.To maintain the cleanliness and consistency of the page, AnQiCMS (AnQiCMS) provides various template filters to help us process these links.Among them, `urlize` and `urlizetrunc` are two very practical tools that can automatically convert URLs in text to clickable links.

2025-11-09

How does the `urlize` filter-generated link perform in terms of browser compatibility?

As an indispensable security CMS user in daily content operation, we often need to ensure that the content we publish is consistent and professional across various terminals and browsers.Among them, the handling of links is a seemingly simple but actually important detail.Today, let's delve into how the `urlize` filter generates links in Anqi CMS and how they perform in terms of browser compatibility. --- ### The "urlize" filter of Anqi CMS: Do the links you generate display perfectly in all browsers?As a content operator

2025-11-09

Can you disable the `urlize` filter from automatically adding `rel="nofollow"`?

When using Anqi CMS for content creation, the `urlize` filter is undoubtedly a very convenient feature.It can intelligently recognize URLs or email addresses in article content, automatically converting them into clickable hyperlinks, saving the麻烦 of manually adding links.However, careful friends may notice that these links automatically generated by `urlize` will default to having the `rel="nofollow"` attribute.

2025-11-09

Does the `urlize` filter apply to processing URLs in a large amount of user-generated content (UGC)?

## The `urlize` filter of AnqiCMS: An intelligent assistant for URL processing in UGC content and operational considerations In the current era where content is king, user-generated content (UGC) has become an indispensable part of the website.From comments, forum posts to user submissions, the vast amount of UGC greatly enriches the website ecosystem.However, the problem that follows is the handling of URLs in the content.

2025-11-09

How to customize the title display format of AnQiCMS article detail page to meet SEO requirements?

In website operation, the title of the article detail page (`<title>` tag) is crucial for search engine optimization (SEO).A well-designed title can not only improve the click-through rate of the article in the search results, but also help search engines better understand the content of the page.AnQiCMS provides a flexible mechanism that allows you to easily customize the title display format of the article detail page according to your own SEO strategy.This article will introduce how to adjust the title of the article detail page in AnQiCMS to meet different SEO requirements.

2025-11-09