Hello I am trying to add JSON-LD schema to each page in sections: events, services, and blog
Each of those sections is a branch bundle on my site. I am wondering what might be a good way to implement the structured data for each page within each section.
Is there a way to store the schema as a branch bundle resource and populate it on render? Should I have one conditional in the baseof based on section? Are you just storing structure as a string and populating with printf then safeHTML? Also I am curious about your use of blocks vs partials?
I am curious to know how you are handling structure data.
Events is a section, services is a section. So you can filter by .Section when you range over the page object and then create based on that the JSON markup from the page object.
Basically, at the end of your <article> tag (I think you might have that if you are using other standards like json-ld too) add a partial like this:
{{ partialCached "schema.html" . . }}
This will cache the output once per page (second .) and receive the page as context (first .). Then in layouts/partials/schema.html you do a check which section you are in and create your schema:
{{ if eq .Section "events" }}
.... events schema
{{ else if eq .Section "services" }}
... services schema
{{ else }}
.. all others
{{ end }}
You might have templates per section, in that case you don’t need to check for the section, just add in a partial. I would keep it out of baseof template and put it into a partial.
Also, work with a dict in your partial and then at the end transform into json via jsonify - that saves some encoding troubles.
I have some rudimentary schema partials here for inspiration. Check the end - jsonify, then safe.JS. That way your output looks good and nothing is scrambled.