python脚本框架webpy模板控制结构有哪些

本篇内容主要讲解“python脚本框架webpy模板控制结构有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python脚本框架webpy模板控制结构有哪些”吧!

控制结构就是for,whileif-else,if-elif,while…else,在web.py中其实和我们以前学过的一样,操作基本是相同的,但是里面还是有一些不同!

for

$for row in range(10):   
        第$row行

$def with(funs)
$for row in funs:

第$row行

这里一定要记住funs不要添加$
如果funs是list,那$ros具体list的一些属性,在while中你可以看到

while

$while funs:
    $funs.pop()

funs是list,具体pop属性

if-else

$for row in range(10):
        $if row==2:
            我是2 
        $elif row==3:
            我是3
        $else:
            $row

实例

index.html中的内容

$def with(fun_name,funs)
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>web参数模板</title>
    </head>
    <body>
        <h3>我的名字:range函数的使用</h3>
        $for row in range(10):   
            第$row行 

        <h3>我的名字:$fun_name</h3>
        $# 注释
        $for row in funs:   
            第$row行 
        <h3>我的名字:while循环</h3>
        $while funs:
            $funs.pop()
        <br/>
       <h3>我的名字:if-else</h3>
        $for row in range(10):
            $if row==2:
                我是2 
            $elif row==4:
                我是4 
            $else:
                $row  
    </body>
</html>

Python中的内容:

#coding:utf-8
    import web
    urls=('/','Index',)
    render =web.template.render('html/')
    class Index:
        def funA(self):
            mylist=['1','2','3','4','5']
            return mylist
        def GET(self):
            web.header('Content-Type','text/html;charset=UTF-8')
            return render.myindex('for循环',[1,2,3,4,5,6,7,8,9,10])
    app=web.application(urls,globals())
    if __name__ == '__main__':
        app.run()

结果:

python脚本框架webpy模板控制结构有哪些

到此,相信大家对“python脚本框架webpy模板控制结构有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!