0

The issue is on Symfony 5, but I guess it can be a general issue for the communication between php and css.

First, let me say that I'm not sure this kind of thing is possible, probably but I don't know for sure, so let me know if there is some libraries that you might know to handle this kind of things

Let's say I have a simple test.html page with this code.

<div class="test">Test text</div>

I simplified but of course you also have an import from a test.css

From my Symfony Controller when I will hit my /test url, I will redirect to this test.html page with a variable marginleftthat could contain a value like 100px for example purpose.

So the question is, do I have a way to dynamically load my css using the php variable ?

To be more specific, I'm not speaking about using some javascript workaround to change the css class content when I load the page. What I am looking for is directly to load my test.css using php variables.

In other words, I would like to do something like this :

.test{
    margin-left : {{marginleft}}
}

My main goal here is to provide a almost full css customization of elements from a page for my users that's why I don't want to just change defaults elements with javascript.

Thanks for your assistance.

  • 1
    you can use echo to print a style tag in the head –  Feb 22 '20 at 17:48
  • 1
    Here is your answer : https://thisinterestsme.com/dynamic-css-stylesheet-php/ and this : Dynamic CSS using php https://stackoverflow.com/questions/42576453/dynamic-css-using-php –  Feb 22 '20 at 18:02
  • Yes but with this solution you don't keep your css in a css file you use it in the header. Not really a must have practice. – Kanki Shogun Feb 22 '20 at 18:40
  • @Dlk Your first link might be a good idea, I'm not sure if you can use it with the assets system of symfony – Kanki Shogun Feb 22 '20 at 18:43
  • 1
    I don’t think will have differences between symphony and codeigniter loading css ! But in case see this https://stackoverflow.com/questions/32991789/how-to-generate-a-dynamic-css-file-using-php-or-codeigniter –  Feb 22 '20 at 18:53
  • I was more thinking about issue like minification, but I guess it can work this way. I will try anyway. Thanks. – Kanki Shogun Feb 22 '20 at 19:50

0 Answers0