In daily content creation and website operation, we often need to cite external resources or provide links to more information.Manually converting these links into clickable hyperlinks is not only time-consuming, but also prone to errors, especially when dealing with large amounts of content or Markdown-formatted text.AnQiCMS (AnQiCMS) deeply understands the pain points of content operation, and through its powerful template filter function, it provides us with an elegant solution -urlizeFilter, it can automatically identify and beautify the URL links in Markdown content.
urlizeFilter: Smart recognition and convenient beautification of URLs.
urlizeThe filter is a very practical tool in the Anqi CMS template engine, its core function being able to intelligently recognize potential URL strings in text content and automatically convert them into standard HTML hyperlinks (<a>Label). Whether it is completehttp://orhttps://URLs that start without a protocolwww.URLs that start with even pure domain names likeexample.comas well as email addresses[email protected],urlizecan be accurately captured and processed.
This automation capability greatly liberates the hands of content creators and operators.Imagine writing a technical article in a Markdown editor, referencing multiple tool websites, reference links, or leaving URLs in user comments in the comments section.If you need to manually add links to each[文本](链接)Markdown syntax or<a>tags, no doubt will increase a lot of repetitive work.urlizeFilters are exactly born to solve this problem, making content publishing more efficient and convenient.
It is worth mentioning that it is considered for search engine optimization (SEO),urlizeThe filter will also automatically add these links when generating hyperlinks.rel="nofollow"The attribute tells the search engine not to track these links, nor to pass the weight of the current page to these external links. This is crucial for managing the flow of external links on the website, preventing weight dispersion, and maintaining the SEO health of the website.
urlizeIt also supports an optional boolean parameter to control the escaping of link content. When set totrueWhen, the link text content will be HTML entity escaped; when set tofalseit will not be. This provides additional flexibility for us to handle links containing special characters.
urlizetrunc: Make long URLs neat and tidy
Sometimes, URL links may be very long, and displaying them directly on the page may look cluttered, affecting the overall aesthetics of the layout. Anqi CMS providesurlizetrunca filter, which isurlizean enhanced version that retainsurlizeOn top of all the features, it has added the function of smartly truncating long link text.
UseurlizetruncAt this point, we can specify a numeric parameter to limit the display length of the link text. When the displayed text of the original URL exceeds this specified length,urlizetruncIt will automatically truncate and add an ellipsis (...), making the link display on the page more tidy. For example, a long URL may be beautified ashttps://www.example.com/long/path/to/resource...This greatly enhances the visual experience of the page while maintaining the link function.
How to apply these filters in the Anqi CMS template.
Apply in the template files of Anqi CMS.urlizeorurlizetruncThe filter is very simple. They are usually used with variables, through the pipe symbol|Connecting. Since these filters generate HTML tags, it is usually necessary to use them in conjunction with|safeA filter to ensure that the browser can correctly parse and display the generated HTML code, rather than outputting it as plain text.
Here are some usage examples:
UseurlizeFilter:
{# 假设 content 变量包含了 Markdown 格式的文本,其中有URL链接 #}
<p>{{ content|urlize|safe }}</p>
{# 也可以通过 filter 标签块来应用,并指定不转义链接内容 #}
{% filter urlize:false|safe %}
<p>请访问我的网站:www.anqicms.com 或发送邮件至 [email protected]。</p>
{% endfilter %}
UseurlizetruncFilter (for example, truncating to display 15 characters):
{# 假设 description 变量包含长URL,希望截断显示 #}
<p>{{ description|urlizetrunc:15|safe }}</p>
{# 通过 filter 标签块应用,截断显示为20个字符 #}
{% filter urlizetrunc:20|safe %}
<p>您可以在这里找到更多信息:https://en.anqicms.com/documentation/detail/long-url-example-document.html</p>
{% endfilter %}
In these examples,|safeThe role of the filter cannot be ignored. Without it, like<a href="...">...</a>such HTML code will be automatically escaped by the template engine as<a href="...">...</a>This causes the link to not display normally.
Practical application scenarios and content operation value
In the content operation strategy of Anqi CMS,urlizeandurlizetruncFilters are a tool to enhance user experience and optimize SEO.
- Enhancing user experience: Whether it is in a blog post, product description, or user comment, users are accustomed to seeing clickable links.
urlizeAutomatically converts plain text URLs into hyperlinks, saving the trouble of copying and pasting for users, making information acquisition more smooth. - Optimize SEO: Added automatically
rel="nofollow"Property,urlizeHelp website operators to better control the SEO signals of external links, avoid unnecessary weight loss, and help maintain a good ranking in search engines. - Keep the content tidy.:
urlizetruncEspecially suitable for content areas that may contain long links, such as sidebar recommendations, footer friend links, or brief product descriptions.It can intelligently trim the link display text, making the page visually cleaner and more efficient in presenting information. - Efficiently handle Markdown content: These two filters can be seamlessly integrated into content that uses a lot of Markdown format, automate URL processing, greatly reduce the workload of content editing, and make the publishing process smoother.
In summary, of Anqi CMS'surlizeandurlizetruncThe filter is an invaluable assistant in content operations. It provides powerful automation features with concise syntax, helping us take solid steps in beautifying content, optimizing user experience, and improving SEO performance.
Frequently Asked Questions (FAQ)
1.urlizeWhat types of links does the filter handle?
urlizeThe filter is very intelligent and can recognize various forms of URLs, including:
- Start with
http://orhttps://Complete URLs starting with, for example:https://en.anqicms.com. - Without a protocol
www.URL starting with, for examplewww.anqicms.com. - URL in domain name format, for example
anqicms.comNeed context judgment. - Email address, for example
[email protected].
2.urlizeDoes the filter automatically add the nofollow attribute? What is the use of this attribute?Yes,urlizeThe filter will automatically generate hyperlinks for the URLs it recognizes and converts in the text.<a>tagsrel="nofollow"Property.nofollowThe attribute's purpose is to tell the search engine not to follow this link and not to pass the page authority to the target page linked from this page. This is very useful in content operations, for example:
- Prevent comment spam links from affecting the website's SEO.
- Control the flow of external links from the website to avoid unnecessary weight loss.
- When linking to some unofficial or uncontrollable external resources, maintain the SEO health of the website.
3.urlizeandurlizetruncWhat should be noted when using the filter?Mainly pay attention to the following two points:
|safeUse of filters: Due tourlizeandurlizetruncThe filter generates HTML code (<a>Label), make sure to add it after these results in the Anqicms template|safefor example, a filter (such as{{ content|urlize|safe }}If missing|safeThe template engine escapes HTML code by default for security reasons, which causes the original HTML tag text to be displayed on the page instead of clickable links.urlizetrunclength truncation: In useurlizetruncAt the time, it is necessary to set the truncation length parameter reasonably. An overly short length may cause the link content to lose readability, while an overly long length may not achieve a neat layout effect.Adjust according to the page design and actual requirements.