I am working on an app which allows users to choose style options inside the app. These options will then be used to generate a dynamic web page. I was wondering what my options are to achieve the styling of this page, and what the pros and cons are of these options?
The way I see it, I have these 3 options:
Apply the css inline
Create a dynamic php stylesheet... e.g:
<link rel="stylesheet" type="text/css" href="http://domain.com/style.php/>
i have also heard about caching stylesheets but I am not even sure about how to do this.开发者_Python百科 If this is the best option where would be the best place to read up on this?
Are these all the options available? And which would be best for what I am trying to achieve?
Thanks for any input
Paul
Of course your second option is the best.
With a style.php you are able to output whenever you want based on the user logged in.
Consider I use style.php without even needing to customize it for the user. With it you can extend CSS functionalities like:
<?php $blue = 'color:#yourbluecolorFFF'; ?> // EXAMPLE
td { <?php echo $blue; ?>}
/*etc*/
Also don't forget to:
header('Content-Type: text/css');
TLDR Summary
- This is the basis of what you'll be doing.
- You could try that, but I think you'll run into the same issue of caching.
- The files get cached based on how your server serves them rather than based on your code.
I'd say load stylesheets incrementally and let the user select the style they want.
That is to say, load your stylesheets in "layers". For example you might use a static "style.css" to put down the basics that are common across all of your styles. Then, you might cook up 3 sheets that each alter/override the style slightly:
- red-style.css for red-themed colors,
- blue-style.css for blue-themed colors,
- etc.
From there you have two options. You can either
- Leave all of your class/id values static and change the stylesheet with a page re-load (and insert it for future page-loads via PHP) or
- You can load them all at once and have your PHP change the class/id values (e.g.
#content-article
becomes#content-article-red
or#content-article-blue
). Not recommended.
My two cents, anyway. :)
This really depends on how elaborate the styling options are. If its some simple stuff, probably going with inline CSS is best option. If there is plenty of customization, then i would suggest that you generate their customized CSS at "save" time and store it on the filesystem. I wont personally prefer styles.php as it is an added load on the server, compare a web server sending a static file versus having to process that request via php and then serving the content.
Unless you have really few visitors and/or the CSS changes VERY frequently (which i cant think why), you are better off with inline css or generating the dynamic css files for each user.
Based on your answer in the comment. I think its best that you generate the CSS file after save and overwrite the default CSS file you would include in the package.
Simplest solution i see. You can have a template CSS file, in which you can have tags. Load the template into a PHP variable. Replace the tags with inputs you receive from the settings form. Save/overwrite the CSS, you are done.
精彩评论