How to automatically convert a URL string into a clickable hyperlink and set `nofollow` in the template of AnQiCMS?

Calendar 👁️ 61

In the daily operation of Anqi CMS, we often need to display external links in the website content.These links, if they are just simple text, not only affect the user experience, but also cannot effectively convey information.At the same time, in order to follow the **practice** of Search Engine Optimization (SEO), especially for links pointing to external sites, we usually want to addrel="nofollow"Attribute, to avoid unnecessary 'weight loss' or passing unnecessary trust. Anqi CMS provides a concise and efficient method to solve this problem.

The core feature revelation:urlizeFilter

The AnQi CMS template engine provides powerful filter functions, whereurlizeThe filter is the core tool to solve the "automatically convert URL strings into clickable hyperlinks" requirement. Its function is to intelligently identify URL strings within the text content, whether they arehttp:///https://starting with orwww.URLs starting with, even email addresses, will be automatically wrapped in clickable HTML<a>.

What's more, urlizeThe filter will automatically add clickable links to the generated hyperlinks during the conversion processrel="nofollow"This property makes it easy to handle any plain text content containing URLs in templates, enabling clickable links and SEO-friendliness.

How to useurlizeFilter:

In the AnQi CMS template files, you can connect any variable that may contain a URL through a pipe|withurlizefilter. SinceurlizeThe filter generates HTML code, in order to ensure that these HTML codes can be correctly parsed by the browser and not displayed as plain text, you also need to add them immediately afterwards|safefilter.

Basic syntax is as follows:

{{ 您的变量 | urlize | safe }}

Among them:

  • 您的变量: Represents a template variable containing a URL string, such as article content (archive.Content) Single-page content (page.Content) or other custom text fields.
  • urlizeAutomatically convert URL to hyperlink and addrel="nofollow".
  • safeTell the template engine that the output content is safe HTML and should be rendered as is, without HTML entity escaping.

Why is it automatically addednofollow?

rel="nofollow"The attribute is used by search engines to identify links that should not follow or pass weight. For most links pointing to external sites, especially those of non-cooperative nature or user-generated content, addnofollowIs a recommended SEO practice. Anqi CMSurlizeThe filter is based on this **practice, and by default, it adds hyperlinks to URLs automaticallyrel="nofollow"Thus, it eliminates the cumbersome process of manual addition.

In addition, Anqi CMS provides global options to manage the behavior of external links in the background.Content settingsAlso, it provides global options to manage the behavior of external links in the context.帮助文档 -> 后台设置 -> 内容设置(help-setting-content.mdIt was mentioned in the ).

Do you want to automatically filter external links: If you do not want external site links in the content you post, you can choose to filter out the links.This will automatically clear any external links when the document is published.If you choose not to filter external links, the links will be retained but will be added withrel="nofollow".

This means that even if you do not use the filter in the templateurlizeIf the background has enabled the 'Do not filter external links' option, the system will also automatically add external links detected during content savingrel="nofollow".urlizeThe filter provides the same for those plain text URLs that may not have been processed by the backend during front-end renderingnofollowFeature.

In which scenarios should this technique be applied?

This method is very practical in various content types:

  1. Content of articles and product detail pages (archive.Content):This is the most common application scenario. When editors paste URLs directly into articles or product descriptions, throughurlizefilters, these URLs can automatically become clickable links and come withnofollow.
  2. Single-page content (page.Content):For "Contact Us", "About Us" and other single-page sections, if they contain the plain text URLs of the company website or social media homepage, they can also be usedurlizePerform conversion.
  3. Custom text field:If you define a custom text field in the content model (such as a "Reference URL" field), and the field may contain plain text URLs, it can also be done byurlizeProcess with the filter.
  4. Friendship Links:Although friendship links are usually throughlinkListTags and background management functions are centralized for management, and may be explicitly set in the backgroundnofollowproperties such asitem.Nofollow == 1),But if some friendship links or external recommendation links are embedded in non-structured content in plain text,urlizeit can also provide a safety net.nofolloweffect.

Operation steps with code examples

