代码之家  ›  专栏  ›  技术社区  ›  Kamurai

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

  •  0
  • Kamurai  · 技术社区  · 7 年前

    好的,我学到了一点,现在可以在网上复制我的网站了。py,但我不喜欢在某些地方这样做。

    2) 我希望能够从布局中调用函数。

    以上任何建议都会很有帮助,提前谢谢你。

    代码.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>
    

    通用.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
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   pbuck    7 年前

    网状物py模板使用白名单来标识可以在模板内执行的函数。您可以通过创建dict并将其传递到渲染器来添加它。例如:

    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 Section2(object):
        def GET(self):
            return Main.Section2.Layout(page=0, level=2, section=2)
    

    (实际上,我认为您永远不需要调用Main,只需将其渲染为 return Section2.Layout(0, 2, 2)

    对其他部分重复上述步骤,并更新布局以直接调用函数,这样就可以了。

        2
  •  0
  •   Kamurai    7 年前

    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
                        }
    

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

    $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中工作发生在哪里