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:urlizeDoes the filter apply directly to all built-in template tags (such assystemandcontactWhat is the output?This is a very practical and worthy of in-depth discussion issue, as it directly relates to how we ensure that the URLs and email addresses on the page can be correctly identified and converted into clickable links, while also considering SEO friendliness.
To understand this, we first need to clarify the respective responsibilities of "tags" and "filters" in the AnQiCMS template engine. Built-in template tags, such assystemandcontactThey are mainly used to retrieve specific data from the system and output it to the template. For example,{% system with name="SiteCopyright" %}and output the copyright information string of the website,{% contact with name="Email" %}It will output the preset contact email address string. They focus on 'what data to obtain'.
While filters, such asurlizeare when the data is outputAfter thatand then performprocessing.the tool.urlizeThe core function of the filter is to scan the text content, automatically identify any existing URLs (such ashttp://example.com/www.example.com) and email addresses (such as[email protected]), and wrap them automatically in<a>Make the link clickable within the tag. It is worth noting that,urlizeit will also automatically add these generated links torel="nofollow"Attribute, this is a very useful default behavior for the website's SEO strategy.
Therefore, the answer to the question is:urlizeThe filter itself andis notsystemorcontactThe built-in label function does not take effect automatically when the label outputs data. Instead,urlizeThe filter should be applied to the labels thatthe text content output. This means that whensystemorcontactLabeling and outputting a string, you can then use this string asurlizethe input for the filter to identify and convert URLs and email addresses.
For example, if you have configured the 'Site Copyright' (SiteCopyright) in the background 'System Settings', and this copyright information contains a website URL, such as '© 2023 AnQiCMS. Visit our website:https://en.anqicms.com。如果直接使用`{%system with name=“SiteCopyright” %}`,then the output will still be plain text. But if you use it like this:
<p>{{ system('SiteCopyright') | urlize | safe }}</p>
Or, first assign the output to a variable and then process it, which is clearer:
{% system copyright_text with name="SiteCopyright" %}
<p>{{ copyright_text | urlize | safe }}</p>
ThenurlizeThe filter will processcopyright_textThe text in this variable willhttps://en.anqicms.comAutomatically converted to<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a>.
Similarly, forcontactThe contact information output by the tag, if联系邮箱The field contains is a plain text email address:
{% contact email_address with name="Email" %}
<p>联系我们:{{ email_address | urlize | safe }}</p>
This will[email protected]to<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.
It needs to be emphasized here| safeThe use of the filter. BecauseurlizeThe filter will generate HTML tags (<a>),while AnQiCMS's template engine defaults to escaping all outputs for security reasons to prevent XSS attacks. If not| safe,<a>label's<and>characters will be escaped to<and>Add, causing the link to fail to display correctly.| safeThen explicitly tell the template engine that this content is safe and does not need to be escaped, and can be output directly as HTML.
In summary,urlizeThe filter is a general-purpose text processing tool that can intelligently convert any string containing potential URLs or email addresses. It is not built intosystemorcontactIn the output mechanism of tags, but as an independent processing step after these tags output data. By reasonably usingurlizeThe filter is applied to the output of these tags, allowing us to easily create clickable links on the page, improve user experience, and optimize the website's SEO.
Frequently Asked Questions (FAQ)
urlizeDoes the filter apply to all data types?urlizeThe filter is mainly designed to handle strings containing URLs or email addressesor plain text contentIf the label outputs a number, boolean value, or is already in HTML structure (such as an image URL or already contains<a>a link tag), thenurlizeThe filter does not work, and it may even produce unexpected results due to improper HTML structure. Therefore, when usingurlizeEnsure that the input is a text string you wish to recognize and convert to a link.When should I use it?
urlizeFilter?When a text field may contain a user input URL or email address, and this content has not been formatted with HTML beforehand,urlizeIf the content output by the label itself is a link (such as
item.Link), it still needs to useurlizefilter?Do not need. If the content output by the label is already a formatted link string (such ashttps://www.example.com/page), or has been generated in a complete way by other means<a>label (such asitem.Linkusually output the URL directly, then you use it in the template<a href="{{ item.Link }}">{{ item.Title }}</a>to build), then you can use it againurlizeThe filter is redundant.urlizeIts value lies in its ability to extractunformatted plain textThe AI recognizes and creates links instead of processing strings that are already determined to be links. Overuse not only wastes resources but may also lead to unnecessary HTML structure nesting.