Assuming you want to display the publication date of the article on the ({模型table}/detail.htmlorarchive/detail.htmlIn it, make all URL strings in the article content clickablenofollowlinks.

  1. Open your template file:Find the template file for rendering the article content. Typically this will be/template/{您的模板名称}/archive/detail.htmlor similar path file.

  2. Modify content output code:Find the output article content (archive.Content) line of code. It may look like this:

    {% archiveDetail articleContent with name="Content" %}
    {{ articleContent|safe }}
    

    Modify it to useurlizeFilter:

    {% archiveDetail articleContent with name="Content" %}
    {{ articleContent|urlize|safe }}
    

    Similarly, if your content is included in other variables, such as a custom fieldmy_custom_url_field, you can use it like this:

    {% archiveDetail myCustomUrlField with name="my_custom_url_field" %}
    <p>更多信息请访问:{{ myCustomUrlField|urlize|safe }}</p>
    

Example effect:

IfarticleContentThe variable contains the following text:

欢迎访问我们的官网 https://en.anqicms.com,这是一个优秀的CMS系统。您也可以通过邮箱联系我们:[email protected]

after{{ articleContent|urlize|safe }}After processing, the actual HTML rendered on the page will be:

欢迎访问我们的官网 <a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>,这是一个优秀的CMS系统。您也可以通过邮箱联系我们:<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>。

URL and email addresses have been successfully converted to includerel="nofollow"attributes.

Points to note

  • **`|

Related articles

How to calculate the number of elements in a string (such as an article title) or an array (such as a tag list) in AnQiCMS templates?

In AnQi CMS template design, we often need to make accurate statistics of the content on the page, whether it is to dynamically display the number of data or to make conditional judgments based on the number, mastering how to calculate the number of elements in a string or array is particularly important.AnQiCMS's powerful template engine provides a concise and efficient method to handle these requirements.

2025-11-07

How to convert newline characters in multi-line text to the HTML `<br>` tag for display in AnQiCMS templates?

In AnQiCMS content management, we often enter multi-line text with line breaks, such as article content, product descriptions, or contact addresses.However, when this content is displayed on the website front-end, we may find that the original clear line breaks are missing, and all the text is squeezed into a single line.This not only affects the reading experience of the content, but may also be inconsistent with our original design intention.Why is this happening?

2025-11-07

How to remove specific HTML tags from an HTML string in the AnQiCMS template (such as `<i>`, `<span>`)?

In AnQiCMS template development, we often encounter content coming from rich text editors, or imported from external sources, which may contain some HTML tags that we do not want to display at specific locations.For example, in the summary section of the article list, we may only want to display plain text, or we may need to remove specific tags such as `<i>`, `<span>`, etc. to maintain the consistency of the page style.

2025-11-07

How to truncate a long string (such as an article summary) to a specified length and display an ellipsis in the AnQiCMS template?

In website content operation, the display length of article abstracts or introductions often needs careful control.Long content can affect the layout and user experience of a page, while a concise summary with an ellipsis can effectively guide users to click and read more details.AnQiCMS provides flexible template tags and filters, allowing us to easily implement this feature.

2025-11-07

How to format a floating-point number to a specified number of decimal places in AnQiCMS template?

In the presentation of website content, we often need to display various numbers, such as product prices, discount percentages, or statistical data.These numbers are often floating-point numbers, and in order to ensure the beauty of the page layout and the clarity of the information, we usually need to format them, such as uniformly retaining two decimal places.In the AnQiCMS template, thanks to its flexible template engine, we can conveniently use the built-in `floatformat` filter to meet this requirement.

2025-11-07

How to implement the conversion of the first letter to uppercase or all uppercase of the string in AnQiCMS template?

In website content presentation, the case format of strings often needs to be flexibly adjusted according to different scenarios, such as the first letter of the article title in uppercase, the username in all lowercase, or the brand name in all uppercase.AnQiCMS as a feature-rich enterprise-level content management system, its template engine provides convenient and powerful filters (Filters), which help us easily implement the case conversion of these strings, making content display more standardized and professional.

2025-11-07

How to customize the display content and link of the homepage Banner in AnQiCMS template?

In website operation, the homepage banner acts as the 'facade' of the website, and its visual effect and guiding role are crucial.A well-designed and tasteful Banner can not only attract visitors' attention but also effectively convey the core information of the website and guide users to take the next step.For friends using AnQiCMS, how to flexibly customize the display content and link of the home page Banner is a key step to improve the user experience and marketing effect of the website.

2025-11-07

How to loop to display user group information in AnQiCMS template?

In the website built with AnQiCMS, the user grouping function provides us with powerful content management and user permission division capabilities, whether it is to implement membership content, display different information according to user level, or plan VIP services, user grouping is an indispensable foundation.How to flexibly display these user grouping information on the front-end template of a website, which is a focus for many operators.AnQiCMS uses a template engine syntax similar to Django, which makes template writing intuitive and efficient.When it comes to user group information, the system provides

2025-11-07