Gio*_*lia 2 python templates overriding web.py
我正在使用带有基本模板的webpy
render = web.template.render(basedir + 'templates/', base='layout', globals=globals_vars_custom)
Run Code Online (Sandbox Code Playgroud)
在layout.html
我有类似的东西:
$def with (content)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>PAGE TITLE</title>
<!-- Some CSS and some javascript -->
</head>
<body>
$:content
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这个基本模板适用于我的网站的90%,但我有一个页面,我需要在<head>
(一些元标记)内插入一些其他数据.
我怎样才能做到这一点?如何在<head>
内部构建一个我可以在模板中轻松覆盖的结构?
谢谢!
它比我更容易:
您可以在模板中创建变量:
在page.html中,您可以定义变量
$var title = 'specific title'
Run Code Online (Sandbox Code Playgroud)
在基本模板layout.html中,您可以调用varable:
$def with (content)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>$content.get("title","DEFAULT TITLE")</title>
<!-- Some CSS and some javascript -->
</head>
<body>
$:content
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我希望这个答案对其他人也有用.