How do I call a helper for every page upon render?

I’m looking for a way to automatically call a helper method upon render of each page. So far all my attempts building an extension for that have failed. I can’t seem to find a hook to call a helper method on a resource.

My helper method is supposed to read the frontmatter and then call content_for in the context of the resource. Is something like that possible? Or is there any other way to achieve the same?

Hi,
i did not understand exactly what you mean with context of the resource. But let’s find out.

With ‘current_page’ you can get the frontmatter data in the context of the current page:

<%= current_page.data.title %>

Then what do you want to do next with content_for?

Let’s say I create a page foo.html. In that page I define some frontmatter data. Upon rendering the page I want middleman to automatically call a helper method on that page. This helper method is supposed to read the frontmatter data and pass it to content_for.

The point is that this helper method needs to be called on every page, but I don’t want to include it on every page. I want this to happen automatically to make things a bit easier and less error prone for an author.

Upon rendering the page I want middleman to automatically call a helper method on that page.

That’s what actually happens. The helper methods gets called when the page get rendered.

The point is that this helper method needs to be called on every page, but I don’t want to include it on every page.

Why not put it in the layout, then the helper method is rendered with every page. Or maybe want to write a partials that can be added to every page.

I know that helper methods I put in my pages will be called upon render. The point is I don’t want to put any method there to make it easier for authors to write pages.

Putting it in the layout might be an alternative. Do helpers in the layout have access to frontmatter of the current page?

Yes, if you do it like this:

Well, that works. I added the helper to my layout and use the current page’s frontmatter data.

Not exactly what I have envisioned, but it works the same.

Great! If I had more information on what you are trying to achieve, I might come up with a better solution. :slight_smile:

It’s pretty simple, actually. I need a specific element (large hero banner) on every page, but the content of that element depends on the page it is rendered on. My original idea was to include configuration data for the hero banner on every page using frontmatter and then automatically call a helper that consumes this frontmatter data. In my layout I have a yield_content for that element.

I wanted everything to work automagically, but including the helper in the layout works, too.

So what should your helper do then?

Of course you could use the content_for helper to render the banner in the layout.

<% content_for :banner do %>
  <div class="banner">Mein Banner</div>
<% end %>

Like I said, I do have a working solution now. The only thing I’d wanted is to eliminate the call to my helper method from the layout and have that handled automatically by an extension, but it’s fine the way I do it now.

Hello @kabuto

Maybe the code of my site can help you

As you can see differents pages have differents informations extracted from the frontmatter

http://davidl.fr/blog/