Possible to disable escaping of content in partial?

I’m working on a partial to generate the rich results (aka: structured data) for all pages in a product site. Each page can have multiple rich result objects (website,webpage, article, course, reviews, faq, etc). Right now I’ve implemented it as one very large partial with a ton of IF-ELSEIF statements… within one big container:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@graph": [
  >>>> big conditional
  ]
}

I’d like to refactor this into multiple partials, one for each type of object, so the usage would be similar to:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@graph": [
    {{- partial "richresults/webpage" (dict .. ) -}}
    {{- if this is a product page...  -}}
      {{- partial "richresults/product" (dict .. ) -}}
    {{- elseif this is a course page...  -}}
      {{- partial "richresults/course" (dict .. ) -}}
    {{- end -}}
  ]
}

These smaller partials generate JSON objects, but they’re getting escaped with quoted newlines breaking the parent object. What I’d like to do is have the partial simply generate raw text… is that possible?

If not, is there another option to achieve what I’m looking to do?

You could build a large, nested dict (ie have the partials generate a dict instead of JSON) and convert that to JSON afterward. Might be easier than assembling JSON from different.
BTW:

There’s no such thing as a “JSON object”. JSON is just a string (representation of an object).

WRT “JSON object”… fair enough… poor choice of words on my part.

The concern I have in the large dict is that it’s very common to have nested objects… which in terms of maintenance, I’m not sure if I’m trading one problem for another.

Which is why I was trying to find a way to get the partial to just return raw text and not escape it.

Here’s what I do in my SEO partial:

    "image": [
      {{ $imageMap := slice }}
      {{ range $.Page.Resources.ByType "image" }}
        {{ $data := dict "@type" "ImageObject" "contentUrl" .Permalink "creator" (dict "@id" $personid )}}
        {{ $imageMap = $imageMap | append $data}}
      {{ end}}
      {{ $imageMap | jsonify | safeJS}}
]

So, it’s some kind of mix between dict/slice and JSON. But then, my LD+JSON is not deeply nested.

Think it was as simple as just marking the output of the partial as safeJS

A partial…

{{- $Image := .featuredImage -}}

{
  "@type": "ImageObject",
  "@id": "{{ (printf "%s#primaryimage" $Image.Permalink) | safeHTML }}",
  "inLanguage": "en-US",
  "url": "{{ $Image.Permalink | absURL }}",
  "width": {{ $Image.Width }},
  "height": {{ $Image.Height }},
  "accessibilityHazard": [
    "noFlashingHazard",
    "noMotionSimulationHazard",
    "noSoundHazard"
  ],
  "accessMode": [
    "textual",
    "visual"
  ]
}

… and usage:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@graph": [

{{/* GLOBAL: add WebPage object */}}
  {{- partial "richresults/webpage" (dict
                                      "siteContext" $Site
                                      "currentPage" $CurrentPage
                                      "featuredImage" $FeaturedImage
                                    ) | safeJS -}}

{{/* GLOBAL: add page image object if defined */}}
  {{- with $FeaturedImage }}
    ,{{- partial "richresults/imageobject" (dict
                                            "image" $FeaturedImage
                                          ) | safeJS -}}
  {{ end -}}
  ]
}

Thanks all!

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.