Access yield in helpers

Attempting to create a extension to middleman, that should be able to be installed without anyone touching any files. Any suggestions on accessing the Erb <%= yield %> method?

This is to prevent doing something like this in the template:

<%= layoutGen.generateBefore %>
<%= yield %>
<%= layoutGen.generateAfter %>

and instead, do something like this:

<%= layoutGen.generate() %>

Hi @MarkSimulacrum,

I’m not entirely sure what you’re trying to achieve.

If you need to wrap a layout, you can use the wrap_layout method. Check out the docs on Templates.

You can use capture and concat to capture content inside a block and yield it back at another point. For real dark magic, you can interact with the ERB buffer directly, but that way lies dragons and madness.

If you expand a bit more, maybe I can be of more help.

Pete

Hi @Aupajo,

I’m trying to achieve accessing the yield method that is called in templates be able to be called from my extension.

This would simplify my extension to Middleman - as there would no longer need to be two methods to call, and the extension user could simply call <% layoutGen.generate() %>, as I described above, This seems to be easier on both the user.

MarkSimulacrum

Hi @MarkSimulacrum,

I think I get what you’re getting at, but a more concrete example would be of help.

By “accessing the yield method”, do you mean that you access the content that yield would return?

What’s your extension trying to do, exactly? Provide a customised layout to the user? What’s the use-case?

For instance, if I placed activate :mark_simlacrums_extension in my config.rb, would I then find my content wrapped in your layout?

Is it a page-level layout (with a <head> and <body>, etc.) or something that goes between the page’s content and the user’s own layout?

Hi @Aupajo,

Yes, I do mean accessing the content that yield would return and it is a page-level layout.

Thanks for responding!

If it’s page-level, could you not just override the layout frontmatter in your extension?