Silverstripe Requirements::css() 不包括 css

Silverstripe Requirements::css() does not include css

我尝试为某个页面包含自定义 css 文件。我在 PageController init() 中使用 Requirements::css() 方法,但我也尝试了模板标签:<% require css(mytheme/css/aa.css) %>。结果是,生成页面中的文件没有 link。没有报告错误,只是没有效果。 init() 方法被调用,我可以从其中触发日志。

class AaPage_Controller extends Page_Controller {

    public function init () {
        parent::init();
        Requirements::css("mytheme/css/aa.css");
    }

}

我现在在兜圈子,有没有SS经验多的人能推荐个debug方法吗?

您的模板文件中是否有 head 区域?在 Silverstipe 将您的要求 css 文件添加到它之前,您需要确保您的 .ss 文件具有最小熊。需要有像下面这样的基本 html 骨架。

<html>
<head>
    <title>Page title</title>
</head>
<body>
</body>
</html>

令人遗憾的是,新的 API 文档不再显示方法参数的解释,希望它能尽快修复。

如果您 look at the code(使用适当的 IDE 很容易,因为您可以跳转到您正在调用的方法),您会看到:

/**
 * Register the given stylesheet into the list of requirements.
 *
 * @param string $file  The CSS file to load, relative to site root
 * @param string $media Comma-separated list of media types to use in the link tag
 *                      (e.g. 'screen,projector')
 */
public static function css($file, $media = null) {
    self::backend()->css($file, $media);
}

The CSS file to load, relative to site root

由于您的主题不在网站根目录下,您需要这样的东西:

Requirements::css("themes/mytheme/css/aa.css");

我建议你使用Requirements::themedCSS。所以要包含 aa.css,您可以这样做:

Requirements::themedCSS('aa');

在您的模板中:

<% require themedCSS("aa") %>

这样它会自动在模板文件夹中搜索所请求的资源。