How to load static files from view HTML in web2py?

Posted by MikeWyatt on Stack Overflow See other posts from Stack Overflow or by MikeWyatt
Published on 2010-05-21T01:45:32Z Indexed on 2010/05/21 1:50 UTC
Read the original article Hit count: 472

Filed under:
|
|

Given a view with layout, how can I load static files (CSS and JS, essentially) into the <head> from the view file?

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>

In the above example, the "myview.css" file is either ignored by web2py or stripped out by the browser.

So what is the best way to load page-specific files like this CSS file? I'd rather not stuff all my static files into my layout.

© Stack Overflow or respective owner

Related posts about web2py

Related posts about static-files