In website content management, we often encounter scenarios where we need to display various links and email addresses in articles or pages.Manually converting this text to clickable HTML links is time-consuming and prone to errors, especially when the volume of content is large, the workload is even more惊人.AnQi CMS fully understands this pain point of users, built-in high-efficiency and intelligent text processing mechanism, can automatically identify and convert URLs and email addresses, greatly enhancing the efficiency of content publishing and user browsing experience.
The implementation of this feature is mainly due to the powerful filter provided by the Anqi CMS template engine——urlizeandurlizetruncThey are like intelligent text assistants, capable of automatically scanning your content, finding potential links and email addresses, and converting them into standard HTML<a>.
Smart link conversion:urlizeFilter
urlizeThe filter is the core tool in AnQi CMS for automating link conversion. When you enter a segment containing URLs (such ashttp://www.anqicms.com/https://anqicms.comevenwww.anqicms.com) or email addresses (such as[email protected]The text applies this filter, it will automatically wrap these text segments in clickable hyperlinks.
The main advantage of this feature lies in:
- Improve user experience:Visitors do not need to manually copy and paste and can directly click to visit related pages or send emails, making the content interaction more smooth and natural.
- Simplify content creation:Users can directly input the original URL or email address when writing content, without manually adding.
<a href="...">Tags greatly save time and reduce the possibility of making mistakes. - SEO friendly:By default,
urlizeThe generated link will automatically containrel="nofollow"Property. This is very helpful for controlling the transmission of external link weight on the website, avoiding unnecessary SEO risks.If your website needs to pass weight to these external links, it can also be adjusted through additional SEO strategies.
Using in Anqi CMS template,urlizeThe filter is very simple. You just need to pass the text variable to be processed through the pipe symbol|pass tourlizeAnd the filter can be. SinceurlizeIt will generate HTML tags, in order to ensure that these HTML codes can be rendered correctly rather than being escaped as plain text by the browser, it is also necessary to usesafefilter.
Example:
Suppose you have a variable namedarticle.ContentThe variable, which includes the main content of the article:
<p>欢迎访问安企CMS官网:http://www.anqicms.com,如果您有任何问题,可以发送邮件至 [email protected]。</p>
In the template, you can use it like thisurlizeFilter:
{{ article.Content|urlize|safe }}
After rendering, this content will automatically become:
<p>欢迎访问安企CMS官网:<a href="http://www.anqicms.com" rel="nofollow">http://www.anqicms.com</a>,如果您有任何问题,可以发送邮件至 <a href="mailto:[email protected]" rel="nofollow">[email protected]</a>。</p>
You can also pass throughurlizeThe filter to control whether the link content is HTML escaped, for exampleurlize:trueorurlize:falseIt depends on your needs, but usually withsafewhen combined,urlizeThe default behavior can meet most scenarios.
Optimize display:urlizetruncFilter
Sometimes, the URL in the content may be very long, and displaying it in full directly can affect the beauty and readability of the page.urlizetruncThe filter is designed to solve this problem.urlizeBased on that, control over the display length of link text has been added.
urlizetruncThe working method of the filter is withurlizeSimilar, but it allows you to specify a numeric parameter to limit the maximum number of characters displayed in the link text. If the text length of the original URL exceeds this limit,urlizetruncAn ellipsis will be added at the end of the link text...to indicate that the content has been truncated, but the actual link is still the full original URL
Example:
Continuing with the above.article.ContentVariable, and I hope to limit the display length of the link text to 20 characters:
{{ article.Content|urlizetrunc:20|safe }}
After rendering, this content may be displayed as:
<p>欢迎访问安企CMS官网:<a href="http://www.anqicms.com" rel="nofollow">http://www.anqicms.com...</a>,如果您有任何问题,可以发送邮件至 <a href="mailto:[email protected]" rel="nofollow">[email protected]...</a>。</p>
(The specific truncation position and ellipsis will vary according to the actual text and character set, this is just an example)
ByurlizetruncYou can maintain the neatness and consistency of the page, especially in lists, summaries, or any area where text length needs to be controlled, it can play an important role.
**Practice and Application Scenarios
In AnQi CMS,urlizeandurlizetruncThe filter is most commonly used on article detail pages, product descriptions, message board content, comment sections, and other user-generated content or dynamic text areas.They ensure that all URLs and email addresses in the input text are presented in a user-friendly manner without manual intervention.
Remember to do so when using it.|safeThe importance of the filter. BecauseurlizeandurlizetruncIt will generate HTML code, if missing|safeThese HTML codes will be displayed as plain text, rather than being parsed by the browser as clickable links.
AnQi CMS simplifies the workflow of content operators through these intelligent filters and also provides a more convenient and efficient browsing experience for website visitors.No matter whether your content is a long essay or a brief introduction, these tools can make the links and contact information within it come alive.
Frequently Asked Questions (FAQ)
Q1: Why am I entering URLs or email addresses in the content, but the page displays plain text instead of clickable links?A1: This is likely because you applied in the templateurlizeorurlizetruncwithout using the filter immediately afterwards|safeFilter. The Anqi CMS defaults to escaping the output HTML code to prevent security issues. WhenurlizeFilter generation<a>after the tag, you need|safeClearly inform the system that this part of the content is safe HTML that can be directly parsed and rendered by the browser. Make sure that your template code is similar{{ content_variable|urlize|safe }}.
Q2:urlizeThe converted link, can I customize their styles?A2: OK.urlizeGenerated by the filter<a>Tags are standard HTML elements, you can completely control their styles with CSS. For example, you can define them in the CSS file of your websitea[rel="nofollow"]or more generalaLabel styles to beautify these automatically generated links. You can set colors, underlines, font sizes, etc., to keep them consistent with the overall design style of your website.
Q3:urlizeCan it automatically recognize all formats of links and email addresses? For example, links with Chinese domain names or special symbols?A3:urlizeThe filter mainly depends on standard URL and email address formats (such ashttp:///https:///www./@Translation of the value field to English.xn--It appears in ASCII compatible form at the beginning, or in other standard, recognizable URL formats, which are usually correctly identified. However, for overly special or non-standard text formats, or some texts that are not valid URLs or email addresses but have similar forms,urlizeMay not be recognized or converted correctly. It is recommended to standardize the link format of the content before use to achieve **conversion effects.