1:經過全局變量 :全局變量是不需要用$def with語法實現傳遞的,僅僅要定義了2:使用模板的幾種方法:
在html中就可以用,樣例例如如下:
===================================================================
#模板公共變量,如下可以定義所有的html文件都要用到的變量 ,不需要複雜的
$def with (va,vb)
t_globals = {
'datestr': web.datestr,
'cookie': web.cookies,
"loginform": login,
"gposts":model.get_posts,
}
#指定模板文件夾,並設定公共模板,base="base"設定共用的base.html模板,
在./templates/base.html這個路徑 找到這個文件
render = web.template.render('templates', base='base', globals=t_globals)
=========================================================
2:經過在python程序中在render時傳入 ,樣例例如如下:
=========================================================
在python文件裏,
render=web.template.render("./")
class index:
def GET(self):
abc="world"
render.index(name=abc)
在index.html文件裏:
$def with (name)
hello $name
===========================================================
可以看到上面的樣例是在python文件裏對index()函數傳入了name,
而在index.html文件裏,要定義一個暫時變量,接受這個傳入的變量
abc是python中的變量的名字
name是html文件裏變量的名字,
在render.index(name=abc)實現了變量的傳遞 ,
注意:在 python中render.index(a,b)可以傳遞多個變量
那麼在 html文件裏就要聲明相應的暫時變量 $def with (va,vb)
===========================================================
1:直接使用html文件,並向index.html文件傳入python變量 ,樣例例如如下,3:如下是python 在html文件裏的基本的語法
在python中:
render=web.template.render("templates") class index: def GET(self): return reder.index("wolrd")#templates是文件夾,到時把所有html文件放在templates文件夾下,如要用到的index.html
2:直接指定詳細的文件,這種方法擴展行很差,
hello=web.template.frender("templates/hello.html") return hello("world")3:使用字符串
html="$def with (name)\n hello $name" hello=web.tempate.Template(html) return hello("wolrd")================================================================
可以看到調用了template的三種方法:
render=web.template.render("templates")僅僅指定html文件的路徑 render.index("world") hello=web.tempalte.frender("templates/hello.html")指定了詳細的html文件 hello("world") hello=web.template.Template(string)直接把字符串傳入進去, hello("world")================================================================
上面三種方法最常常使用的是第一中,render.index的方式,
================================================================
$varible $(varible) ${varible}
2:在html文件裏建立新的變量 ,確定是在賦值時纔會建立新的變量 啊html
語法例如如下,$ 加上空格 加上變量名,空格很是重要$ bug=True $ va=1 <div> $var </div>
3: 在取變量的值的時候 ,你會看到兩種語法:python
第一種: $a
4: \ 這個符號的有點意思,會使多行的內容,僅僅顯示一行web
hello \
5:問你個問題,怎樣在html文件裏顯示$這個符號(因爲給webpy當特殊的用了)瀏覽器
答案很是easy,輸入兩個$$便可了
6:在html中怎樣寫python風格的凝視呢,我說的不是<!這種凝視哦>安全
$#這是凝視,你在瀏覽器中是看不到的,webpy把這部分給filter了
7:到了控制流部分了, 注意的面的i want這一句的縮進,要大於兩個空格,markdown
你用tab按鍵通常不會有問題$for i in range(10): i want eat $i apple(s) $ a=4 $while a<10: $a $ a+=1 $if a>10: hell $a $else: keep on ,you will do it 一個for 在 html應用中的樣例,這樣建立一個表 <table> $for c in ["a", "b", "c", "d"]: <tr class="abc"> <td>$index</td> <td>$c</td> </tr> </table>
8:其餘一些實用的東西 如,$defcookie
還可以在html中定義函數,這是多麼方便的東西$def tr(value): <tr> $for i in value: <td> $i </td> </tr> $def table(rows): <table> $for row in rows: $:row </table> $ data=[['a', 'b', 'c'], [1, 2, 3], [2, 4, 6], [3, 6, 9] ] $:table([tr(d) for d in data])
9:另外一個奇妙的 keyword code,所有的python代碼都可以寫在code 塊如下:app
$code: x = "you can write any python code here" y = x.title() z = len(x + y) def limit(s, width=10): """limits a string to the given width""" if len(s) >= width: return s[:width] + "..." else: return s回來到html
$def with (title, body) $var title: $title $var content_type: text/html <div id="body"> $body </div>在python中
>>> out = render.page('hello', 'hello world') >>> out.title u'hello' >>> out.content_type u'text/html' >>> str(out) '\n\n<div>\nhello world\n</div>\n'可以看到varkeyword的做用是把在 html中定義的變量,傳回到python程序中,
11:在html文件裏可以訪問的builtin 函數 和變量 ,常常使用的函數都是函數
能訪問的,如max,min,range,import web import markdown globals={"markdown":markdown.markdown} render=web.template.render("tempaltes",globals=globals)這樣在所有的html文件裏都可以使用 makrdown這個函數了
12:出於安全考慮,如下的命令不能在html模板中出現 post
import ,exec