Zend certified PHP/Magento developer

Adding layout handles to blocks in cms pages

We have a cms page with a content like this:

<p>Lorem Ipsum</p>

<div>{{block class="VendorModuleBlockFeature" block_id="feature_wrapper_block"}}</div>

<p>More Lorem Ipsum</p>

The block referenced in the cms page mostly consists of a template which, among other things, echoes other blocks.
These other blocks obviously need to exist in order to be injected.

My first attempt was to declare them in the default.xml layout so that they are available, then use $block->getLayout()->getBlock('block_name')->toHtml() in the phtml template to echo the other blocks. While this seems to work fine the problem is this approach will probably hurt performance a bit since I’ve put the declaration in default.xml the blocks are injected into every page.

My next idea was to put this in a layout handle which would add the blocks but that does not work because I’m unable to tell Magento to use that layout handle:

  1. Using $this->getLayout()->getUpdate()->addHandle('add_blocks'); in the block injected into the cms page does not work because the handle is evaluated after the block itself which means the other blocks created in the handle are not available while they are echoed in the phtml and thus everything explodes.
  2. Adding {​​​​{​​​​​​layout handle="add_quickorder_blocks"}​​​​​​}​​​​​​ in the cms page itself does not work because again, the {{block}} directive is evaluated before the {{layout}} directive and thus fails to render the block because the layout handles were not yet evaluated.
  3. I also tried adding a layout handle directly to the cms page but that didn’t work at all and is not a good solution either since the customer wants to be able to change the url of the cms page.
  4. Using an observer to add the layout handle works, but then the handle is present on every page again.

Another idea would be to add a new cms page layout which adds the required blocks but that sounds a bit overkill tbh. It might be the most efficient solution at the end of the day though.

What is the proper way to do this?