In the flexible template system of AnQiCMS, dynamic content processing is an indispensable part of daily operation.Sometimes, we may need to replace a specific keyword in a string when displaying content, such as displaying a unified brand name consistently, or adjusting the presentation of certain text without modifying the original data.AnQiCMS template engine provides a simple and efficient method to complete this task, among whichreplaceThe filter is our powerful assistant.

UnderstandingreplaceFilter

replaceThe filter is a practical tool in AnQiCMS template language, which allows you to find an 'old keyword' in a string and replace it with a 'new keyword'.This process is entirely performed during template rendering, it does not touch the original data in the database, thus providing you with great flexibility to make various adjustments to the display layers without affecting the content source.

Its basic usage is very intuitive, you can input the string that needs to be processed and then pass it through the pipe|InvokereplaceFilter, and specify the "old keyword" and "new keyword" in a comma-separated manner.

For example, if you have a variable named文章标题The variable contains “Welcome to AnQiCMS”, and you want to replace it with a more brand-sensitive “AnQiCMS”, you can write the template code like this:

{{ 文章标题|replace:"安企CMS,AnQiCMS" }}

After such processing, the content displayed on the page will become "Welcome to AnQiCMS".

replaceThe filter has some special behaviors to note. If the old keyword is set to empty, it will match at the beginning of the string and after each UTF-8 character sequence, which is usually used to insert content between characters.And if the new keyword is set to empty, then the old keyword will be removed.For example, if you want to remove “AnQiCMS” and leave out “AnQi”, you can write it like this:{{ "安企CMS"|replace:"安企," }}The result will be "CMS".

Actual application scenarios and examples

In daily website operations,replaceFilters can be applied to various scenarios:

  1. Standardize brand or term names:When your company name, product name, or industry terminology changes over different periods or needs to be unified in a specific style on a particular page, this filter becomes particularly important. Assuming your article detail page mentions 'AnQi Content Management System', you want to display it as 'AnQiCMS'.

    <p>{{ archive.Description|replace:"安企内容管理系统,AnQiCMS" }}</p>
    
  2. Adjust the display style of URLs or links:Sometimes, the URL you get from the backend may contain the completehttp://orhttps://Prefix, and you want to display only the domain part on the page.

    <a href="{{ item.Link }}">{{ item.Link|replace:"https://,"|replace:"http://," }}</a>
    

    Here, we used it twice in a row.replaceFilter, removed first.https://Then removed again.http://To ensure that any protocol can be displayed uniformly.

  3. Format the separators in the content:If the content output of a field is a date string, for example2023-10-26It shows as you wish2023/10/26,replaceThe filter can help you quickly convert

    <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02")|replace:"-","/" }}</span>
    

    This example combinesstampToDateThe filter first formats the timestamp into a date string, thenreplaceFilter and replace the separators in it.

  4. Simple content correction on the display level:Although it is more recommended to correct the original content in the background, but for some minor typos or temporary content adjustments that do not affect the core data,replaceThe filter can also provide immediate display corrections.

    <h1>{{ archive.Title|replace:"错别字,正确词" }}</h1>
    

Use with other filters

replaceFilters can be used with other filters to achieve more powerful functionality.

  • Process strings containing HTML:It is very important to use when you need to replace content that includes HTML tags (such as the output of a rich text editor article content)|safefilter.AnQiCMSThe template engine defaults to escaping the output HTML content to prevent XSS attacks. If you directly apply HTML contentreplacewithout adding|safe, the browser may display the HTML tags in your replacement result as plain text.

    <div class="article-content">
        {{ archive.Content|replace:"旧的HTML内容,新的HTML内容"|safe }}
    </div>
    
  • To perform case-insensitive replacement:If the case of the old keyword is uncertain, you can first use|loweror|upperThe filter converts the string to uppercase or lowercase and then replaces it.

    {{ item.Title|lower|replace:"cms,内容管理系统" }}
    

    This will be replaced with 'Content Management System' regardless of whether the original title is 'CMS' or 'cms'.

Important reminder

UsereplaceWhen using a filter, it is necessary to clarify its scope. It is simply to modify the output string during template rendering.It is a modification at the display level.It will not change the original content stored in the database. If you need to make large-scale, permanent content changes, such as updating the brand words of the entire site, then you should use the 'Global Content Replacement' feature provided by AnQiCMS backend (which can be found in the 'Content Management' module), it is a batch operation executed on the backend, directly modifying the content in the database, which is also more SEO-friendly.

Moreover, althoughreplaceThe filter is very convenient, but it also needs to consider potential performance impacts when processing very large strings or using it frequently in loops.Moderate use, combined with backend features, can help you better manage and display website content.


Frequently Asked Questions (FAQ)

1.replaceCan the filter implement batch replacement of content throughout the site?No.replaceThe filter is provided by the AnQiCMS template engineFront-end display layerThe tool, it only temporarily modifies the display of the string when the content is rendered on the page.To implement batch modification of all site content (i.e., directly changing the data in the database), you need to use the "Full Site Content Replacement" function in the AnQiCMS backend.

2. I used it in the templatereplaceFilter, but the front-end page does not change, what could be the reason?This usually has several reasons. First, check if your template syntax is correct, including comma separators, quotes, and pipes|The use. Next, confirm whether the template file you modified is the template file currently being used by the website, and whether the AnQiCMS system cache has been cleared after modification.Sometimes browser cache may also cause you not to see the latest changes, try clearing the browser cache or access in incognito mode.

3.replaceDoes the filter support regular expressions for more complex replacements?AnQiCMS template inreplaceThe filter currently does not support regular expressions. It performs simple string matching replacement.If you need to use regular expressions for advanced matching and replacement and want to modify the original data, you should consider using the 'Document Keyword Replacement' feature in AnQiCMS backend, which supports batch replacement using regular expression rules.