When operating a website, we often add some URLs to articles, product descriptions, or single-page content, which may be recommended external resources or references to other content within the site.But if these URLs are displayed only in plain text, users will have to copy and paste to access them, which is inconvenient and affects the reading experience.
AnqiCMS fully considered this point, providing us with a very convenient way to automatically convert plain text URLs in the content into clickable links, greatly enhancing the convenience of user browsing and the professionalism of the website.
Make the URLs in the content automatically 'alive':urlizeFilter
The template engine provided by AnqiCMS (which draws on the syntax style of Django) has a very practical tool calledurlizeFilter. Its main function is to intelligently identify URL strings (includinghttp:///https://with prefixes, evenwww.with prefixes) and automatically wrap them into<a>The label can be turned into a clickable link.
The method of use is very simple, you just need to add it after the text variable you need to process|urlizeYou can. For example, if the content of your article is stored in{{ archive.Content }}In this variable, you can handle it like this:
<div>
{{ archive.Content|urlize|safe }}
</div>
Pay special attention here|safeThis filter. AnqiCMS for website security, the default is to escape the output content of the template to prevent potential XSS attacks. This means that ifurlizeThe filter converts the URL to HTML format.<a>After the tag, you directly output it, and the browser will display it as plain text instead of a real link. Add|safeTell the template engine that this content is safe HTML that can be rendered directly, so the link can be displayed and clicked normally.
By default,urlizeThe filter will also automatically add to the generated link.rel="nofollow"property.This is very helpful for the SEO strategy of external links, it tells search engines not to pass 'weight' to these links, which helps you better control the flow of external links on your website.
Handle long URLs:urlizetruncFilter
Sometimes, the URLs that appear in articles may be very long. Displaying them directly can make the page look cluttered and affect the overall aesthetics. At this time,urlizetruncThe filter comes into play. It isurlizeBased on that, it adds the feature of truncating link display text, allowing you to specify the maximum length of the link text.
urlizetruncthe way of use isurlizeSimilar, but an additional numeric parameter needs to be passed in to indicate the maximum display length of the link text. The part exceeding this length will be replaced with....
<div>
{{ archive.Content|urlizetrunc:50|safe }} {# 将网址显示文本截断为50个字符,多余部分显示... #}
</div>
This is a long URL, even if it is included in the original text, the link text displayed on the final page will be neat, and it will still be able to jump to the full original URL when clicked.
Content assistance: Markdown editor
In addition to applying filters directly in the template, if you are accustomed to using Markdown format when writing content in the background, AnqiCMS's Markdown editor can also automatically help you process URLs.
When you enter in a Markdown editor[点击这里](https://en.anqicms.com)such link syntax, or simply input a bare URLhttps://en.anqicms.com,The editor usually also renders the content into a clickable HTML link on the front end after it is published.
Make sure of this, you need to enable the Markdown editor feature in the "Global Settings" -> "Content Settings" of the AnqiCMS backend.If your content is mainly written through the backend editor and Markdown rendering is enabled, then many basic URL conversion tasks will be automatically completed at the content storage and rendering stage, further reducing the burden on the template level.
Why is it important to do this?
Whether it is throughurlizeandurlizetruncThe filter, or through the Markdown editor, converting plain text URLs into clickable links, can bring multiple benefits:
- Improve user experience:Users do not need to copy and paste, just click to access, which greatly simplifies the operation process.
- Improve the beauty of the page:Avoids long URL strings from destroying the page layout, especially
urlizetruncFilter, making long URLs display more elegantly. - Enhance content interactivity:Interactive pages are more likely to attract users to stay and explore.
- SEO-friendly (indirect):Although
nofollowAttribute limits the transmission of weight, but a clear link structure helps search engines better understand the page content, and improving user experience itself is also an important aspect of SEO.
Imagine you are writing an article about the new features of the Go language, in which you mentioned several related GitHub repository addresses and official document links. Rather than let them lie there barren, it is better to use them directlyurlizeThe filter makes them clickable instantly. When visitors read resources of interest, they can click and jump immediately, greatly enhancing the smoothness of reading and the efficiency of obtaining information.
ByurlizeandurlizetruncThe filter, combined with the powerful Markdown editor, makes it easy for AnqiCMS to automatically parse URLs.This not only makes your website content more interactive and improves user experience, but also makes the page layout more beautiful.In just a few steps, you can truly bring your website to life, making it convenient for every visitor to browse.
Frequently Asked Questions (FAQ)
1.urlizeWhy does the filter parse link by defaultrel="nofollow"?
AnqiCMS default tourlizeThe external link added by the filterrel="nofollow"It is mainly for SEO optimization considerations.nofollowThe attribute tells the search engine not to pass the weight or trust level of the current page to the linked external page.This helps prevent spam links and allows webmasters to better control the impact of external links on their own SEO.If you really need to pass the weight, you may need to manually adjust the link or consider other processing methods in specific scenarios.
2. Can Iarchive.Contentat the same timeurlizeandurlizetrunc?
In theory, the filter can be used in a chain. ButurlizetruncActually,urlizeThe expansion of the function, which includes the feature of truncating displayed text while converting URLs. Therefore, it is usually sufficient to select to useurlizetruncand specify the truncation length within it, no need to use it separatelyurlizeFor example,{{ archive.Content|urlizetrunc:50|safe }}can complete the parsing and truncation display function.
3. How can I prevent the URLs in my content from being automatically parsed as links?
In most cases, automatically parsing URLs provides a better user experience. However, if you have special requirements and do not want certain URLs to be automatically parsed, you can consider the following methods:
- Manual control content:If it is a Markdown editor, avoid using naked URLs and instead use
\Escape special characters, or use code blocks in a way that prevents parsing of the text. - Custom content model field: For very specific URLs that do not require automatic parsing, you can consider creating a dedicated custom field in the content model to store these special URLs, and then output the content of the field in plain text in the template without using
urlizefilter. - Locally disable filter:If only a small part of the content is not desired to be parsed, it can be preprocessed before output, or the template can be used to skip parsing specific content.
urlizeProcess.