Answer the question
In order to leave comments, you need to log in
How to pass data from twig variable to ld+json?
Good day!
I'm trying to attach micro-markup application/ld+json to the opencart-3 product card with a modifier.
And everything seems to work, but only until the {{ description_short }} variable is included in the text of which there are "(quotes).
How to escape the text of the twig variable - {{ description_short }}, to display it in application/ld+json ?
<file path='catalog/view/theme/*/template/product/product.twig'>
<operation error="skip" info=''>
<search><![CDATA[
<div class="breadcrumbs">
]]></search>
<add position='before'><![CDATA[
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "{{heading_title}}",
"image": "{{popup}}",
"brand": "{{ manufacturer }}",
"description": "{{ description_short }}",
"offers": {
"@type": "Offer",
"priceCurrency": "BYN",
{% if price %}"price": "{{ price|trim(' р.') }}",
{% else %}"price": "{{ special|trim('р.') }}",{% endif %}
"itemCondition" : "http://schema.org/NewCondition",
"availability" : "{{ stock }}"
}
}
</script>
]]></add>
</operation>
</file>
Answer the question
In order to leave comments, you need to log in
The solution was found in the next study of the twig documentation - Filter escape .
{{ description_short|escape}}
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<code>microdata_json</code>
<name><![CDATA[Microdata in JSON OC 3.0.x]]></name>
<version><![CDATA[1.0]]></version>
<author><![CDATA[Alex]]></author>
<date><![CDATA[2018.02.01]]></date>
<file path='catalog/view/theme/*/template/product/product.twig'>
<operation error="skip" info=''>
<search><![CDATA[
<div class="breadcrumbs">
]]></search>
<add position='before'><![CDATA[
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "{{heading_title}}",
"image": "{{popup}}",
"brand": "{{ manufacturer }}",
"description": "{{ description_short|escape }}",
"offers": {
"@type": "Offer",
"priceCurrency": "BYN",
{% if price %}"price": "{{ price|trim(' р.') }}",
{% else %}"price": "{{ special|trim('р.') }}",{% endif %}
"itemCondition" : "http://schema.org/NewCondition",
"availability" : "{{ stock }}"
}
}
</script>
]]></add>
</operation>
</file>
<file path='catalog/view/theme/*/template/common/header.twig'>
<operation error="skip" info=''>
<search><![CDATA[
</head>
]]></search>
<add position='before'><![CDATA[
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Store",
"name": "Название сайта",
"image": "ссыль на лого",
"@id": "ссыль на главную сайта",
"url": "ссыль на главную сайта",
"telephone": "+телефон",
"priceRange": "от 10 р. до 2 700 р.",
"address": {
"@type": "PostalAddress",
"streetAddress": "адрес",
"addressLocality": "город",
"postalCode": "212029",
"addressCountry": "BY"
},
"geo": {
"@type": "GeoCoordinates",
"latitude": 53.865026,
"longitude": 30.3307494
},
"openingHoursSpecification": {
"@type": "OpeningHoursSpecification",
"dayOfWeek": [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
],
"opens": "09:00",
"closes": "21:00"
}
}
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "WebSite",
"name": "Название сайта",
"url": "ссыль на главную сайта"
}
</script>
]]></add>
</operation>
</file>
</modification>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question