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

时间:2017-08-11 02:39:09

标签: python function templates web.py

好的,我已经学到了一些东西,现在我可以在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)

模板/的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

1 个答案:

答案 0 :(得分:0)

在这个问题上真正归功于@pbuck。问题的第一部分通过使用dict并更新所有内容来解决:

在code.py中的dict:

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)

模板/的layout.html $ def with(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中工作发生的地方

我可能会将其作为一个单独的问题发布。