In the flexible content management system of AnQiCMS, customizing content model fields is the core of personalized content display.However, sometimes the data we obtain from these custom fields may not always be presented in the format we expect.replaceFilter becomes an indispensable tool, which can help us perform precise search and replace on strings when outputting content, thereby achieving more refined content control and formatting.
UnderstandingreplaceThe core function of the filter
replace
Its basic syntax is very intuitive:{{ obj|replace:"旧字符串,新字符串" }}
Here are several key points to note:
obj: This is the target string you wish to replace, which can be any value obtained from a custom content model field, such asarchive.customField.replaceThis is the name of the filter."旧字符串,新字符串":This isreplaceThe parameters received by the filter, one separated by a comma.,The string is separated. The left side of the comma is the text you need to find and replace, and the right side is the new text to replace with. Please note that this parameter needs to be enclosed in double quotes.
replaceThe filter also shows flexibility when handling some special cases:
- If the 'old string' is emptyThe filter will insert 'new string' at the beginning of the target string and after each UTF-8 character sequence.
- If 'new string' is emptyThe filter will remove all matched "old string" from the target string.
For example, if we have a string欢迎使用安企CMS,and hope to replace 'AnQi' with 'AnQi'{{"欢迎使用安企CMS"|replace:"安企,AnQi"}}The output will be:欢迎使用AnQiCMS
If you want to remove the word 'AnQi':{{"欢迎使用安企CMS"|replace:"安企,"}}The output will be:欢迎使用CMS
Actual application in the custom content model field
replaceThe filter plays an important role here.
Suppose we created a content model named "product" and added a custom field, called the field nameproduct_featuresEnglish for storing product feature descriptions. We may also have onecontact_phonefield for storing phone numbers.
Get custom field values In the template, you can use:
archive.你的自定义调用字段名Get the value of the custom field directly. For example:{{ archive.product_features }}. Or, if you are on the document details page, you can also use{% archiveDetail with name="你的自定义调用字段名" %}you can get it by tag.Combine
replacefilter processing: Once the field value is obtained, it can be used directly asobjApplyreplaceFilter.Case one: Brand name in the unified product feature descriptionAssume
product_featuresThe content of the field is 'This product uses AnQiCMS technology and provides powerful content management functions.' If we want to unify the brand name to 'AnQiCMS', we can do it like this:{{ archive.product_features|replace:"安企CMS,AnQiCMS" }}So, when displayed on the front end, 'AnQiCMS' will be uniformly replaced with 'AnQiCMS', and the original data in the database will remain unchanged.Case two: Clean up phone number formatIf
contact_phoneField stores phone numbers in inconsistent formats, such as "138-0013-8000" or "139 1234 5678", we hope to display a pure number phone on the front end so that users can copy or click to dial:{{ archive.contact_phone|replace:"-, "|replace:" , " }}Here used chain operation, first to use the hyphen-Replace with empty, then replace spaces with empty. This will always result in a pure numeric phone number regardless of the original format.Example Three: Remove Specific Placeholders in Custom FieldsSometimes, custom fields may contain some placeholders or tags for internal use that should not be displayed to the end user. For example,
notesField may contain[INTERNAL_ONLY]such tags:{{ archive.notes|replace:"[INTERNAL_ONLY]," }}This will directly remove all[INTERNAL_ONLY]text to make the content more concise.
Practical skills and precautions
- 链式操作:AnQiCMS 的过滤器支持链式调用,意味着你可以在一个输出中连续应用多个过滤器,处理顺序是从左到右。例如:
{{ obj|filter1:param1|filter2:param2 }}This is very useful for complex text processing, as shown in the example of cleaning up phone numbers above. - With
safefilter combined withIfreplaceThe content of the replacement may contain HTML tags, and if you want these tags to be parsed normally by the browser instead of being escaped into entities, you need to use them in conjunction.safefilter. For example:{{ archive.rich_text|replace:"<old_tag>,"|safe }}This will ensure that the replaced HTML structure is rendered correctly. - Differentiate global replacement and template layer replacementAnQiCMS backend provides the core feature of 'Full Site Content Replacement', which is batch modification of content at the database level.
replaceThe filter is completely dynamic processing at the time of template rendering and does not affect the original data in the database. Both have their own focuses, and the use should be selected according to actual needs. The template layer,replaceThe filter is more suitable for temporary formatting and standardization of display, without involving changes to the content source. - Case sensitive:: Usually,
replaceThe filter is case-sensitive when searching for the "old string", so it is necessary to match exactly when defining the replacement rules. - Thorough testing: Applied in the template
replaceAfter the filter, it is essential to test under different custom field values and expected output scenarios to ensure the replacement effect meets expectations.
By using flexibilityreplaceFilter, AnQiCMS users can effectively control the output format of custom content model fields, improve the display quality and user experience of website content, while maintaining the original integrity of background data, bringing great convenience to website operation.
Common Questions (FAQ)
Q1:replaceWhat are the differences between the 'Site-wide Content Replacement' feature of the filter and AnQiCMS backend?A1:replaceThe filter is a template-level tool that searches and replaces data during page rendering, but does not modify the original content stored in the database.The replacement is executed each time the page is loaded.