以编程方式包含条件(特定于浏览器的)样式表
如何以编程方式在 Drupal 6 中包含特定于浏览器的样式表?
长话短说,我有一个包含非常具体的样式规则的模块,我希望仅将其包含在使用该模块的页面上。
drupal_add_css()
工作正常,但我可能需要包含某些特定于 IE 的规则,并且我不想将这些规则添加到主站点条件样式表中。主站点 CSS 已经相当长,这些更改应该与站点的其余部分保持隔离。
How does one programmatically include browser-specific stylesheets with Drupal 6?
Long story short, I have a module which includes very specific style rules, which I want included only on pages which make use of the module.
drupal_add_css()
works fine, but I may well need to include IE-specific rules for some bits, and I don't want to add these rules to the main site conditional stylesheets. The main site CSS is already quite lengthy, and these changes should remain segregated from the rest of the site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不想将条件样式表添加到每个页面,则需要使用预处理函数。
预处理函数是允许您添加/删除将在页面上呈现的内容的钩子。在您的情况下,您希望使用适当的 *_preprocess_page 函数来挂钩整个页面的渲染点(因此您可以访问 page.tpl.php)。
在此预处理函数中,您应该添加一个如下所示的新变量:
然后在 page.tpl.php 中,添加这个新的
my_custom_stylesheet
变量,其中中有 CSS 样式表声明;
你的 page.tpl.php,像这样:就是这样。您可以在主题或模块中使用预处理函数,因此请使用最有意义且最容易编写上面代码的
if(满足条件)
部分的函数。If you don't want to add the conditional stylesheet to every page, you need to use a preprocess function.
Preprocess functions are hooks that allow you to add/subtract content that will be rendered on the page. In your case, you want to use the appropriate
*_preprocess_page
function to hook into the point at which the entire page is rendered (and which therefore gives you access to page.tpl.php).In this preprocess function, you should add a new variable like this:
Then in your page.tpl.php, add this new
my_custom_stylesheet
variable where you have the CSS stylesheet declarations in the<head>
of your page.tpl.php, something like this:That's it. You can use preprocess functions either in your theme or in a module, so use whichever makes the most sense and is easiest to write the
if (condition is met)
part of the code above.您可以使用条件注释。
这是一篇关于它的文章:
http://www.quirksmode.org/css/condcom.html< /a>
这是一个片段:
请参阅提供的链接中的文档。
You can use conditional comments.
Here is an article about it:
http://www.quirksmode.org/css/condcom.html
Here is a snippet:
See the documentation in the link provided.
你说的是哪个版本的IE?
您可以执行以下操作,而无需包含单独的样式表:
样式表应由浏览器缓存。通过将它们添加到代码中,您将阻止此功能。
Which version of IE are you talking about?
You can do things like the following without including separate stylesheets:
Stylesheets should be be cached by the browser. By adding them in code you're braking this feature.
为了跟进这一点,Drupal 7(我刚刚发现)确实支持以编程方式包含条件 css 如其文档中所述:
这是调用:
drupal_add_css($directory . 'ie7.css', array('group' => CSS_THEME, 'browsers' = > array('IE' => 'lte IE 7', '!IE' => FALSE), 'every_page' => TRUE));
因为它包含在 Drupal 的 Zen 主题中7.
不管怎样,这对我正在从事的项目(Drupal 6...)没有任何帮助,而且对于以编程方式注入条件 css 是否是一个好主意的问题,我认为保持开放。
Just to follow up on this, Drupal 7 (I just discovered) does support programmatically including conditional css as referred to in their documentation here:
Here's the call:
drupal_add_css($directory . 'ie7.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lte IE 7', '!IE' => FALSE), 'every_page' => TRUE));
as it is included in the Zen theme for Drupal 7.
For what it's worth, this didn't help me at all in the project I was working on (it was Drupal 6...) and the question of whether or not it's even a good idea to programmatically inject conditional css, I think remains open.