实现动态 CSS 和 CSS 的最佳方式是什么? SVG 值(PHP)?
我正在开发一个基本系统来保存多个域上的页面。我打算填充数据库中的值并使用这些值生成页面。
我使用的值将包括颜色值,然后将其注入到 CSS 文件和 SVG 文件中。
目前我正在考虑使用 .htaccess 作为 PHP 处理 .css/.svg 并从那里放入值,但是有没有一种更有效/优雅的方法而不必将这些文件格式作为 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 thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
免责声明:我还没有真正使用过 svg,但通过查看一些示例代码,我认为这对我的答案并不重要。
你的 css 文件中有关 php 的某些内容不适合我。我以前尝试过,但并没有让事情变得更容易。默认情况下,CSS 文件不解析 php,即使您可以创建它们,也很容易忘记如何设置。这是我的处理方法...
如果每个页面的 css 部分都不同,那么在 html/svg 页面的头部声明它是有意义的。您可以将默认样式表与几乎所有样式链接起来,然后只需覆盖页面头部的特定颜色值。或者甚至是内联,如果这能让你的输出更容易。您不必担心同一页面的多个文件中有 php 输出。
或者,如果您有一组要重用的有限样式,那么在同一文件中为它们提供所有类名也没有什么问题。然后你可以用 php 输出类名,并为所有内容使用相同的 css 文件。这将使您保持井井有条,并且文件必须非常大,未使用的类才会影响性能。
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.
我认为最好将所有这些都放在 PHP 中(或至少在服务器端)。您可以使用 PHP 输出 CSS 和 SVG。您可以为其中任何一个创建文件,然后像 PHP 一样执行它们。大多数内容都是静态的,但在一些地方您可以使用 PHP 填充值。
对于 CSS,你可以看看 LessCSS(或 LessPHP)。
无论如何,我永远不会为此使用 Javascript。至少,不是客户端 JavaScript。 :) 这通常应该在服务器上完成。
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.
我一直在考虑做这样的事情。但就我而言,我使用名为 MODx 的 CMS。
您可以在 MODx 中创建一个 css 文件,然后在调用时使用 php 片段填充该 css 文件。我的想法是让大部分 css 文件不是动态的,然后使用一个或多个 php 片段,根据用户所在的页面、我发送片段的变量等返回特定的 css。
个人注意:MODx 是超级强大的 CMS,您几乎可以用它做任何事情。
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.