I get no errors or warnings and in the page, I have:
<span>some markup</span>
<p>some very long content</p>
<span>end markup</span>
So this approach works just the way I need it, but I’m concerned with whether this is considered good practice. Can it go wrong? Perhaps it wouldn’t work in an older PHP version? I haven’t seen it done before.
That’s an interesting approach and I can see how it can be useful.
I don’t think it will break, but it’s a bit clunky and other devs who may work on the site later probably won’t immediately understand what’s going on (I’ve never seen it either). So I would recommend keeping it simple by splitting the snippet into a header and footer as usual.
Well, my actual use case is for header navigation links. Each nav item can have a dropdown or not, based on the page content, but I want that dropdown to have different formatting across some pages. So I pass the item’s $page and the $dropdown closure where I put the dropdown content (where you could easily pass the current nav item $page as a parameter). I also kind of lied about the static content part. It is mostly static, but there are some dynamic stuff based on the nav item’s $page. For example, and id attribute for the javascript (that toggles the dropdown) to use as hook.
So the end result is - a snippet that can output a nav item link with dynamic dropdown content. This is what it looks like: