简体   繁体   English

以编程方式包含条件(特定于浏览器的)样式表

[英]Programmatic inclusion of conditional (browser-specific) stylesheets

How does one programmatically include browser-specific stylesheets with Drupal 6? 如何在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. drupal_add_css()可以很好地工作,但是我可能需要为某些位包含IE特定的规则,并且我不想将这些规则添加到主要站点条件样式表中。 The main site CSS is already quite lengthy, and these changes should remain segregated from the rest of the site. 主站点CSS已经相当长,这些更改应与站点的其余部分保持隔离。

You can use conditional comments. 您可以使用条件注释。

Here is an article about it: 这是关于它的文章:

http://www.quirksmode.org/css/condcom.html http://www.quirksmode.org/css/condcom.html

Here is a snippet: 这是一个片段:

<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->

See the documentation in the link provided. 请参阅所提供链接中的文档。

Which version of IE are you talking about? 您正在谈论哪个版本的IE?

You can do things like the following without including separate stylesheets: 您可以执行以下操作,而无需包含单独的样式表:

color:#ffffff; /* default value */
_color:#ffff00; /* overwrite for IE6 */
#color:#ff0000; /* overwrite for IE7 */

Stylesheets should be be cached by the browser. 样式表应由浏览器缓存。 By adding them in code you're braking this feature. 通过将它们添加到代码中,您将停止使用此功能。

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). 在您的情况下,您想使用适当的*_preprocess_page函数来钩住整个页面的渲染点(因此可以访问page.tpl.php)。

In this preprocess function, you should add a new variable like this: 在此预处理功能中,应添加一个新变量,如下所示:

<theme_name>_preprocess_page(&$vars) {
  if (condition is met) {
    $vars['my_custom_stylesheet'] = "<markup for adding a conditional stylehseet>";
  }
}

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: 然后在您的page.tpl.php中,添加新的my_custom_stylesheet变量,在page.tpl.php的<head>中具有CSS样式表声明,如下所示:

<?php print $my_custom_stylesheet; ?>

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. 您可以在主题或模块中使用预处理功能,因此请使用最有意义且最容易编写上述代码的if (condition is met)部分的方法。

Just to follow up on this, Drupal 7 (I just discovered) does support programmatically including conditional css as referred to in their documentation here: 只是为了跟进此事,Drupal 7(我刚刚发现)确实以编程方式支持其文档中所提及的条件css

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. 因为它包含在Drupal 7的Zen主题中。

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. 就其价值而言,这对我正在研究的项目(这是Drupal 6 ...)根本没有帮助,而对于以编程方式注入条件CSS是否是一个好主意的问题,我认为仍然存在打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM