开发者

What is the best way to implement dynamic CSS & SVG values (PHP)?

I'm working a rudimentary system for holding pages on a number of domains. I intend to populate values in a DB and use these values to produce a page.

The values I'd use would include colour values which I'd like to then inject in to the CSS file and an SVG file.

Currently I'm thinking of using .htaccess to process .css/.svg as PHP and drop the values in from there, but is there a more efficient/elegant way without having to process these file formats as PHP?

All advice appreciated.

Many th开发者_JAVA技巧anks.


Disclaimer: I haven't really worked with svg but from looking at some sample code I don't think this matters for my answer.

Something about php in your css file doesn't sit right with me. I've tried it before and it didn't make things easier. CSS files don't parse php by default and even though you can make them it becomes easy to forget how you set things up. Here's how I would approach it...

If parts of the css is going to be different for each page, then it would make sense to declare that in the head of your html/svg page. You can link your default stylesheet with almost all your styling and then just override the specific color values in the head of your page. Or even inline if that makes your output easier. You won't have to worry about having php output in multiple files for the same page.

Or, if you have a limited set of styles that are going to be reused, there's nothing wrong with giving them all class names in the same file. Then you can just output the class name with php and use the same css file for everything. This would keep you organized and the file would have to be pretty huge for the unused classes to affect performance.


I think it's better to make this all PHP (or server side at least). You can use PHP to output both CSS and SVG. You can make files for either and just execute them as were they PHP. Most content will be static, but in a couple of places you can fill in the values using PHP.

For CSS you could take a look at LessCSS (or LessPHP).

Anyhow, I wouldn't use Javascript for this, ever. At least, not client side Javascript. :) This should typically be done on the server.


I've been thinking about doing something like to this. But in my case I use a CMS called MODx.

You could create a css file in MODx and then use php snippets to fill in the css file when it's called. My thinking was to have most of the css file not-dynamic and then have one or several snippets of php that would return specific css according to the page a user was on, the variables I send the snippet, etc.

Personal Note: MODx is a super powerful CMS, that you can do pretty much anything with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