Using global Scratch map within a Shortcode


#1

The whole post has been revised and thus increased in size. My apologies but I need to add as much information as I have.


Reference repository

A long time ago I needed a way to structure some my Shortcodes into subdirectories because some of them have similar names and after realizing I wouldn’t be able to do so, I created a “master” Shortcode with several conditionals that would direct the flow to a different Partial Template accordingly to the Parameters passed.

Not the best approach but it worked very well so far.

Then, more recently, given the overall complexity theme got along the years, I started dividing each “subsystem” in a set of Partials (i.e SEO, OpenGraph, Schema.org…). As a data-based Template, because index still doesn’t support deep search, until now, several getJSON were used each time, in different Partials to retrieve a different subset of information.

Example: Several of Schema.org’s Entities accepts Thumbnail information. To reduce the file size and improve maintainability, this block of information (ImageObject) has been abstracted into its own Partial, setting a Scratch that can be used whenever it’s needed (1)

Then I decided to give it a try to not use this much of getJSON, reading these files only once per Page and setting a global Scratch in the Header Partial, the earliest I’m aware of. (2)

All went well until I tried to read this data within the Shortcode Templates.

Because the Articles I write were getting too long, hard to maintain with everything in one single MD File, with the help of someone in here, I started componentizing them, creating smaller Content Files and “plugging” them into the Main Page.

This componentization is achieved with this Shortcode.

And then came the problems. If I call this “master” Shortcode from one of the Main Articles, it works fine, but if I call the same “master” Shortcode from one of these “plugged” Content Files, the Scratch doesn’t exist or is empty (can’t say for sure).

Then I tried to isolate the problem in the repository linked, but even though I repeated exactly the same steps, it worked as expected. No changes if compared with my real theme, the same variables and scopes and it simply doesn’t work.

As you may notice along this text, I’ve marked two points:

  1. In the first scenario (1), as far as I can tell, the Scratch is created from the inside and accessed to the outside and as I understood the docs, this is perfectly possible — so much that it works
  2. The second scenario (2), seems to be the opposite. I create it outside (Header) and use it inside (Partial called by a Shortcode) and this *might* not work

Of course, I understand that different themes can react in different ways but since the codes are exactly the same I’m out in the dark here because I don’t know what else could it be.

If you clone/download the repository, /shortcode is what I referred myself to Main Article and /another-page is an exemplary componentizable Page, “plugged” into the Main Article