How to implement string content replacement in AnQiCMS template?

Calendar 👁️ 67

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.

  1. cutwithtrim:Content cleaning good helper

    • cut: "关键词":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.
  2. 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.
  3. truncatecharswithtruncatewords: Smart content truncation

    • truncatechars:数字: 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.
  4. 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.
  5. 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堆积。
  6. urlencodewithurlize: URL processing

    • urlencodePercent-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.
  7. safewithrenderSafe output and rendering of HTML content

    • safeTell 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

Related articles

How to extract a specified part of a string or list in the AnQiCMS template?

In the daily operation of Anqi CMS, we often encounter scenarios where we need to display content in a refined manner.How to flexibly extract the part we want from a string or list, whether it is an article summary, product parameter list, or a user comment excerpt, is the key to improving user experience and page cleanliness.As an experienced website operation expert, I deeply understand the strength and flexibility of AnQiCMS based on Go language and Django template engine. Today, let's delve into how to accurately extract the specified part of a string or list in the AnQiCMS template.###

2025-11-07

How does the AnQiCMS template handle the escaping and unescaping of HTML content?

As an experienced website operations expert, I am well aware of the subtle balance between content safety and flexible display.In AnQiCMS such an efficient and secure content management system, understanding how its templates handle the escaping and unescaping of HTML content is crucial for building both aesthetically pleasing and secure user experiences.AnQiCMS is developed based on Go language, its template engine inherits many safety design concepts of modern web frameworks when processing HTML, including automatic escaping of HTML content.

2025-11-07

How to set a custom template for the document detail page or category list page of AnQiCMS?

As an experienced website operations expert, I am well aware that the flexibility and customizability of a content management system (CMS) are crucial for the long-term development of the website and the user experience.AnQiCMS (AnQiCMS) provides an excellent solution in this aspect with its powerful functions and ease of use.Today, let's delve into how to set up custom templates for your document detail page or category list page in AnQiCMS, making your website content display more personalized and professional.

2025-11-07

How does the AnQiCMS template support adaptive, code adaptation, and PC+mobile mode?

## AnQiCMS template: How to ride the multi-end content display with wisdom and flexibility?In today's fast-paced digital world, it is common for users to access websites through various devices.As experienced website operators, we know that whether a website can provide a smooth and high-quality experience on PCs, tablets, mobile phones, and other terminals is directly related to user retention and business conversion.

2025-11-07

How to get the current website's system configuration information in AnQiCMS template (such as website name, Logo)?

As an experienced website operations expert, I am happy to discuss with you how to efficiently and flexibly obtain the current system configuration information of the website in AnQiCMS templates.This is not only the foundation of template development, but also the key to ensuring the consistency and maintainability of website information.AnQiCMS is an enterprise-level content management system developed based on the Go language, its concise and efficient design philosophy is also reflected in the use of template tags, making the call of technical information in the template layer extremely intuitive.### Cleverly utilize the `system` tag

2025-11-07

How to get the current page's TDK (Title, Description, Keywords) information in AnQiCMS template?

As an experienced website operation expert, I am well aware that TDK (Title, Description, Keywords) is crucial for the search engine optimization (SEO) of any website, as it is like the facade of the website and a guide for search engines.A meticulously optimized TDK that can make your website stand out in the vast sea of online information, attracting targeted users to click and bringing valuable traffic.

2025-11-07

How does the AnQiCMS template handle arithmetic operations on numbers?

Under the powerful system of AnQi CMS, content operation is far more than just piles of text and images.For modern websites, especially for e-commerce, data display, or scenarios requiring dynamic calculations, the ability to perform numerical operations directly in templates is particularly important.It makes our pages no longer static display boards, but intelligent interfaces that can respond in real-time to data.

2025-11-07

How to implement multilingual content switching and display in AnQiCMS templates?

As an experienced website operations expert, I am happy to delve deeply into the powerful capabilities of AnQiCMS in multi-language content switching and display.In today's increasingly globalized world, making website content accessible to users of different languages has become a key factor in expanding the market and enhancing brand influence for enterprises.AnQiCMS, as an efficient and customizable content management system, naturally it also provides a mature and flexible solution in this regard.

2025-11-07