2015-06-28 47 views
3

我正在使用Play框架。将静态文件内容包含到Play 2.4模板中

在模板中,可以执行以下操作来包含css。

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"> 

但是我希望做的是包括CSS直接进入网页

<style>@[include the static file contents]</style> 

这可能吗?

+0

我只是想知道...你为什么要内联你的styleshe et档案? – Roman

+0

因此,不需要额外的请求来获取它。这不是什么大问题,但为什么不呢? – Andrey

+1

我强烈建议**不要**这样做。你从中得不到什么。所有现代浏览器都会缓存你的外部样式表。它只会被传送一次。另外不要忘记优化规则:1.不要。 2.还没有(http://c2.com/cgi/wiki?RulesOfOptimization) – Roman

回答

4

正如其他人提到它是首选方式,反正你可以使用普通的'tags' technique这样做,

只需创建一个文件views/tags/yourStyles.scala.html与内容:

<style> 
    * { 
     background: orange; 
    } 
</style> 

所以在你的模板/图你可以使用它作为(样本):

<head> 
    <title>@title</title> 
    <link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")"> 
    <link rel="shortcut icon" type="image/png" href="@routes.WebJarAssets.at("images/favicon.png")"> 
    @tags.yourStyles() <!-- <-here -> 
</head> 
相关问题