Templates Webpy模板函数语法

Templates Webpy模板函数语法,templates,syntax,web.py,Templates,Syntax,Web.py,我正在尝试使用Webpy编写一个web应用程序。我的应用程序获取有关乙醚关闭或打开的输出的信息。我想用一个图像来显示“开”按钮或“关”按钮。 发送到Webpy模板的数据是一个输出字典(键定义输出)和一个值(字符串类型)。该值为“1”表示打开,为“0”表示关闭。 我的第一个想法是在模板中编写一个函数,根据如下值返回图像文件: 模板\u测试仪\u simple.py: import web render = web.template.render('templates/') urls = ('/',

我正在尝试使用Webpy编写一个web应用程序。我的应用程序获取有关乙醚关闭或打开的输出的信息。我想用一个图像来显示“开”按钮或“关”按钮。 发送到Webpy模板的数据是一个输出字典(键定义输出)和一个值(字符串类型)。该值为“1”表示打开,为“0”表示关闭。 我的第一个想法是在模板中编写一个函数,根据如下值返回图像文件:

模板\u测试仪\u simple.py:

import web
render = web.template.render('templates/')
urls = ('/', 'index')
template_tester_simple = web.application(urls, globals())


class index: 
    def GET(self):
        return render.test_func(data)


def add_data():
    data = {'currSet':'75','currTemp':'60','currMode':'Off',
'Cool':'1', 'Heat':'1', 'RevValve':'1', 'EmHeat':'1','Fan':'1',}            
    return data        

data = add_data()
if __name__=="__main__":
    web.internalerror = web.debugerror
    template_tester_simple.run()
$def with (data)

<!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>

    <body>
            <ul>
                <li><strong>Cooling</strong>
            $if data['Cool'] == '1':
                <img src="../static/switch_on.png" height="15px" width="40px"></img>
            $else:
                <img src="../static/switch_off.png" height="15px" width="40px"></img>
          </li>
            <li><strong>Heating</strong>
            $if data['Heat'] == '1':
                <img src="../static/switch_on.png" height="15px" width="40px"></img>
            $else:
                <img src="../static/switch_off.png" height="15px" width="40px"></img>
          </li>
           </ul>
        </body>
和我的test_func.html模板:

$def with (data)

<!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>
    $code:
        def getSwitchImg(item):
            x=""
        if (item=='1'):
            x="<img src= '../static/switch_on.png'></img>"
        else: "<img src= '../static/switch_off.png'></img>"
        return x

<body>
        <ul>
                <li><strong>Cooling</strong><p>
                $getSwitchImg($data['Cool'])
                </p></li>
                            <li><strong>Reversing Valve</strong><p>
                $getSwitchImg($data['RevValve'])
                </p></li>

        </ul>
</body>
$def带(数据)
试验
$code:
def getSwitchImg(项目):
x=“”
如果(项目=='1'):
x=“”
否则:“
返回x
  • 冷却 $getSwitchImg($data['Cool']))

  • 换向阀 $getSwitchImg($data['RevValve']))

这将返回语法错误,如下所示:

在/ 无效语法模板回溯:文件“templates/test_func.html”,第23行无(test_func.html,第23行)

我无法找出语法错误。它列出了一行作为无序列表的结束标记,这没有任何意义,因为它已经超过了所有python代码。所以这一定是WebPy模板系统中我不理解的东西。功能块是否未正确“关闭”? 出于沮丧,我随后使用相同的模板\u tester\u simple.py将模板更改为以下内容:

import web
render = web.template.render('templates/')
urls = ('/', 'index')
template_tester_simple = web.application(urls, globals())


class index: 
    def GET(self):
        return render.test_func(data)


def add_data():
    data = {'currSet':'75','currTemp':'60','currMode':'Off',
'Cool':'1', 'Heat':'1', 'RevValve':'1', 'EmHeat':'1','Fan':'1',}            
    return data        

data = add_data()
if __name__=="__main__":
    web.internalerror = web.debugerror
    template_tester_simple.run()
$def with (data)

<!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>

    <body>
            <ul>
                <li><strong>Cooling</strong>
            $if data['Cool'] == '1':
                <img src="../static/switch_on.png" height="15px" width="40px"></img>
            $else:
                <img src="../static/switch_off.png" height="15px" width="40px"></img>
          </li>
            <li><strong>Heating</strong>
            $if data['Heat'] == '1':
                <img src="../static/switch_on.png" height="15px" width="40px"></img>
            $else:
                <img src="../static/switch_off.png" height="15px" width="40px"></img>
          </li>
           </ul>
        </body>
$def带(数据)
试验
  • 冷却 $if数据['Cool']='1': $else:
  • 加热 $if数据['Heat']='1': $else:

这是可行的,但我没有定义我想要使用的函数。我被迫一次又一次地复制/粘贴基本相同的代码。随着我进一步开发这个项目,我计划添加更多的开关,从而增加更多的数字输出。如果我能得到第一个模板代码,它将使我的应用程序更容易扩展。我还想了解与WebPy模板中的函数相关联的语法。我已经阅读了Webpy网站上的templator教程。我认为,一旦您使用了“代码”:您使用了标准python语法,然后一旦您从缩进块中出来,它应该是标准HTML。有人能解释一下这个语法吗,以及我的错误。

我不知道这是否只是你复制/粘贴代码的方式,但我在这里看到了缩进问题

    def getSwitchImg(item):
        x=""
    if (item=='1'):
        x="<img src= '../static/switch_on.png'></img>"
    else: "<img src= '../static/switch_off.png'></img>"
    return x
def getSwitchImg(项目):
x=“”
如果(项目=='1'):
x=“”
否则:“
返回x
应该是:

    def getSwitchImg(item):
        x=""
        if (item=='1'):
           x="<img src= '../static/switch_on.png'></img>"
        else: 
           x="<img src= '../static/switch_off.png'></img>"
        return x
def getSwitchImg(项目):
x=“”
如果(项目=='1'):
x=“”
其他:
x=“”
返回x