2016-09-19 89 views
2

可以在我的應用程序中定義主顏色和輔助顏色(十六進制代碼),將它們保存到數據庫。從數據庫生成CSS類?

輔助顏色例如用於鏈接。我不想說<a href="#" style="color: $fromDatabase">Text</a>而是<a href="#" class=secColor>Text</a>其中.secColor具有類似

.secColor { 
    color: $fromDatabase; 
} 

我使用Laravel BTW。

回答

2

您可以通過下面的代碼包含PHP文件作爲一個css:

的index.html:

<!DOCTYPE HTML> 
<html> 
<head> 
    <link rel="stylesheet" href="style.php"> 
</head> 
<body> 
<!-- stuff goes here --> 
</body> 
</html> 

style.php:

<?php 
    header("Content-type: text/css"); 
?> 
//DB Query 
.secColor{ 
    color: <?php echo $fromDatabase;?> 
} 
+0

神奇的解決了這個方法,謝謝! – Scarwolf

0

你可以這樣做:

創建新路線規則:

Route::get('style/generate.css', function ($id) { 
    // take your color 
    $data['firstColor']= Colors::where('alias', '=', 'firstColor')->get(); 
    ... 
    return View::make('css.colors', $data) 
}); 

,並創建資源/視圖/ CSS /顏色新觀點:

.firstColor{ 
    color: $colors['firstColor']; 
} 

而在你的主要觀點

我用創建JS自定義文件