1:python程序中的變量通過以下方法傳入到html:
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的方式,
================================================================
1:得到變量的值 ,注意僅僅是語法,沒有太多的為什么
$varible $(varible) ${varible}
2:在html文件里創建新的變量 ,肯定是在賦值時才會創建新的變量 啊
語法例如以下,$ 加上空格 加上變量名,空格非常重要$ bug=True $ va=1 <div> $var </div>
3: 在取變量的值的時候 ,你會看到兩種語法:
第一種: $a另外一種: $:a
默認的python會使用web.websafe filter對變量做HTML-encoding.就是第一種方式,另外一種方法不會對變量a做html-encoding
4: \ 這個符號的有點意思,會使多行的內容,僅僅顯示一行
hello \wolrd
注意:要在\ 這個符號后面立即敲enter,要不然 \的特殊含義會消失,並且會一起顯示出來
5:問你個問題,怎樣在html文件里顯示$這個符號(由於給webpy當特殊的用了)
答案非常easy,輸入兩個$$即可了美元的符號是$$
親,上面僅僅會顯示一個$哦
6:在html中怎樣寫python風格的凝視呢,我說的不是<!這種凝視哦>
$#這是凝視,你在瀏覽器中是看不到的,webpy把這部分給filter了
7:到了控制流部分了, 注意的面的i want這一句的縮進,要大於兩個空格,
你用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:其他一些實用的東西 如,$def
還能夠在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 塊以下:
$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
上面定義的變量在這里也能夠用,
比如
$limit(x)
10:var塊,這是個比較難懂的東東,看以下的代碼
在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程序中,
python就能夠依據這些內容做很多其它的處理,
11:在html文件里能夠訪問的builtin 函數 和變量 ,經常使用的函數都是
能訪問的,如max,min,range,True,False也是能識別的,
與builtin相應的一個概念是詳細應用程序的globals變量或是函數,
怎樣使用這些globals變量或是函數能夠被全部的html templates訪問呢?
樣例例如以下:
import web import markdown globals={"markdown":markdown.markdown} render=web.template.render("tempaltes",globals=globals)這樣在全部的html文件里都能夠使用 makrdown這個函數了
感覺這個函數就像是builtin的一樣,
12:出於安全考慮,以下的命令不能在html模板中出現
import ,exec訪問屬性時不能用 _開頭,
不能使用open,getattr,setattr這些函數
假設你的模板不小心用了上面的情況,會出現SecurityException 這個安全
異常
知道上面的事,你就能夠在html中寫python了 ,