Split content to inject partial?

Hi everyone!

After realizing today that Jekyll 3 broke my category plugins (and removed the built-in category functionality), I decided to try out Middleman. So far so good. Anyhoo, I have some code in my Jekyll site that looks for a string to text to then replace with HTML. Here’s the relevant snippet:

<main class="main">
  {% if content contains "<!--/ ad /-->" %}
    {{ content | split:"<!--/ ad /-->" | first % }}
      {% include ad.html %}
    {{ content | split:"<!--/ ad /-->" | last % }}
  {% else %}
    {{ content }}
  {% endif %}
</main>

I was wondering if it is at all possible to do the same thing in Middleman.

This community seems to be super dead, but maybe because it’s new? Idk, I’m new so whatevs. Anyhoo! Here’s the solution:

<main class="main">
  <% content = yield %>

  <% if content.include?("<!--/ ad /-->") %>
    <%= content.split("<!--/ ad /-->").first %>
      <%= partial "partials/ad" %>
    <%= content.split("<!--/ ad /-->").last %>
  <% else %>
    <%= content %>
  <% end %>
</main>

Welcome!

Sorry, I put this in my queue to respond to, but you got it figured out first.

Middleman lets you do Ruby code so it will let you do a lot more than Jekyll (Liquid)

I post my questions on StackOverflow - not that the community is great there, but it is OK… and I’d like to see it take off there :slight_smile:

I know this is a rather late entry. I’m still new to Middleman myself and don’t check the forum that often. I saw this post last week some time. But since you seemed to have found your solution, I just continued onward to read the next post. But I came across this again and was bothered a bit - as I was the first time - with the amount of code. And it got me thinking about a different way to do it. Since everything is generated during the build process, why not just do something like this:

<main class="main">
    <%= yield.sub("<!--/ ad /-->", partial("partials/ad")) %>
</main>

Of course, .sub() only replaces the first instance. So if you have multiple instances, you could use .gsub(). I tested it with a small .haml file on one of my own sites and it worked. But for the second .sub() parameter, I had to use either partial("partials/ad") or (partial "partials/ad"). If I just used partial "partials/ad" (no parentheses), I got a syntax error. Maybe you wouldn’t have the same issue in a .erb file. (?)

Cheers

I discovered that I could just continue using an older version of Jekyll so I don’t have to deal with broken category plugins. Still, your answer looks good!