Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hello,
I am writing Schema markup for various pages on my website, a lot of which are built using sections.
According to Shopify documentation, I understand that variables within a section are not accessible beyond that section. I also understand that those variables do become dynamically accessible to snippets rendered within the same section block.
I am looking for a work around. Is there any possible way to pull dynamic values from a section block to place into a snippet?
I have created a snippet that contains JSON LD schema markup and am hoping I can find a way to pull dynamic values from various section blocks into this snippet. If this was possible, my Schema markup could be nested, making it easy for search engines to understand.
As it is now, I have to render each pertinent schema snippet into the section block in order for the dynamic values to be available. The output of this is several different schema markups for one page, which makes it more difficult for search engines to understand the focus of the page. If I could only render a schema snippet singularly per page, it would make my page much more understandable. I realize this is easily possible of the text is static. But that's not why I'm here!
How can I make dynamic values from sections available to an overarching snippet?
Solved! Go to the solution
This is an accepted solution.
Basically, all variables inside section are scoped to this section.
So you can't define variable in one section and use it in another.
Section blocks, however, are not scoped, so whatever you define in a section block will be visible throughout entire section in all blocks.
Snippets, when they are rendered (rather then included) are also scoped and see no variables from parent snippet/section/template. Unless you pass them as snippet parameters explicitly.
The entire idea behind scoping is to render sections and snippets independently in parallel to improve rendering speed.
If you include a snippet in a section, it will have access to all variables defined in this section.
As to your concern -- it's fine to have multiple JSON-LD occurrences on the same page, you'd just need to link them together by using @context and @Id :
See https://github.com/Shopify/dawn/pull/3380#issuecomment-2074894230 and https://www.w3.org/TR/json-ld/#advanced-context-usage.
Hello @Jim_Borchardt
Why don't you use this simple FREE SEO shopify app for schema related requirements
https://apps.shopify.com/spider-website-seo-optimizer
This is an accepted solution.
Basically, all variables inside section are scoped to this section.
So you can't define variable in one section and use it in another.
Section blocks, however, are not scoped, so whatever you define in a section block will be visible throughout entire section in all blocks.
Snippets, when they are rendered (rather then included) are also scoped and see no variables from parent snippet/section/template. Unless you pass them as snippet parameters explicitly.
The entire idea behind scoping is to render sections and snippets independently in parallel to improve rendering speed.
If you include a snippet in a section, it will have access to all variables defined in this section.
As to your concern -- it's fine to have multiple JSON-LD occurrences on the same page, you'd just need to link them together by using @context and @Id :
See https://github.com/Shopify/dawn/pull/3380#issuecomment-2074894230 and https://www.w3.org/TR/json-ld/#advanced-context-usage.
Hey Tim, thanks so much for your reply. It was very helpful. Never thought about it from the angle mentioned in your last sentence. That is how I will execute.
Hey Tim, quick question. I'm hung up a bit on the particulars:
Does the @Id value used across the various schemas need to be of the exact same value?
This:
@type CollectionPage
@Id /collections/jacket
@type ImageObject
@Id /collections/jacket
Or this:
@type CollectionPage
@Id /collections/jacket#CollectionPage
@type ImageObject
@Id /collections/jacket#JacketGallery
You can have multiple objects represented on the same page. Say, you can have your company object, product object, recipe, articles...
If you have several occurrences for, say @type Product, they will be recognised as multiple products, each missing some data.
That's why you need to use the same @Id to tell the parsers to combine both jsons into single object.
So, if we're talking about collection page and image is the collection image, I'd be leaning toward the first of your codes. As in the github link above.
Finally, you can always feed your data to a testing tool https://developers.google.com/search/docs/appearance/structured-data to see what results it will produce.
Thanks Tim. Yes, I have different objects in each snippet. For example. I have a collection page that contains a collection, an image with text section, a gallery, and lastly a video.
I have created Schema markup for the collection page, and for all the subsections. Each section is assigned its own object. Thus my question regarding @Id. Each section has a standalone purpose, but is related to every other section. Priority overall is to ensure that search engines recognize that each markup contribute to the collection page as a whole.
Sounds like I can get away with using the main url as the @Id followed by a tag particular to the section
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024