如何正确调用模板中的函数?

How to properly call functions in templates?

好的,我学到了一些东西,现在可以在 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 模板使用白名单来识别可以在模板内执行的功能。您可以通过创建字典并将其传递给渲染器来添加它。例如:

common_globals = {'getHeader': Universal.getHeader,
                  'getCSS': Universal.getCSS,
                  'getHead': Universal.getHead,
                  'getNavBar': Universal.getNavBar}
Main = web.template.render('templates/')
Section1 = web.template.render('templates/Section1/', globals=common_globals)
Section2 = web.template.render('templates/Section2/', globals=common_globals)
Section3 = web.template.render('templates/Section3/', globals=common_globals)

然后,在你的内心Layout.html你可以做到:

$def with(page, level, section)
<html>
<head>
    $:getHead(section)
</head>
etc....

...然后您在 code.py 中对 class 第 2 节的调用如下所示:

class Section2(object):
    def GET(self):
        return Main.Section2.Layout(page=0, level=2, section=2)

(实际上,我认为您不需要调用 Main,只需呈现为 return Section2.Layout(0, 2, 2)

重复其他部分并更新您的布局以直接调用您的函数,它会起作用。

感谢@pbuck。问题的第一部分通过使用字典并更新所有内容以匹配它来解决:

code.py 中的字典:

common_globals = {'getHead': Universal.getHead,
                    'getCSS': Universal.getCSS,
                    'getHeader': Universal.getHeader,
                    'getLogo': Universal.getLogo,
                    'getNavBar': Universal.getNavBar,
                    'getFooter': Universal.getFooter,
                    'getNavigation': Navigation.getNavigation,
                    'getContent': Content.getContent,
                    'getVersions': Versions.getVersions
                    }

引用 code.py

中的页面
class index:
    def GET(self):
        vPage = '0'
        vLevel = '0'
        vSection = '0'
        return Main.Layout(vPage, vLevel, vSection)

templates/Layout.html $def 与 (vPage, vLevel, vSection)

<html>
<head>
    $:getHead(vSection)
</head>
<body id="idBody">
    <table id="idTableMain">
        <tr id="idHeaderRow">
            <td id="idHeaderRowCenter" colspan="3">
                $:getHeader()
            </td>
        </tr>
        <tr id="idNavigationRow">
            <td id="idNavigationBar" colspan="3">
                $:getNavBar()
            </td>
        </tr>               
        <tr id="idCenterRow">
            <td id="idCenterRowLeft">
                <h4>
                    Navigation
                </h4>
                $:getNavigation(vLevel)
            </td>
            <td id="idCenterRowMain">
                $:getContent(vLevel+'_'+vPage+'P')
            </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>
                $:getVersions(vLevel+'_'+vPage+'P')
            </td>   
        </tr>
        <tr id="idFooterRow">
            <td id="idFooterMain" colspan="3">
                $:getFooter()
            </td>
        </tr>
    </table>
</body>
</html>

我知道这并不花哨,但这是一次很好的学习练习。唯一剩下的就是渲染器问题,而这更多是关于 Code.py

中工作发生的位置

我可能 post 将其作为一个单独的问题。