As an experienced website operations expert, I know that flexibility and efficiency are the key to success in daily content management.Especially on the content display level, how to quickly and accurately adjust the string content on the page, whether it is for SEO optimization, brand unity, or to cope with the ever-changing market needs, it is a core skill that we must master.Today, let's delve into how the AnQiCMS template cleverly implements the replacement function for string content, enhancing your content operation like a tiger wing.
AnQi CMS provides great convenience for small and medium-sized enterprises and content operation teams with its concise and efficient architecture.Its template engine is based on Django syntax, making it not only easy to learn and use but also built-in with rich filters (Filters) and tags (Tags), making it easy to process string content on the front-end.We do not need to directly modify the original data in the database, but can dynamically replace and adjust content and format during page rendering, which undoubtedly brings unprecedented flexibility to content operations.
Core mechanism: Flexible template filter for string replacement
In Anqi CMS template engine, string content replacement is mainly achieved through 'filters'.A filter is a concise syntax structure that allows us to perform various transformations and processing on the output value of variables.{{ 变量名|过滤器名称:参数 }}This is the most direct way to implement string replacement.replacefilter.
replaceFilter: The tool for precise replacement.
replaceThe filter is your preferred choice for precise replacement of string content. Its function is to replace the specified old string in the variable with a new one.
How to use: {{ 您的变量|replace:"旧字符串,新字符串" }}
For example, suppose you have used "AnQiCMS" multiple times in your content, but you want to display it as "AnQiCMS" uniformly on the front end. You can do this in the template:
{{ archive.Title|replace:"安企CMS,AnQiCMS" }}
This will change the document titlearchive.TitleAll occurrences of "AnQiCMS" are replaced with "AnQiCMS" without affecting the original data in the database. This non-destructive replacement method is particularly suitable for the following scenarios:
- SEO keyword optimization:Quickly adjust the density or variants of keywords on the page to adapt to changes in search engine algorithms.
- Brand naming consistency:Ensure consistency in the display of company names, product names, etc., throughout the site.
- Temporary content correction:Urgently correct errors or outdated information on the page without waiting for backend data updates.
- Link update:Although there is a background link replacement function for the entire site, the filter can also provide assistance for dynamic link replacement in certain specific situations.
It should be noted that,replaceThe “old string” and “new string” in the filter need to be separated by an English comma,Perform separation.If the old string to be replaced is empty, it will match at the beginning of the string and after each UTF-8 sequence.If the new string is empty, it is equivalent to deleting the old string.
Other commonly used string processing filters: enrich your content presentation
In addition to directreplace,AnQi CMS also provides a large number of practical string processing filters, which are not directly called "replacement", but can "replace" or "change" the form of strings in different dimensions, thereby optimizing content display.
cutwithtrim:Content cleaning good helpercut: "关键词":Remove all specified keywords from the string. For example,{{ "Hello AnQiCMS World"|cut:"AnQiCMS" }}The output will be 'Hello World'.trimRemove spaces or specified characters from the beginning and end of the string.{{ " 内容 "|trim }}Output "Content".trimLeftandtrimRightThen they are used to clean the left or right.These filters are particularly useful when processing user input, or importing content from external sources that may contain extraneous spaces or specific characters, ensuring the cleanliness of the content.
upper,lower,capfirst,titleFormat uppercase and title case.upperConvert all English characters to uppercase.{{ "anqicms"|upper }}Output "ANQICMS".lower: Convert all characters to lowercase.{{ "ANQICMS"|lower }}Output "anqicms".capfirst: Capitalize the first character of the string.{{ "hello world"|capfirst }}Output "Hello world".title: Capitalize the first letter of each word in a string.{{ "hello world"|title }}Output "Hello World". These filters are very useful when it is necessary to maintain the consistency of content case style, generate standardized titles, or highlight specific text.
truncatecharswithtruncatewords: Smart content truncationtruncatechars:数字: Truncate strings by character count, with an ellipsis (...) for the excess.{{ "一段很长的描述"|truncatechars:5 }}Output a long string: ...truncatewords:数字Truncate string by word count, and replace the overflow part with “…”._htmlSuffix version (such as)truncatechars_htmlIt can safely handle strings containing HTML tags without destroying the HTML structure.They are essential tools for generating article summaries, SEO meta descriptions, card view previews, and more, ensuring that the content is concise and the HTML structure is complete.
splitwithjoinTruncate string to array.split:"分隔符": Split the string into an array according to the specified delimiter.{{ "tag1,tag2,tag3"|split:"," }}Create an array containing "tag1", "tag2", and "tag3".join:"连接符"Concatenate the array elements into a string.{{ 标签数组|join:" - " }}Join array elements with a "-".These filters are very useful for processing strings separated by specific symbols, such as tags and multi-select fields, and are convenient for traversing or recombining in templates.
linebreakswithlinebreaksbrProcess text line breaks.linebreaksTranslate line breaks to HTML in text<p>and<br/>.linebreaksbrOnly translate line breaks to HTML in text<br/>Label. These filters ensure that the plain text content obtained from the backend can be presented in a structured and readable manner on the frontend, avoiding large blocks of text堆积。
urlencodewithurlize: URL processingurlencodePercent-encode URL parameters to ensure the correct transmission of content.urlizeAutomatically recognize URLs or email addresses in text and convert them into clickable links.<a>Label and automatically addrel="nofollow"Property.urlizetruncIt can limit the length of the link text. They are indispensable when you need to convert links in plain text into clickable forms or handle URL parameters.
safewithrenderSafe output and rendering of HTML contentsafeTell the template engine that this variable contains HTML content and does not need to be escaped, output directly. This is crucial for outputting rich text editor content, but be sure to pay attention to XSS risks.render:Especially for the content generated by the Markdown editor in AnQiCMS,renderThe filter can render Markdown text into HTML format.{{ archive.Content|render|safe }}.
Backend bulk replacement vs. template filter: Wise choice
In Anqi CMS, in addition to string replacement at the template level, you may also notice that there is a 'Document Keyword Replacement' feature in the 'Content Management' module on the backend. This is a backend feature for 'site-wide content replacement' and it has essential differences from template filters, each with its own applicable scenarios:
Backend batch replacement:
- Function:Directly modify the original content in the database.
- Application scenarios:Large-scale, permanent content updates, such as complete replacement of old brand names, updating internal links after website domain migration, etc. This will affect all pages that call this field.
- Features:Change is lasting, operate cautiously.
Template filter replacement:
- Function:Modify the display of content dynamically during page rendering without touching the database.
- Application scenarios:A temporary, dynamic replacement that only affects frontend display, such as displaying different information for different user groups, A/B testing, SEO keywords