2017-08-11 79 views
0

好的,我已经学会了一点,现在能够在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 

回答

0

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类第2节呼叫的样子:

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

(实际上,我认为你不需要给Main打电话,只是渲染为return Section2.Layout(0, 2, 2)

重复其他部分并更新您的布局直接调用您的功能,它将工作。

+0

谢谢@pbuck!我一直在为字典找到一些东西,但不能拼凑在一起,这是我所需要的。期待尽可能地尝试。 – Kamurai

+0

对于Main和Section2的问题,你如何表达我是如何打算这样做的,但我一直得到像“没有项目名称布局存在于这里”,并不得不做Main.Section2 ....等等得到不同的模板。 我会在您的第一个建议后重新尝试。 :) – Kamurai

+0

我不确定你想用'Main'和'Section1'做什么。如果它们只是不同的页面,那么也许你应该使用单个渲染器('Main'),并且有该渲染器调用模板页面('Layout'),每次提供不同的参数值。如果它们代表不同的页面样式(如“主页”,“帮助页面”等),则可以使用具有不同模板页面('home.html','help.html'等)的相同渲染器('Main') )为每个特定页面实例提供不同的参数值(即Main.help(title =“登录帮助”,description = ...),Main.help('Privacy','...') – pbuck

0

信用真的去这个@pbuck。在code.py

字典:

参考页code.py

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

问题的第一部分是通过使用字典和更新的一切,以匹配解决模板/的layout.html $ DEF与(v第,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中工作的地方

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

+0

很高兴它为你工作!随意'接受'我的答案。 – pbuck