As an experienced website operations expert, in the practice of AnQiCMS (AnQiCMS), we deeply understand the importance of structured data (Schema.org Markup) for modern SEO.It is not only to make search engines better understand your content, but also to enhance the display form of the website in search results and obtain the key to "rich media abstracts".Today, let's delve deeply into a common problem encountered in the development of Anqi CMS templates:{% jsonLd %}Does the tag supportwithVariable assignment to organize complex Json-LD content?
Of Security CMS{% jsonLd %}Tag: The foundation of structured data
In AnQi CMS,{% jsonLd %}Tags provide us with a powerful ability to embed and manage JSON-LD structured data directly in templates.According to the AnQiCMS design document, the purpose of this tag is very clear: 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 item, which brings us great flexibility.
For example, the most basic one{% jsonLd %}The 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 structured data content that is relatively fixed or less.However, in actual operation, our structured data often needs to be dynamically generated according to page content (such as article titles, product prices, author information, etc.) and may contain complex nested structures.At this point, 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 templates, avoiding repeated calculations or passing complex expressions. ThroughwithVariables defined by the tag are limited to{% with %}and{% endwith %}between, which can carry strings, numbers, and even complex objects returned by other tags.
A typicalwithThe usage scenario of the tag may be like this:
{% with pageTitle="安企CMS深度解析" pageKeywords="AnQiCMS, Json-LD, 结构化数据" %}
<title>{{ pageTitle }}</title>
<meta name="keywords" content="{{ pageKeywords }}">
{% endwith %}
Here, pageTitleandpageKeywordsVariables are defined and used for subsequent output, making the code logic clearer.
Skillfully combined:withwith{% jsonLd %}in-depth collaboration
Then, let's go back to our core question:{% jsonLd %}Does the tag supportwithAssign variables to organize complex Json-LD content?
The answer is affirmative, and this is exactly the practice of building high-quality, maintainable Json-LD structured data!
The template engine of AnQi CMS is processing{% jsonLd %}When the content inside the tag is parsed, it is treated as ordinary template content. This means that as long as you in{% jsonLd %}the closing block of the tag (i.e.,<script type="application/ld+json">The template variable syntax (such as{{ variableName }}) is used inside the tag, the template engine will replace these variables with their corresponding values before outputting.
By{% with %}tags, we can pre-select from{% archiveDetail %}/{% system %}/{% contact %}Extract the required information from the built-in data tags of AnQiCMS, or perform some preliminary data processing, and then assign the processed data towithvariables. Subsequently,{% jsonLd %}The JSON structure inside the tag, directly quote thesewithvariables only.
Let's experience the power of deep collaboration through a specific example: Suppose we need to generate a product article that includes the name, description, image, price, and number of comments for the product.ProductSchema.
{# 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 %}
In this example, we first go through a{% with %}Code block, assign the title, description, image URL, price, and number of comments of the article to easily understandable variables. Then, in{% jsonLd %}Labels inside, these variables are seamlessly embedded in the JSON-LD structure. This way, even if the content of the article changes, we only need to ensurearchive.TitleCorrectly obtaining data fields, Json-LD will automatically update, greatly enhancing the dynamism and maintenance efficiency of the template.
Consideration and **practice in action
- Integration of data sources:
{% with %}The power of tags lies in their ability to aggregate data from different AnQiCMS tags. You can{% archiveDetail %}get article information from{% categoryDetail %}get category information, even from{% system %}and{% contact %}Retrieve the global settings and contact information of the site, then organize them in a block, and finally inject them into Json-LD.withBlock, and finally inject them into Json-LD. - JSON-LD syntax verification:Although AnQiCMS template engine handles variable replacement, the final Json-LD content still needs to strictly follow the Schema.org specification.Make sure to use the Google Structured Data Testing Tool or other online validators to check the validity of the Json-LD you generate, to avoid incorrect parsing by search engines due to syntax errors.
- URL path processing:All URLs (such as
image/urlfields) should usecomplete absolute pathsincludinghttp://orhttps://This is crucial for the proper crawling and understanding by search engines. AnQiCMS'sitem.LinkThe field usually returns a relative path, you may need to use `{% system with name=“BaseUrl”}