2010-05-21 74 views
6

給定一個佈局視圖,如何從視圖文件加載靜態文件(本質上是CSS和JS)到<頭>?如何在web2py中從視圖HTML加載靜態文件?

的layout.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{{=T.accepted_language or 'en'}}"> 
    <head> 
     <title>{{=response.title or request.application}}</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <!-- include requires CSS files 
     {{response.files.append(URL(request.application,'static','base.css'))}} 
     {{response.files.append(URL(request.application,'static','ez-plug-min.css'))}} 
     --> 
     {{include 'web2py_ajax.html'}} 
    </head> 
    <body> 
     {{include}} 
    </body> 
</html> 

myview.html

{{extend 'layout.html'}} 
{{response.files.append(URL(r=request,c='static',f='myview.css'))}} 

<h1>Some header</h1> 
<div> 
    some content 
</div> 

在上面的例子中, 「myview.css」 文件或者由的web2py忽略或由剝離出瀏覽器。

那麼加載特定於頁面的文件如此CSS文件的最佳方式是什麼?我寧願不把我所有的靜態文件放入我的佈局中。

回答

7

在myview.html扭轉前兩行

{{response.files.append(URL(r=request,c='static',f='myview.css'))}} 
{{extend 'layout.html'}} 

記住,1.78.1和1.78.2有一個bug並沒有讓這個工作。它在同一天被固定在1.78.3。 response.file.append(...)也可以在需要它的控制器動作中移動。您不應該在擴展之前放置邏輯,而是定義要傳遞給擴展視圖的變量。

+0

完美。謝謝! – MikeWyatt 2010-05-21 12:54:59