Optimize content display: The application of variable filters in AnQi CMS
How to present information accurately and beautifully in website content management is the key to improving user experience.The Anqi CMS is a flexible and efficient content management system that provides a powerful template engine, allowing us to finely process and display variable content through various filters, thus better meeting the needs of page layout and information delivery.These filters are like the 'makeup artists' of content, allowing raw data to be displayed in the most appropriate form.
The template engine of Anqi CMS adopts syntax similar to Django, which means we can make full use of the rich filter functions when dealing with variables in templates. A variable is usually represented by double curly braces{{ 变量名 }}Translate to English. And when we need to process this variable, we just need to add a pipe symbol after it.|English, followed by the name of the filter. If the filter requires parameters, use a colon.:Separated. For example,{{ 变量名 | 过滤器名称 : 参数 }}. This concise syntax makes dynamic content processing easy.
Simplified text:truncatecharsandtruncatechars_html
Imagine you have a list of articles, each with a long description. Displaying them directly might make the page look disorganized.truncatecharsThe filter comes into play. It can truncate the string to the specified number of characters and automatically add an ellipsis at the end....)。This is very useful for creating content summaries or fixed-length titles.
For example, if you want to display only the first 50 characters of the article description:{{ item.Description | truncatechars:50 }}Here, even if the original description is very long, only 50 characters and an ellipsis will be displayed on the page, maintaining the page's neatness.
It is worth noting that,truncatecharsIs aimed at plain text. If your description content contains HTML tags (such as content generated by rich text editors), use it directly.truncatecharsIt may damage the HTML structure. Safe CMS providestruncatechars_htmlFilter.It truncates text while intelligently handling HTML tags, ensuring that the output HTML is still valid.This is particularly important when dealing with rich text summaries containing images, links, etc.
Safe output:safeThe importance of filters
In website development, content security is always of great importance, especially when the content comes from user input or rich text editors.HTML or JavaScript code that has not been processed may lead to cross-site scripting (XSS) attacks.<Converted to<,>Converted to>to prevent the execution of malicious code effectively.
However, in some cases, we indeed need to display the original HTML content, such as the main body of the article, the formatting content generated by rich text editors, etc.safeFilter is crucial. By adding to the variable|safeWe can tell the template engine that the current output content is 'safe' and does not need to be HTML-escaped, and can be rendered directly as HTML code.
For example, when displaying the article content:{{ archive.Content | safe }}This will ensure that the HTML elements such as titles, paragraphs, images, etc. in the article can be displayed normally and not treated as plain text. But please note that you should only use it when you are sure that the content is reliable and does not contain malicious scripts.safeFilter. Caution should be exercised or additional cleaning should be performed for unreviewed content submitted by users.
Overview of other practical filters.
In addition to the aforementioned two commonly used filters, Anqi CMS also provides many other convenient content processing filters:
default:Set a default value for the variable. If the variable is empty or does not exist, it will display the default value you specified, to avoid the page from appearing blank or with errors.{{ userName | default:"匿名用户" }}stampToDateFormat the timestamp into a readable date and time.The template function is also provided by AnQi CMS to handle 10-digit timestamps and supports Golang date format strings.{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}upper/lower/title:Used to adjust the case of the text.upperConvert to uppercase,lowerConvert to lowercase,titleThen capitalize the first letter of each word.{{ item.Title | upper }}striptags/removetags: is used to remove HTML tags from text.striptagsRemove all HTML tags,removetagsCan specify to remove a specific HTML tag.{{ item.Description | striptags }}addIs used to concatenate strings or perform numerical addition.{{ "商品数量:" | add:item.Quantity }}urlize:Automatically identify URLs or email addresses in text and convert them into clickable hyperlinks. This is very useful for automatically generating links in comments or messages.{{ comment.Content | urlize | safe }}
Flexible combination and **Practice
The filters of the Anqi CMS can be chained, which means you can apply multiple filters to a variable consecutively, with the processing order from left to right. For example:{{ item.Content | striptags | truncatechars:100 | safe }}This code will first remove HTML tags, then truncate to 100 characters, and finally output safely. This combination capability makes content processing very flexible and powerful.
In practical operations, the reasonable use of these filters can greatly enhance the quality and maintenance efficiency of website content. For fields that need to be limited in length, such as titles, summaries, it is preferable to considertruncatecharsortruncatewordsauto. For rich text content, such as article details, it is imperative to usesafeBut make sure the content source is secure. Through continuous practice and exploration, you will find that the filter of the security CMS is an indispensable tool for content operation.
Common Questions (FAQ)
问:为什么我使用
truncatecharsWhen truncating content containing HTML, is the page display abnormal? AnswerThis is becausetruncatecharsThe filter truncates text purely, it does not intelligently recognize and handle HTML tags.When the HTML tag is truncated, it may lead to unpaired tags, thereby destroying the page structure.truncatechars_htmlFilter that can safely truncate text containing HTML while maintaining the integrity of the HTML structure.Q:
safefilters andautoescape offWhat is the difference between tags? Which one should I use? Answer:safeThe filter acts on a single variable, it tells the template engine not to escape the output content of the specific variable.autoescape offLabels act on a template code block, they disable the automatic HTML escaping of all variables within the code block. It is generally recommended to usesafeFilter, as it has a smaller scope of action, it can more precisely control which content should not be escaped, reducing security risks. It should only be considered when it is very certain that the entire content of the code block does not need to be escaped.autoescape off.问:Can I use a custom filter? What custom methods does Safe CMS support? Answer:English CMS is developed based on Go language, and its template engine supports extension.Although the document does not directly specify how users can add custom filters through the backend interface or simple configuration, as a customizable and extensible system, this usually means that you can register new filter functions by modifying or writing Go language code.For ordinary operators, it is recommended to prioritize the use of the rich built-in filters of the system to meet their needs.If there are special business requirements, you can consult the developers or refer to the more in-depth secondary development documentation.