Dynamic css in a plugin

Is there any way I can generate/import “dynamic” css through a plugin?

I would like to access some options set by the plugin and generate some css rules based on these.

The basic idea is to serve a php file as css, but as a plugin only loads the index.css file, I’m looking for an alternative way to load a index.css.php file, for example:

<?php
header('Content-type: text/css; charset: UTF-8');
$tags = option("plugin.tags");

foreach ($tags as $tag) {
    echo ".field-$tag {
        display: block;
    }";
}

Any ideas are welcome,
Thank you

Hey,

you could wrap Your code in a route and call that url in Your html head, for example in a plugin:

Kirby::plugin('k-community/dynamic-css', [
	'routes' => [
		[
			'pattern' => 'dynamic-assets/css/index.css',
			'action'  => function () {
				$styles = [
					".field-tag1 {
						background: red;
					}",
					".field-tag2 {
						background: blue;
					}",
				];
				return new Response(implode($styles), 'text/css');
			}
		]
	]
]);

and then in your template:

<html>
<head>
<?= css([
    'dynamic-assets/css/index.css'
  ]) ?>
</head>
//...

But this will always regenerate the styles on page load, so I doubt, that this is good practice.
I guess You’d have to investigate a little further, if You want to cache the results.

Guide / Reference:
routes
response