A
A
Alexey2018-02-15 07:06:51
Twig
Alexey, 2018-02-15 07:06:51

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>

update:*************************
Continuing the experiments, I did this - {{ data|json_encode(description_short) }} . This did not cause a 500 error, but now it displays a warning in this field - Warning: json_encode() expects parameter 2 to be integer, string given in /system/library/template/Twig/Extension/Core.php on line 664

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2018-02-15
@AlexSF

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 question

Ask a Question

731 491 924 answers to any question