在html中寫python代碼的語法和特色-----基於webpy的httpserver

在html文件裏寫python語法的內容,的注意事項:

1:python程序中的變量經過如下方法傳入到html:
1:經過全局變量 :全局變量是不需要用$def with語法實現傳遞的,僅僅要定義了
在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)
===========================================================
2:使用模板的幾種方法:
1:直接使用html文件,並向index.html文件傳入python變量 ,樣例例如如下,
在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的方式,
================================================================
3:如下是python 在html文件裏的基本的語法  
1:獲得變量的值 ,注意僅僅是語法,沒有太多的爲何 
$varible
$(varible)
${varible}


2:在html文件裏建立新的變量 ,確定是在賦值時纔會建立新的變量 啊html

語法例如如下,$ 加上空格 加上變量名,空格很是重要
$ bug=True
$ va=1
<div>
$var
</div>


3: 在取變量的值的時候 ,你會看到兩種語法:python

第一種:    $a
另一種:    $:a
默認的python會使用web.websafe filter對變量作HTML-encoding.就是第一種方式,另一種方法不會對變量a作html-encoding


4: \ 這個符號的有點意思,會使多行的內容,僅僅顯示一行web

hello \
wolrd
注意:要在\ 這個符號後面當即敲enter,要否則 \的特殊含義會消失,而且會一塊兒顯示出來


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
上面定義的變量在這裏也可以用,
好比
$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模板中出現 post

import ,exec
訪問屬性時不能用 _開頭,
不能使用open,getattr,setattr這些函數
假設你的模板不當心用了上面的狀況,會出現SecurityException 這個安全
異常
知道上面的事,你就可以在html中寫python了
相關文章
相關標籤/搜索