In the daily operation of websites, we often need to manage the page content in a fine-grained manner, especially in terms of Search Engine Optimization (SEO), structured data plays an increasingly important role.For AnQiCMS users, how to flexibly control and modify various contents on the page, including Json-LD structured data, is a common question.replaceIs the filter suitable for replacing?Json-LDContent in structured data.

Deep understanding of AnQiCMS.replaceFilter

First, let's review AnQiCMS in.replaceThe core function of filters. According to the document description,replaceFilter is a tool for string processing, which is used to replace a specific keyword in a string with another keyword. Its usage is very intuitive:{{obj|replace:"旧关键词,新关键词"}}。比如,如果有一个变量title的值是 “欢迎使用安企CMS”,我们可以通过{{title|replace:"安企,AnQi"}}将其替换为 “欢迎使用AnQiCMS”。

This filter is very powerful when dealing with plain string outputs of text content, links, and even custom fields. It can help us quickly and batch-adjust the text information on the page. However, its operating object isString.

AnQiCMS 中Json-LDThe way to process structured data

Next, let's see how AnQiCMS handlesJson-LDStructured data. In AnQiCMS, structured data is usually processed through{% jsonLd %}...{% endjsonLd %}Labels can be used to define. This tag allows us to embed standard JSON-LD script blocks directly in the template, for example:

{% jsonLd %}
<script type="application/ld+json">
{
	"@context": "https://schema.org",
	"@type": "Article",
	"headline": "{{ archive.Title }}",
	"author": {
		"@type": "Person",
		"name": "AnQiCMS 编辑部"
	},
	"image": [
		"https://en.anqicms.com/uploads/default_thumb.webp"
	]
}
</script>
{% endjsonLd %}

The document clearly states that AnQiCMS will automatically merge and process these custom fields with the system's default structured data. If there is a conflict, the custom fields will override the default values. This indicates{% jsonLd %}Label inside is the JSON structure written by the user, not a single string variable that can bereplaceprocessed by the filter. The system parses this JSONstructure,and use it as<script type="application/ld+json"></script>output the content of the tag.

replaceWhether the filter is suitable forJson-LDthe content?

Based on thereplacefilters andJson-LDThe understanding of the processing method leads us to the conclusion:AnQiCMSreplaceThe filter is not suitable for replacing the entire{% jsonLd %}...{% endjsonLd %}content in the structured data block.

This is becausereplaceThe filter expects an operational string variable as input,{% jsonLd %}The tag wraps a segment of HTML containing JSON format<script>The content of the tag, which is a template,Block(block),instead of something that can be directly operated by a pipelinevariable. You cannot process it like{{ variable | replace:... }}directly in{% jsonLd %}after adding|replace.

Actual operation and suggestion: how to modify effectivelyJson-LDContent

Although we cannot directly apply to the wholeJson-LDblockreplacefilter, this does not mean that we cannot modify it flexiblyJson-LDThe content. In fact, AnQiCMS provides a more structured and flexible way to achieve this goal:

  1. Directly at{% jsonLd %}Definition and override of content within tags:This is the most direct and recommended method. AnQiCMS allows you to{% jsonLd %}define or override any Json-LD field in a block. For example,archive.TitleContains the keywords that need to be replaced, you can directly output the result after replacement here:

    {% jsonLd %}
    <script type="application/ld+json">
    {
    	"@context": "https://schema.org",
    	"@type": "Article",
    	"headline": "{{ archive.Title|replace:'旧名称','新名称' }}", {# 对变量应用replace过滤器 #}
    	"author": {
    		"@type": "Person",
    		"name": "AnQiCMS 编辑部"
    	},
    	"image": [
    		"{{ archive.Logo|default:'https://en.anqicms.com/uploads/default_thumb.webp' }}" {# 假设这里也需要一个默认值或替换 #}
    	]
    }
    </script>
    {% endjsonLd %}
    

    In this example,replaceFilter is applied toarchive.TitleThisvariableon, before it is embedded intoheadlineBefore the field, the content has already been replaced. This fully utilizes the flexibility of the AnQiCMS template engine.

  2. Using the "Full Site Content Replacement" feature (batch replacement) of AnQiCMS backend:AnQiCMS's project advantages mention the 'Full Site Content Replacement' feature, which allows one-click replacement of keywords or links throughout the entire site.If some content in the Json-LD structured data is from the article body, category description, etc., which are editable common fields in the background, and these fields may also be modified by the global content replacement feature, then this global replacement will also indirectly affect the content referred to in Json-LD.replaceFilter.

In summary,replaceFilter is a powerful tool for string variables in AnQiCMS. Although it cannot be applied directly to the entireJson-LDStructured data block, but we can cleverly use it forJson-LDBlock referencesvariableUp, thus achieving precise control and replacement of structured data content.This requires us to understand more clearly the scope of template tags and filters, as well as the logic of AnQiCMS processing structured data.

Common Questions (FAQ)

  1. Why can't I add it directly in{% jsonLd %}...{% endjsonLd %}behind|replaceFilter? {% jsonLd %}is a block-level tag, it is not a variable that returns a string, but an area used to define and output JSON-LD structured data.replaceThe filter needs to act on a specific string variable.Imagine that you can't apply a string replacement filter to the entire code file, you can only replace a specific variable or text within the file.

  2. If myJson-LDThe content is mostly static, only a small part needs to be replaced, what should I do?Even static content, it is recommended to split it into variables or directly in{% jsonLd %}block. If only a small part of the text needs to be replaced, you can define this part of the text as a template variable, and then use this variable where the variable is needed (i.e., in theJson-LDStructure inside applicationreplacefilter. For example:"description": "{{ static_description_variable|replace:'旧词','新词' }}".

  3. ExceptreplaceFilter, AnQiCMS does it have other ways to batch or automate the modificationJson-LDContent in the structure?Except in{% jsonLd %}Using variables within the tag internalreplaceFilter performs local modifications, AnQiCMS also provides the background "Full Site Content Replacement" feature (inhelp-content-list.md提及的“文档关键词替换”)。This feature can batch replace keywords or links in all document content, if your Json-LD is dynamically extracted or referenced from these document contents, then the full-site replacement will also indirectly affect Json-LD.In addition, custom development is the ultimate solution, as AnQiCMS is developed in Go language and has a modular design, which allows for more flexible handling and generation of Json-LD data on the backend logic level.