I have a module that needs to display specific CSS values for each entry in my custom model collection.
This is trivial enough, as I can simply echo these values with PHP in my template as needed; however, I can't get over the fact that this requires me to plant an embedded style sheet in my template.
I'm essentially doing:
<?php $models = Mage::getModel('my/model')->getCollection() ?>
<style type="text/css">
[class*='item-'] {
display: block;
padding: 20px;
}
<?php foreach ($models as $model): ?>
.item-<?php echo $model->getId() ?> {
background: <?php echo $model->getMyColor() ?>;
}
<?php endforeach ?>
</style>
<ul>
<?php foreach ($models as $model): ?>
<li class="item-<?php echo $model->getId() ?>">
<p><?php echo $model->getText() ?></p>
</li>
<?php endforeach ?>
</ul>
The purist in me really doesn't like this approach. I start to twitch when I see the markup mixed with PHP and CSS like that... though, in all fairness, it does what it needs to do.
Is there a better approach I am missing? While I am comfortable using SASS or other CSS preprocessors, I can't rely on these being present in a random server environment.
What methods do you use to inject PHP variables into your stylesheets?