如何让多个web2py应用使用相同的layout.html?

5 投票
1 回答
1001 浏览
提问于 2025-04-17 02:09

我有三个应用程序,但我想让它们使用相同的layout.html和css。有办法做到这一点吗?

编辑:

我把静态文件夹和layout.html等放在了web2py根目录下的/common/文件夹里。

这是我在模型中做的:

import os
global web2py_path
web2py_path = os.environ.get('web2py_path', os.getcwd())
session.layout_path = web2py_path + '/common/layout.html'
print 'session.layout_path = ' + session.layout_path

然后在视图中:

{{extend session.layout_path}}

编辑 2:

关于下面评论提到的编译问题,我决定把'common'文件夹放到'/applications/'目录下,并把静态文件夹(css、图片)放在'common'文件夹里,就像一个普通的应用一样。然后我把layout.html放在'common'的根目录下。接着在另一个应用的视图中,我使用了:

{{extend '../../common/layout.html'}}

这样就引用了来自common应用的layout.html文件。这个layout.html文件再通过以下方式引用'common'中的静态文件夹里的文件:

{{=URL('common','static','css','style.css')}}

就像你在普通应用中那样。

1 个回答

5

在你的web2py文件夹的根目录下,创建一个新的文件夹,叫做'templates'。

/web2py/templates

把你的layout.html文件放到这个文件夹里。

现在在你的视图文件中,做如下操作:

{{extend 'path/to/web2py/templates/layout.html'}}

撰写回答