如何在web2py中从视图HTML加载静态文件?

6

如果给定一个带有布局的视图,我该如何从视图文件中加载静态文件(主要是 CSS 和 JS 文件)到 <head> 中?

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文件的特定页面文件的最佳方法是什么? 我不想把所有静态文件都塞到我的布局中。
1个回答

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(...)也可以移动到需要它的控制器操作中。您不应该在扩展之前放置逻辑,但可以定义要传递给扩展视图的变量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接