In the flexible content management system of AnQiCMS, custom content model fields are the core of personalized content display.However, sometimes the data we retrieve from these custom fields may not always be in the format we expect.At this time, the AnQiCMS template engine providesreplaceThe filter has become an indispensable tool, which can help us perform precise search and replace on strings during content output, thus achieving more refined content control and formatting.
UnderstandingreplaceThe core function of the filter
replaceA filter, as the name implies, is mainly used to find a specific "old string" in a string and replace it with the specified "new string", and finally return the processed new string.This process occurs during template rendering and does not modify the original content in the database.
Its basic syntax is very intuitive:{{ obj|replace:"旧字符串,新字符串" }}
There are several key points to note:
objThis is the target string you want to replace, it can be any value obtained from a custom content model field, for examplearchive.customField.replaceThis is the name of the filter."旧字符串,新字符串"This isreplaceThe filter receives parameters, one separated by a comma,The string is separated. The text on the left side is the one you need to find and replace, and the new text on the right side. Please note that this parameter needs to be enclosed in double quotes.
replaceThe filter also performs well in handling some special cases:
- If the 'old string' is empty: The filter will insert 'new string' at the beginning of the target string and after each UTF-8 character sequence.
- If 'new string' is empty.: The filter will remove all matched "old string" from the target string.
For example, if we have a string欢迎使用安企CMSAnd hope to replace "AnQi" with "AnQi", the code will be like this: {{"欢迎使用安企CMS"|replace:"安企,AnQi"}}The result will be:欢迎使用AnQiCMS
If you want to remove the word '安企':{{"欢迎使用安企CMS"|replace:"安企,"}}The result will be:欢迎使用CMS
The actual application in the custom content model field
The AnQiCMS content model allows us to define various custom fields for different content types (such as articles, products), such as "product features", "contact phone", "summary", etc.When the content of these fields is displayed on the front end, we may need to format or standardize them.replaceThe filter can play an important role here.
Assuming we created a content model named "product" and added a custom field calledproduct_featuresUsed to store product feature descriptions. We may also have onecontact_phonefield to store phone numbers.
Get custom field value: In the template, you can go through
archive.你的自定义调用字段名Directly get the value of the custom field. For example:{{ archive.product_features }}Or, if you are on the document detail page, you can also use{% archiveDetail with name="你的自定义调用字段名" %}tags to obtain.Combine
replaceFilter processingOnce 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 this:{{ archive.product_features|replace:"安企CMS,AnQiCMS" }}This will be unified replaced with "AnQiCMS" when displayed on the front-end, while the original data in the database remains unchanged.Example two: Clean phone number formatIf
contact_phonePhone numbers are stored in inconsistent formats, such as “138-0013-8000” or “139 1234 5678”, and we want to display a pure number phone on the front-end so that users can copy or click to dial:{{ archive.contact_phone|replace:"-, "|replace:" , " }}This uses chained operations, first to convert the hyphen-Replace spaces with nothing. This will always result in a pure numeric phone number regardless of the original format.Case three: Remove specific placeholders from custom fieldsSometimes, custom fields may contain some internal placeholders or markers that are not intended to be shown to the end user. For example,
notesFields may contain[INTERNAL_ONLY]such tags:{{ archive.notes|replace:"[INTERNAL_ONLY]," }}This will directly remove all[INTERNAL_ONLY]text, making the content simpler.
Practical skills and precautions
- Chaining operations: AnQiCMS's filter supports chained calls, meaning you can apply multiple filters consecutively in an output, with the order of processing from left to right. For example:
{{ obj|filter1:param1|filter2:param2 }}This is very useful for complex text processing, such as the example of cleaning up phone numbers mentioned above. - with
safeFilter combinationIf:replaceThe content may contain HTML tags, and if you want these tags to be parsed by the browser normally instead of being escaped as entities, you need to use them in conjunction withsafea filter. For example:{{ archive.rich_text|replace:"<old_tag>,"|safe }}This will ensure that the replaced HTML structure is rendered correctly. - Distinguish between global replacement and template layer replacement: AnQiCMS backend provides the core function of "full site content replacement", which is to batch modify content at the database level. And
replaceThe filter is completely dynamic processing during template rendering, which will not affect the original data in the database. Both have their own focuses, and they should be chosen according to actual needs. The template layer'sreplaceThe filter is more suitable for temporary formatting and standardization display without involving changes to the content source. - Case sensitive:
replaceThe filter is case-sensitive when searching for the - Thorough testing: Applied in the template
replaceAfter the filter, be sure to test in different custom field values and expected output scenarios to ensure the replacement effect meets expectations.
By flexible applicationreplaceFilter, AnQiCMS users can more effectively control the output format of custom content model fields, improve the quality of website content display and user experience, while maintaining the original integrity of background data, bringing great convenience to website operation.
Frequently Asked Questions (FAQ)
Q1:replaceWhat is the difference between the filter and the "Full Site Content Replacement" feature of the AnQiCMS backend?A1:replaceThe filter is a template-level tool that performs search and replace on data during page rendering, but it does not modify the original content stored in the database.The replacement is executed every time the page is loaded. The background "Full Site Content Replacement" function is to batch modify the content in the database, once