As a senior website operation expert, in the practice of AnQiCMS (AnQiCMS), we fully understand the importance of structured data (Schema.org Markup) for modern SEO.It is not only to help search engines better understand your content, but also the key to improving the display form of the website in search results and obtaining 'rich media summary'.{% jsonLd %}Label support?withVariable assignment, to facilitate the organization of complex Json-LD content?

Anqi CMS's{% jsonLd %}Label: the cornerstone of structured data

In the Anqi CMS,{% jsonLd %}Tags provide us with a powerful ability to embed and manage JSON-LD structured data directly in templates.The purpose of this tag is very clear according to the AnQiCMS design document: it allows developers to insert custom JSON-LD content, and the system will intelligently merge these custom contents with the structured data generated by AnQiCMS by default.If there is a conflict, your custom content will take precedence over the default items, which brings us extremely high flexibility.

For example, a most basic{% jsonLd %}Label usage may be like this:

{% jsonLd %}
<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "WebPage",
  "name": "安企CMS官网",
  "url": "https://en.anqicms.com"
}
</script>
{% endjsonLd %}

This method is simple and clear, suitable for cases where the structured data content is relatively fixed or less.However, in actual operation, our structured data often needs to be dynamically generated based on page content (such as article titles, product prices, author information, etc.) and may contain complex nested structures.This is when, if all the content is hardcoded in the JSON string, not only will the code become long and difficult to maintain, but it will also greatly reduce the reusability of the template.

withLabel: Flexible definition of template variables

The Anqi CMS template engine (based on Django template engine syntax) provides{% with %}Label, this is a very practical tool used to declare and assign variables temporarily in templates.Its main purpose is to improve the readability and maintainability of the template, avoiding repetitive calculations or passing complex expressions.withLabel-defined variables, whose scope is limited to{% with %}and{% endwith %}between, which can carry strings, numbers, or even complex objects returned by other tags.

a typicalwithThe usage scenario of the tag may be as follows:

{% with pageTitle="安企CMS深度解析" pageKeywords="AnQiCMS, Json-LD, 结构化数据" %}
  <title>{{ pageTitle }}</title>
  <meta name="keywords" content="{{ pageKeywords }}">
{% endwith %}

Here,pageTitleandpageKeywordsThe variable is defined and used for subsequent output, making the code logic clearer.

Smart combination:withWith{% jsonLd %}deep collaboration

Then, let's return to our core issue:{% jsonLd %}Label support?withvariable assignment to organize complex Json-LD content?

The answer is affirmative, and this is precisely the **practice** of building high-quality, maintainable Json-LD structured data!

The AnQi CMS template engine handles{% jsonLd %}The content within the tag, when it is, is treated as ordinary template content for parsing. This means that as long as you in{% jsonLd %}the closing block of the tag (i.e.<script type="application/ld+json">Tag inside)used legal template variable syntax(such as{{ variableName }}),the template engine will replace these variables with their corresponding values before outputting.

Pass{% with %}tags, we can pre-specify from{% archiveDetail %}/{% system %}/{% contact %}Extract the required information from the AnQiCMS built-in data tags or perform some preliminary data processing, and then assign these processed data towith.{% jsonLd %}Label the JSON structure inside, directly refer to thesewithvariables.

Let us feel the power of this deep collaboration through a specific example: Suppose we need to generate a product article that includes name, description, image, price, and the number of comments.ProductSchema. English.

{# 1. 使用with标签定义变量,从文章详情中提取所需数据 #}
{% with 
    productName=archive.Title 
    productDescription=archive.Description 
    productImage=archive.Logo 
    productPrice="99.99" {# 假设价格字段为字符串,或从自定义字段中获取 #}
    reviewCount=archive.CommentCount 
    productUrl=archive.Link 
    currency="CNY"
%}

{% jsonLd %}
<script type="application/ld+json">
{
  "@context": "https://schema.org/",
  "@type": "Product",
  "name": "{{ productName }}",
  "image": "{{ productImage }}",
  "description": "{{ productDescription|truncatechars:150 }}", {# 可以对描述进行截断处理 #}
  "sku": "AQ-CMS-001", {# 示例SKU #}
  "offers": {
    "@type": "Offer",
    "url": "{{ productUrl }}",
    "priceCurrency": "{{ currency }}",
    "price": "{{ productPrice }}",
    "itemCondition": "https://schema.org/NewCondition",
    "availability": "https://schema.org/InStock"
  },
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "4.5", {# 示例评分,实际应动态获取 #}
    "reviewCount": "{{ reviewCount }}"
  }
}
</script>
{% endjsonLd %}

{% endwith %}

在这个例子中,我们首先通过一个 English.{% with %}The code block assigns the title, description, image URL, price, and number of comments of the article to easily understandable variables. Then,{% jsonLd %}Label within, these variables are seamlessly embedded into the JSON-LD structure. In this way, even if the content of the article changes, we only need to ensurearchive.TitleThe data fields can be correctly retrieved, Json-LD will automatically update, greatly enhancing the dynamism and maintenance efficiency of the template.

Consideration and **practice in practice

  1. Integration of data sources: {% with %}The power of the label lies in its ability to aggregate data from different AnQiCMS labels. You can get{% archiveDetail %}article information from{% categoryDetail %}category information, even from{% system %}and{% contact %}Get the global settings and contact information of the site, and then unify it inwiththe block, and finally inject it into Json-LD.
  2. JSON-LD syntax verification:Although AnQiCMS's template engine handles variable replacement, the final Json-LD content still needs to strictly follow the Schema.org specification.Please make sure to use the Google Structured Data Testing Tool or other online validators to check if the Json-LD you generate is valid, to avoid incorrect parsing by search engines due to syntax errors.
  3. URL path processing:Json-LD中的所有URL(如image/url字段)都应使用The complete absolute path,http://orhttps://。这对于搜索引擎的正确抓取和理解至关重要。AnQiCMS的item.LinkThe field will usually return a relative path, you may need to use `{% system with name=“BaseUrl”}