Is there any way to use ifconfig
in Magento 2 to conditionally add CSS assets, similar to Magento 1? Here's an example from Magento 1:
<action ifconfig="config_path/group/field" method="addItem">
<type>skin_css</type><name>css/styles.css</name>
</action>
I tried something like this in Magento 2 but it doesn't work:
<head>
<css src="css/styles.css"
ifconfig="config_path/group/field" />
</head>
It just displays the ifconfig
attribute on frontend:
<link rel="stylesheet" type="text/css"
ifconfig="config_path/group/field"
href="http://example.com/pub/static/frontend/Magento/luma/en_US/css/styles.css" />
Is there any equivalent in Magento 2?
Best Answer
It seems like it currently is not possible looking at the
head.xsd
definition in the develop branch:https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/View/Layout/etc/head.xsd#L9
you could do something like the Require Js Module: https://github.com/magento/magento2/blob/develop/app/code/Magento/RequireJs/Block/Html/Head/Config.php
Here, the require config and mixins are inserted directly after the require.js file.
You could add your css files like this with a condition.
I also found an Issue here with a similar problem, but it did not get an answer: https://github.com/magento/magento2/issues/2004