I'm updating a Magento 1.x extension to Magento 2.x. The extension adds some dynamic css to the head based on configuration values in the backend (for instance colorpickers that save a hex value to be assigned to an element).
The Magento 1.x extension uses a rewrite of html_head to add the following code:
protected function _prepareLayout() {
parent::_prepareLayout();
if($head_block = $this->getLayout()->getBlock('head')) {
$styles_block = $this->getLayout()->createBlock('Mage_Core_Block_Template', 'custom_styles')->setTemplate('module/styles.php');
$head_block->setChild('custom_styles', $styles_block);
}
}
And the styles.php looks somehting like this:
<style type="text/css">
<?php
$model = Mage::getModel('module/model')->getCollection();
foreach ($model as $item) :
?>
.element { background-color: <?php echo $item['bgcolor'] ?>; }
<?php
endforeach;
?>
</style>
This works great in Magento 1.x but I have yet to figure out how to do this for Magento 2.x.