Passing local data variables between layout and template

I need to be able to share local data (in a yaml file) from templates with a layout. I want to be able to load relevant YAML data in the template then have that data be accessible from the layout (and associated partials). Is this possible?

I found this blog post which seems to indicate that this is possible through some shennaigans but I can’t get it to work and this seems hacky. Is there a built in way to pass data between templates and layouts? And if not, can the hack in this blog post be made to work?

It turns out all you have to do to pass data from a template to a layout is declare an instance variable. So for YAML data like I was doing:

# In your template
<% @my_var = YAML.load_file("path/to/data.yml") %>

# In your layout
<p>My_var contains: <%= @my_var %></p>

I don’t know if this is obvious if you’re familiar with the tool chain underneath middleman, or if it’s in the documentation somewhere and I just couldn’t find it, but that’s all there is to it.

Thanks so much to @dddd1919 for dropping this knowledge bomb.

Hey, for some reasons this answer didn’t work for me but this worked (in case someone is searching):

<%= partial("layouts/yourpartial", locals: {title: "Hello World" }) %>

…and in partial:

<h2><%= locals[:cta] %></h2>

<h2><%= cta %></h2> should also suffice