2015-06-28 46 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> 
相關問題