通过.info文件添加样式表,对于大多数主题来说,已经足够了.由于.info文件是静态的,所以不能动态的添加样式表。依据主体是如何处理样式表的,将它们放到一块也是可以的。当你有所疑虑的时候,使用.info文件就可以了。
有两个API函数可用来处理样式表,drupal_add_css 和drupal_get_css。下面是一个动态添加样式表的例子。
将前缀"drop"改为你的主题名。
<?php
function drop_preprocess_page(&$variables) {
$front_style = path_to_theme() .'/front-page.css';
$path_style = path_to_theme() .'/path-'. arg(0) .'.css';
if (file_exists($front_style) && $variables['is_front']) {
$include_style = $front_style;
}
elseif (file_exists($path_style)) {
$include_style = $path_style;
}
if (isset($include_style)) {
drupal_add_css($include_style, 'theme', 'all', FALSE);
$variables['styles'] = drupal_get_css();
}
}
?>
在上面的例子中,访问首页,将会加载样式表"front-page.css",而访问其它页面,则会根据内部路径的不同加载其它的样式表。例如,对于页面,http://example.com/admin,将会使用"path-admin.css"。
一些注意点:
原文:http://drupal.org/node/225868
译者:葛红儒, Think in Drupal