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.rel="nofollow"Properties, to avoid unnecessary 'weight loss' or passing unnecessary trust. Anqi CMS provides a simple and efficient method to solve this problem.

Reveal the core features:urlizeFilter

The template engine of Anqi CMS provides powerful filter functions,urlizeThe filter is the core tool to solve the "automatically convert URL strings to clickable hyperlinks" requirement. Its function is to intelligently recognize URL strings in the text content (whetherhttp:///https://Start, orwww.Starting URLs, even email addresses), and automatically wrap them in clickable HTML<a>Label.

What's more,urlizeThe filter will automatically add hyperlinks to the generated content during the conversion processrel="nofollow"属性。这使得您在模板中处理任何包含URL的纯文本内容时,都能轻松实现链接的可点击性和SEO友好性。

How to useurlizeFilter:

In the template files of AnQi CMS, you can pass any variable that may contain a URL through a pipe|Withurlizefilter connection. SinceurlizeThe filter will generate HTML code, in order to ensure that these HTML codes can be correctly parsed by the browser instead of being displayed as plain text, you also need to add it immediately after.|safeFilter.

The 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.
  • urlize:执行URL到超链接的自动转换,并添加rel="nofollow".
  • safeTell the template engine that the output content is safe HTML and should be rendered as is, rather than being escaped as HTML entities.

Why is it automatically addednofollowproperties?

rel="nofollow"The attribute is used by the search engine 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, addnofollowIt is a recommended SEO practice. The Anqi CMS isurlizeThe filter is based on this **practice, and it adds hyperlinks to URLs automatically by default.rel="nofollow"Thus, it eliminates the cumbersome manual addition.

In addition, the Anqi CMS provides global options to manage the behavior of external links in the background.Content SettingsIn the following, there is also a global option to manage the behavior of external links.帮助文档 -> 后台设置 -> 内容设置(help-setting-content.md) It is mentioned in the following:

Whether to automatically filter external linksWhen you do not want external links in the content you post, you can choose to filter outlinks.This will automatically remove external links when publishing documents.rel="nofollow"Label.

This means that even if you have not used the templateurlizefilters, if the "Do not filter external links" option is enabled on the backend, the system will also automatically add external links detected during content savingrel="nofollow".urlizeFilter provides the same for those plain text URLs that may not have been processed by the backend during frontend renderingnofollowFunction.

In which scenarios to apply this technique?

This method is very useful in various content types:

  1. Content of article and product detail page (archive.Content):This is the most common application scenario. When editors paste URLs directly into articles or product descriptions,urlizethese URLs can be automatically turned into clickable links and accompanied bynofollow.
  2. [en]Single-page content (page.Content):For single pages like "Contact Usurlizeto convert.
  3. Custom text field:If you define a custom text field (such as a "References URL" field) in the content model and the field may contain plain text URLs, it can also beurlizeFiltering is performed.
  4. Friendship links:Although friendship links are usually throughlinkListTags and background management features are centrally managed, and may be explicitly set in the backgroundnofollowproperty (such asitem.Nofollow == 1),但如果某些友情链接或其他站外推荐链接以纯文本形式嵌入到非结构化内容中,urlize也能提供兜底的nofollow效果。

Operation steps with code examples

假设您希望在文章详情页 ({模型table}/detail.htmlorarchive/detail.htmlIn it, make all URL strings in the article content automatically clickablenofollowLink.

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

  2. Output code for modifying content:Find the code line for outputting the article content (archive.Content) 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_fieldYou 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>。

The URL and email address have been successfully converted to clickable links withrel="nofollow"clickable links.

Precautions

  • **`|