In the daily operation of websites, we often need to manage the content of pages in a fine-grained manner, especially in the field 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.Today, let's discuss AnQiCMS'sreplaceIs the filter applicable for replacementJson-LDThe content in structured data.
Deep understanding of AnQiCMS.replaceFilter
First, let's review what is in AnQiCMS.replaceThe core function of the filter. According to the document, replaceA filter is a tool used for string processing, its function is to replace a specific keyword in a string with another keyword. Its usage is very intuitive:{{obj|replace:"旧关键词,新关键词"}}. For example, if there is a variabletitleThe value is 'Welcome to AnQiCMS', we can replace it by{{title|replace:"安企,AnQi"}}Welcome to AnQiCMS.
This filter is very powerful when handling text content, links, and even custom field pure string outputs, and can help us quickly and batch adjust the text information on the page. However, its operation object isstring.
AnQiCMS inJson-LDThe way of processing structured data
Next, let's see how AnQiCMS handlesJson-LDStructured data. In AnQiCMS, structured data is usually processed through{% jsonLd %}...{% endjsonLd %}This tag defines. 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 the processing of these custom fields with the default structured data, and if there is a conflict, the custom fields will override the default values. This indicates{% jsonLd %}The tag contains 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>Label content output.
replaceIs the filter applicable toJson-LDcontent?
Based on the understanding ofreplaceFilters andJson-LDthe handling method, we can draw the conclusion that:AnQiCMS'replaceThe filter is not applicable to directly replace the entire{% jsonLd %}...{% endjsonLd %}content within a structured block.
This is becausereplaceThe filter expects an operable string variable as input, whereas{% jsonLd %}A tag wraps a section containing HTML formatted as JSON<script>The tag content, it is a template ofblock(block), not something that can be directly piped through a pipeline.Variable. You cannot process like{{ variable | replace:... }}that, directly in{% jsonLd %}Add after|replace.
Actual operation and suggestions: How to effectively modifyJson-LDcontent
Although you cannot directly modify the entireJson-LDblock usagereplaceThe filter, but 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:
directly in
{% jsonLd %}Define and override content within tags:This is the most direct and recommended way. AnQiCMS allows you to{% jsonLd %}Block defines or overrides any Json-LD field. For example, ifarchive.Titlecontains keywords that need to be replaced, you can directly output the replacement result 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,
replaceThe filter is applied toarchive.TitleThisVariableAbove, before it is embedded intoheadlinethe content has already been replaced in the field. This fully utilizes the flexibility of the AnQiCMS template engine.Using the AnQiCMS backend "Full Site Content Replacement" feature (batch replacement):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 of the content in the Json-LD structured data comes from the article body, category description, and other background editable common fields, and these fields may also be modified by the full-site content replacement feature, then this global replacement will also indirectly affect the content referenced in Json-LD.Please note, this is a backend feature, not a template level feature
replacefilter.
In summary,replaceThe filter is a powerful tool in AnQiCMS for string variables. Although it cannot be applied directly to the entireJson-LDStructured data block, but we can cleverly use it forJson-LDblock-internal referencesVariableUp, thus enabling precise control and replacement of structured data content.This requires us to have a clearer understanding of the scope of template tags and filters, as well as the logic of AnQiCMS handling structured data.
Frequently Asked Questions (FAQ)
Why can't I directly use in
{% jsonLd %}...{% endjsonLd %}add|replaceFilter?{% jsonLd %}It is a block-level tag, it is not a variable that returns a string, but a region used to define and output JSON-LD structured data.replaceThe filter needs to act on a specific string variable. Imagine that you cannot apply a string replacement filter to the entire code file, you can only replace a variable or specific text within the file.If my
Json-LDMost of the content is static, only a small part needs to be replaced, what should I do?Even static content is recommended to be split into variables or directly written in{% jsonLd %}block is written. 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 the variable where it is needed (i.e., in the template itself)Json-LDStructure internally) applicationreplacea filter. For example:"description": "{{ static_description_variable|replace:'旧词','新词' }}".except
replaceFilter, does AnQiCMS have other ways to batch or automate modificationJson-LDContent in the structure?Except in{% jsonLd %}Using the referenced variable inside the tag internal toreplaceThe filter performs local modifications, AnQiCMS also provides the background "Full Site Content Replacement" feature (athelp-content-list.mdDocument keyword replacement is mentioned. This feature can replace all keywords or links in the document content in bulk. If your Json-LD is dynamically extracted or referenced from these document contents, the site-wide replacement will 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 logical level.