Storing URLs in the custom field of AnQiCMS backend is a very practical feature, allowing us to add various personalized information to the content model according to business needs.However, when these URLs need to be displayed in the frontend template, we not only need to ensure they are output correctly, but sometimes we also hope to perform some basic format checks to enhance the robustness and user experience of the page.stringformatThe filter combines with other features to process and initially 'verify' the URLs stored in custom fields.

AnQiCMS中的自定义字段:存储URL的基石

AnQiCMS provides flexible content model features, allowing users to customize fields according to actual needs, such as text, numbers, images, etc.When we need to store external links, download addresses, or social media homepages, etc., we usually choose the 'Single Line Text' type of custom field.外部资源链接”的自定义字段,其“调用字段”设定为external_link,用于存储与文章相关的外部网址。

When editing content in the background, the user will enter the full URL (such ashttps://www.example.com/resourceEnter this custom field.Once the data is saved, we can retrieve the values of these custom fields through specific tags in the front-end template.archiveDetailTags:

{% archiveDetail externalLinkValue with name="external_link" %}

Here,externalLinkValuewhich is the URL string we get from the custom field "external_link".

UnderstandingstringformatFilter

stringformatis a powerful and general filter provided by AnQiCMS template engine, which mainly acts to convert various data types (whether they are numbers, strings, or more complex objects) into standard string formats according to our predefined formats for output. Its usage is similar to that in Go language.fmt.Sprintf()function.

For example, if we want to ensure that a variablemyVaris output in the form of a plain string regardless of its original type, we can usestringformatof%sthe format specifier:

{{ myVar|stringformat:"%s" }}

Althoughstringformat主要用于格式化输出,但它在处理自定义URL字段时,首先确保了我们拿到的是一个纯净的字符串,为后续的检查和展示奠定了基础。

Display and preliminary "validation" of URL in the template

It is a basic requirement to display the URL stored in a custom field in the template, but if the URL format is incorrect, it may cause the link to fail or the page to show an error. AlthoughstringformatDoes not have strict URL syntax validation capabilities (for example, judgmenthtttp://Is it a legal protocol), but we can achieve a lightweight, template-level preliminary "verification" by combining other filters and logical judgment tags in the AnQiCMS template.

Here, the term 'verification' mainly refers to checking whether the URL contains common protocol prefixes (such ashttp://orhttps://),and ensure it is not an empty value. If these basic checks pass, we then render it as a clickable hyperlink.

To achieve this, we can take the following steps:

  1. Get custom field valuesAs described above,archiveDetailtags to obtain the stored URL.
  2. Cleaning and preliminary inspection: UsetrimFilter removes any leading and trailing whitespace characters from the URL string, thenlengthFilter checks its length, ascontainFilter checks if it contains a protocol header.
  3. Formats the output and converts: UtilizingstringformatEnsure the output is a string, then combineurlizeThe filter converts strings into clickable hyperlinks.urlizeThe filter itself has certain intelligent recognition capabilities, which can automatically convert URLs and email addresses in the text to<a>tags and add them by defaultrel="nofollow"Properties, which are beneficial for SEO and user experience.

Practical Case: Displaying URL safely and beautifully.

Suppose we have a custom field, the field name calledmy_custom_url_fieldEnglish, used to store the reference link of the article.We hope that in the article detail page, if this field has a value and looks like a valid URL, it should be displayed as a clickable link, otherwise, display the corresponding prompt.

`twig {# 1. From the custom field to get the URL value #} {% archiveDetail customUrlField with name=“my_custom_url_field” %}

{% if customUrlField is not empty %} {# Check if the field is not empty #}

{# 2. 清理URL字符串,去除首尾空白字符 #}
{% set cleanUrl = customUrlField|trim %}

{# 3. 进行初步“验证”:检查长度并判断是否包含常见的协议头 #}
{% if cleanUrl|length > 7 and (cleanUrl|contain:"http://" or cleanUrl|contain:"https://") %}
    <p>
        外部资源链接:
        {# 4. 使用stringformat确保输出为字符串,再通过urlize转换为可点击链接 #}
        {{ cleanUrl|stringformat:"%s"|urlize|safe }}
    </p>
{% else %}
    {# 如果初步验证不通过,提供提示 #}
    <p>外部资源链接:提供的地址 (<code>{{ cleanUrl }}</code>) 格式不符合URL规范,请检查。</p>
{% endif %}

{% else %} English

{# 如果自定义字段本身就没有值,也提供提示 #}
<p>外部资源链接:暂无。</p>

{% endif