Parameters for dynamically creating CSS styles from a database in php

I am working on an application that allows users to select style options within the application. These options will then be used to create a dynamic web page. I was wondering what my options are for achieving the style of this page, and what are the pros and cons of these options?

As I see it, I have these 3 options:

  • Apply css inline

  • Create a dynamic php style sheet ... for example: <link rel="stylesheet" type="text/css" href="http://domain.com/style.php/>

  • I also heard about caching style sheets, but I'm not even sure how to do this. If this is the best option, where would it be best to read about it?

Are these options available? And what would be best for what I'm trying to achieve?

Thanks for any input.

Floor

+4
source share
3 answers

Of course, your second option is the best.

With style.php, you can output whenever you want based on a registered user.

Suppose I use style.php without even setting it up for the user. With it, you can extend the functionality of CSS, for example:

 <?php $blue = 'color:#yourbluecolorFFF'; ?> // EXAMPLE td { <?php echo $blue; ?>} /*etc*/ 

Also do not forget:

 header('Content-Type: text/css'); 
+5
source

Review TL; DR

  • This is the foundation of what you will do.
  • You can try this, but I think you will come across the same caching issue.
  • Files are cached based on how your server serves them, not based on your code.

I would say add table styles gradually and let the user select the style they want.

That is, load your stylesheets into β€œlayers”. For example, you can use the static "style.css" to define the basics that are common to all of your styles. Then you can prepare 3 sheets, each of which slightly changes / redefines the style:

  • red-style.css for red-themed colors,
  • blue-style.css for flowers with blue color,
  • and etc.

From there you have two options. You can either

  • Leave all your static class / id values ​​and change the stylesheet to reload the page (and insert it for future page loads via PHP) or
  • You can download them all at once and change the PHP class / identifier values ​​(for example, #content-article will become #content-article-red or #content-article-blue ). Not recommended.

My two cents, anyway. :)

+1
source

It depends on how complex the style options are. If its some simple things probably come with inline CSS, this is the best option. If there are a lot of settings, I would suggest you create your custom CSS in a "time-saving" time and save it in the file system. I personally do not prefer styles.php, since this is the added load on the server, compare the web server sending the static file instead of processing this request through php and then serving the content.

If you really have few visitors and / or CSS changes VERY often (which I cannot understand why), you are better off working with the built-in css or generating dynamic css files for each user.

Based on your answer in a comment. I believe that it is best to create a CSS file after saving and overwrite the default CSS file that you would include in the package.

The simplest solution I see. You may have a CSS template file in which you can have tags. Load the template into a PHP variable. Replace the tags with the inputs you receive in the settings form. Save / rewrite CSS, you're done.

0
source

All Articles