如何使用 web py (2.7) 在模板内部调用 html
How to call html inside of the tempate using webpy (2.7)
我正在尝试学习如何使用 web.py 重新创建网站,但找不到我需要的语法。
在我使用过的所有其他语言中,包括纯 HTML,有一种方法可以通过调用脚本、对象、包或视图来在网页中呈现 html东西有点模块化。那么我该如何使用 Python?
我会很好地将所有非布局文件重新排列到静态目录,将它们重写为 Python 文件,以某种方式写入和 return HTML,或包括url 处理中的子文件,如果有办法以这种方式访问并将它们加载到 html。
我希望将 .py 调用到 运行 一个脚本,该脚本将编写或调用该部分所需的 html,但我似乎找不到符合我要求的示例需要。
在此先感谢您提出任何建议。
现在我的代码看起来像这样:
code.py
import web
urls = (
'/favicon.ico', 'icon',
'/', 'index',
'/index', 'index'
)
app = web.application(urls, globals(), autoreload=True)
render = web.template.render('templates/')#, base='Layout')
static = web.template.render('static/')
Main = web.template.render('templates/')
MainContent = web.template.render('Content/')
class static:
def GET(self):
return static()
class icon:
def GET(self):
return static.favicon()
class index:
def GET(self):
return Main.Layout(0, 0, 'Main.css')
if __name__ == '__main__':
app.run()
/templates/Layout.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="static/$vCSS" rel="stylesheet" type="text/css">
</head>
<body id="idBody">
<table id="idTableMain">
<tr id="idHeaderRow">
<td id="idHeaderRowCenter" colspan="3">
<img src="static/logo.jpg"/>
</td>
</tr>
<tr id="idNavigationRow">
<td id="idNavigationBar" colspan="3">
<!--Display /templates/NavBar.html -->
</td>
</tr>
<tr id="idCenterRow">
<td id="idCenterRowLeft">
<h4>
Navigation
</h4>
<!--Display /templates/Navigation.html -->
</td>
<td id="idCenterRowMain">
<!--Display /templates/Content/Content_index.html -->
</td>
<td id="idCenterRowRight">
<h4>
Information
</h4>
This was written with Python.<br><br>
Other versions of this page are here:<br>
<!--Display /templates/Versions_index.html -->
</td>
</tr>
<tr id="idFooterRow">
<td id="idFooterMain" colspan="3">
<!--Display /templates/Footer.html -->
</td>
</tr>
</table>
</body>
</html>
好的,我学到了一些东西,现在可以在 web.py 中复制我的网站,但我不喜欢我在某些地方的做法。
1) 我希望能够在每次不调用 Main 的情况下调用模板。
2) 我希望能够从布局中调用函数。
如果喜欢请评论,以上任何建议都会有所帮助。
我会复制这个并作为另一个问题重新发布。
code.py:
import web
import Universal
import Navigation
import Content
import Versions
urls = (
'/favicon.ico', 'icon',
'/', 'index',
'/Section1/index', 'Section1',
'/Section2/index', 'Section2',
'/Section3/index', 'Section3'
)
app = web.application(urls, globals(), autoreload=True)
render = web.template.render('templates/')#, base='Layout')
static = web.template.render('static/')
Main = web.template.render('templates/')
Section1 = web.template.render('templates/Section1/')
Section2 = web.template.render('templates/Section2/')
Section3 = web.template.render('templates/Section3/')
class static:
def GET(self):
return static()
#class icon:
# def GET(self):
# return static.favicon()
class index:
def GET(self):
vPage = '0'
vLevel = '0'
vSection = '0'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
return Main.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
class Section1:
def GET(self):
vPage = '0'
vLevel = '1'
vSection = '1'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
return Main.Section1.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
class Section2:
def GET(self):
vPage = '0'
vLevel = '2'
vSection = '2'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
return Main.Section2.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
class Section3:
def GET(self):
vPage = '0'
vLevel = '3'
vSection = '3'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
#return render.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
return Main.Section3.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
templates/Layout.html:
$def with (vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
<html>
<head>
$:vHead
</head>
<body id="idBody">
<table id="idTableMain">
<tr id="idHeaderRow">
<td id="idHeaderRowCenter" colspan="3">
$:vHeader
</td>
</tr>
<tr id="idNavigationRow">
<td id="idNavigationBar" colspan="3">
$:vNavBar
</td>
</tr>
<tr id="idCenterRow">
<td id="idCenterRowLeft">
<h4>
Navigation
</h4>
$:vNavigation
</td>
<td id="idCenterRowMain">
$:vContent
</td>
<td id="idCenterRowRight">
<h4>
Information
</h4>
This was written with Python 2.7 and web.py.<br><br>
Other versions of this page are here:<br>
$:vVersions
</td>
</tr>
<tr id="idFooterRow">
<td id="idFooterMain" colspan="3">
$:vFooter
</td>
</tr>
</table>
</body>
</html>
Universal.py
def getHead(vSection):
vResult = '<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">'
vResult += '<link href=' + getCSS(vSection) + ' rel=\"stylesheet\" type="text/css">'
return vResult
def getCSS(vSection):
if vSection == '1':
vResult = '/static/Section1/Section1.css'
elif vSection == '2':
vResult = '/static/Section2/Section2.css'
elif vSection == '3':
vResult = '/static/Section3/Section3.css'
else:
vResult = '/static/Main.css'
return vResult
def getHeader():
vResult = '<img id=\"idLogo\" src=' + getLogo() + '>'
return vResult
def getNavBar():
vResult = '<a class=\'navBar\' href=\'/index\'>Home</a>'
vResult += '<a class=\'navBar\' href=\'/Section1/index\'>Web Programming</a>'
vResult += '<a class=\'navBar\' href=\'/Section2/index\'>Private Projects</a>'
vResult += '<a class=\'navBar\' href=\'/Section3/index\'>Downloadable Projects</a>'
return vResult
我正在尝试学习如何使用 web.py 重新创建网站,但找不到我需要的语法。
在我使用过的所有其他语言中,包括纯 HTML,有一种方法可以通过调用脚本、对象、包或视图来在网页中呈现 html东西有点模块化。那么我该如何使用 Python?
我会很好地将所有非布局文件重新排列到静态目录,将它们重写为 Python 文件,以某种方式写入和 return HTML,或包括url 处理中的子文件,如果有办法以这种方式访问并将它们加载到 html。
我希望将 .py 调用到 运行 一个脚本,该脚本将编写或调用该部分所需的 html,但我似乎找不到符合我要求的示例需要。
在此先感谢您提出任何建议。
现在我的代码看起来像这样: code.py
import web
urls = (
'/favicon.ico', 'icon',
'/', 'index',
'/index', 'index'
)
app = web.application(urls, globals(), autoreload=True)
render = web.template.render('templates/')#, base='Layout')
static = web.template.render('static/')
Main = web.template.render('templates/')
MainContent = web.template.render('Content/')
class static:
def GET(self):
return static()
class icon:
def GET(self):
return static.favicon()
class index:
def GET(self):
return Main.Layout(0, 0, 'Main.css')
if __name__ == '__main__':
app.run()
/templates/Layout.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="static/$vCSS" rel="stylesheet" type="text/css">
</head>
<body id="idBody">
<table id="idTableMain">
<tr id="idHeaderRow">
<td id="idHeaderRowCenter" colspan="3">
<img src="static/logo.jpg"/>
</td>
</tr>
<tr id="idNavigationRow">
<td id="idNavigationBar" colspan="3">
<!--Display /templates/NavBar.html -->
</td>
</tr>
<tr id="idCenterRow">
<td id="idCenterRowLeft">
<h4>
Navigation
</h4>
<!--Display /templates/Navigation.html -->
</td>
<td id="idCenterRowMain">
<!--Display /templates/Content/Content_index.html -->
</td>
<td id="idCenterRowRight">
<h4>
Information
</h4>
This was written with Python.<br><br>
Other versions of this page are here:<br>
<!--Display /templates/Versions_index.html -->
</td>
</tr>
<tr id="idFooterRow">
<td id="idFooterMain" colspan="3">
<!--Display /templates/Footer.html -->
</td>
</tr>
</table>
</body>
</html>
好的,我学到了一些东西,现在可以在 web.py 中复制我的网站,但我不喜欢我在某些地方的做法。
1) 我希望能够在每次不调用 Main 的情况下调用模板。 2) 我希望能够从布局中调用函数。
如果喜欢请评论,以上任何建议都会有所帮助。
我会复制这个并作为另一个问题重新发布。
code.py:
import web
import Universal
import Navigation
import Content
import Versions
urls = (
'/favicon.ico', 'icon',
'/', 'index',
'/Section1/index', 'Section1',
'/Section2/index', 'Section2',
'/Section3/index', 'Section3'
)
app = web.application(urls, globals(), autoreload=True)
render = web.template.render('templates/')#, base='Layout')
static = web.template.render('static/')
Main = web.template.render('templates/')
Section1 = web.template.render('templates/Section1/')
Section2 = web.template.render('templates/Section2/')
Section3 = web.template.render('templates/Section3/')
class static:
def GET(self):
return static()
#class icon:
# def GET(self):
# return static.favicon()
class index:
def GET(self):
vPage = '0'
vLevel = '0'
vSection = '0'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
return Main.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
class Section1:
def GET(self):
vPage = '0'
vLevel = '1'
vSection = '1'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
return Main.Section1.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
class Section2:
def GET(self):
vPage = '0'
vLevel = '2'
vSection = '2'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
return Main.Section2.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
class Section3:
def GET(self):
vPage = '0'
vLevel = '3'
vSection = '3'
vHead = Universal.getHead(vSection)
vHeader = Universal.getHeader()
vNavBar = Universal.getNavBar()
vNavigation = Navigation.getNavigation(vLevel)
vContent = Content.getContent(vLevel)
vVersions = Versions.getVersions(vLevel)
vFooter = Universal.getFooter()
#return render.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
return Main.Section3.Layout(vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
templates/Layout.html:
$def with (vHead, vHeader, vNavBar, vNavigation, vContent, vVersions, vFooter)
<html>
<head>
$:vHead
</head>
<body id="idBody">
<table id="idTableMain">
<tr id="idHeaderRow">
<td id="idHeaderRowCenter" colspan="3">
$:vHeader
</td>
</tr>
<tr id="idNavigationRow">
<td id="idNavigationBar" colspan="3">
$:vNavBar
</td>
</tr>
<tr id="idCenterRow">
<td id="idCenterRowLeft">
<h4>
Navigation
</h4>
$:vNavigation
</td>
<td id="idCenterRowMain">
$:vContent
</td>
<td id="idCenterRowRight">
<h4>
Information
</h4>
This was written with Python 2.7 and web.py.<br><br>
Other versions of this page are here:<br>
$:vVersions
</td>
</tr>
<tr id="idFooterRow">
<td id="idFooterMain" colspan="3">
$:vFooter
</td>
</tr>
</table>
</body>
</html>
Universal.py
def getHead(vSection):
vResult = '<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">'
vResult += '<link href=' + getCSS(vSection) + ' rel=\"stylesheet\" type="text/css">'
return vResult
def getCSS(vSection):
if vSection == '1':
vResult = '/static/Section1/Section1.css'
elif vSection == '2':
vResult = '/static/Section2/Section2.css'
elif vSection == '3':
vResult = '/static/Section3/Section3.css'
else:
vResult = '/static/Main.css'
return vResult
def getHeader():
vResult = '<img id=\"idLogo\" src=' + getLogo() + '>'
return vResult
def getNavBar():
vResult = '<a class=\'navBar\' href=\'/index\'>Home</a>'
vResult += '<a class=\'navBar\' href=\'/Section1/index\'>Web Programming</a>'
vResult += '<a class=\'navBar\' href=\'/Section2/index\'>Private Projects</a>'
vResult += '<a class=\'navBar\' href=\'/Section3/index\'>Downloadable Projects</a>'
return vResult