成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

web.py源碼分析: 模板(2)

figofuture / 3096人閱讀

摘要:上一篇文章源碼分析模板說明了的模板的大致工作原理。本文重點講述模板支持的語法是如何轉(zhuǎn)換生成函數(shù)的。模板的名稱統(tǒng)一是。模板代碼斷行模板內(nèi)容函數(shù)內(nèi)容從結(jié)果來看,模板中的斷行只是為了不再結(jié)果中插入一個多余的換行符而已。

上一篇文章web.py源碼分析: 模板(1)說明了web.py的模板的大致工作原理。本文重點講述web.py模板支持的語法是如何轉(zhuǎn)換生成__template__函數(shù)的。

web.py模板語法和__template__()函數(shù)的對應(yīng)關(guān)系

本章會列出模板內(nèi)容以及轉(zhuǎn)換之后的__template__()函數(shù)的內(nèi)容,以及必要的文字說明。模板的名稱統(tǒng)一是hello.html。

純字符串

模板內(nèi)容

hello, world

函數(shù)內(nèi)容

def __template__():
    __lineoffset__ = -5
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([u"hello, world
"])

    return self
def with

模板內(nèi)容

$def with (name, value=[], *args, **kargs)
hello, $name

函數(shù)內(nèi)容

def __template__ (name, value=[], *args, **kargs):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([u"hello, ", escape_(name, True), u"
"])

    return self
    

從生成的函數(shù)可以看出,def with語法所生成的就是__template__()函數(shù)的參數(shù)列表。

表達式替換

模板內(nèi)容

$def with (name, value)
$name
${name + value}
$(name + value)ing.
$name[value].function()

函數(shù)內(nèi)容

def __template__ (name, value):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([escape_(name, True), u"
"])
    extend_([escape_((name + value), True), u"
"])
    extend_([escape_((name + value), True), u"ing.
"])
    extend_([escape_(name[value].function(), True), u"
"])

    return self

表達式的替換就是執(zhí)行表達式(表達式對應(yīng)的代碼),得到的結(jié)果添加到TemplateResult實例中。

賦值

模板內(nèi)容

$def with (name, func)
$ name1 = name
$ name2 = func()

函數(shù)內(nèi)容

def __template__ (name, func):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    name1 = name
    name2 = func()

    return self

其實,轉(zhuǎn)換后就是Python代碼里的賦值語句。

內(nèi)容過濾

模板內(nèi)容

$def with (name)
$name
$:name

函數(shù)內(nèi)容

def __template__ (name):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([escape_(name, True), u"
"])
    extend_([escape_(name, False), u"
"])

    return self

從生成的代碼來看,是否使用過濾語法的區(qū)別就是傳入excape_函數(shù)的第二個參數(shù),這個函數(shù)其實只是一個字符串處理函數(shù),后續(xù)再說。

模板代碼斷行(newline suppression)

模板內(nèi)容

$def with (name, func)
$name
hello, 
$name 
!
$func(1, 2, 3, 4, 5)

函數(shù)內(nèi)容

def __template__ (name, func):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([escape_(name, True), u"
"])
    extend_([u"hello, "])
    extend_([escape_(name, True), u" "])
    extend_([u"!
"])
    extend_([escape_(func(1, 2, 3, 4, 5), True), u"
"])

    return self

從結(jié)果來看,模板中的斷行只是為了不再結(jié)果中插入一個多余的換行符而已。另外,一個表達式的中間是不支持斷行的,就比如在模板中不能把func函數(shù)的參數(shù)列表寫成兩行。

$符號

模板內(nèi)容

$$

函數(shù)內(nèi)容

def __template__():
    __lineoffset__ = -5
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([u"$", u"
"])

    return self
    
注釋

模板內(nèi)容

$# comment line
hello, world.

函數(shù)內(nèi)容

def __template__():
    __lineoffset__ = -5
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([u"
"])
    extend_([u"hello, world.
"])

    return self

模板中注釋的行在生成的函數(shù)中只有一個換行符。

控制結(jié)構(gòu) for循環(huán)

模板內(nèi)容

$for i in range(10):
    I like $i

函數(shù)內(nèi)容

def __template__():
    __lineoffset__ = -5
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    for i in loop.setup(range(10)):
        extend_([u"I like ", escape_(i, True), u"
"])

return self

模板中的for循環(huán)被轉(zhuǎn)換成了代碼中的for循環(huán),而且用上了變量loop(ForLoop對象后續(xù)再來看)。

while循環(huán)

模板內(nèi)容

$def with (name_list)

$while name_list:
    hello, $name_list.pop()

函數(shù)內(nèi)容

def __template__ (name_list):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([u"
"])
    while name_list:
        extend_([u"hello, ", escape_(name_list.pop(), True), u"
"])

    return self
    

注意和for循環(huán)的區(qū)別,沒有使用loop變量。

for循環(huán)的loop變量

模板內(nèi)容

$def with (name_list)
$for name in name_list:
    $loop.index
    $loop.index0
    $loop.first
    $loop.last
    $loop.odd
    $loop.even
    $loop.parity
    $loop.parent
    hello, $name

$for i in range(10):
    $for name in name_list:
        $loop.parent
        hello, $name

函數(shù)內(nèi)容

def __template__ (name_list):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    for name in loop.setup(name_list):
        extend_([escape_(loop.index, True), u"
"])
        extend_([escape_(loop.index0, True), u"
"])
        extend_([escape_(loop.first, True), u"
"])
        extend_([escape_(loop.last, True), u"
"])
        extend_([escape_(loop.odd, True), u"
"])
        extend_([escape_(loop.even, True), u"
"])
        extend_([escape_(loop.parity, True), u"
"])
        extend_([escape_(loop.parent, True), u"
"])
        extend_([u"hello, ", escape_(name, True), u"
"])
        extend_([u"
"])
    for i in loop.setup(range(10)):
        for name in loop.setup(name_list):
            extend_([escape_(loop.parent, True), u"
"])
            extend_([u"hello, ", escape_(name, True), u"
"])

    return self

這里展示了loop變量的成員,以及嵌套循環(huán)的使用。

if-else

模板內(nèi)容

$def with (name_list)
$if name_list:
    $len(name_list)
$else:
    0

函數(shù)內(nèi)容

def __template__ (name_list):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    if name_list:
        extend_([escape_(len(name_list), True), u"
"])
    else:
        extend_([u"0
"])

    return self

elif語句也支持。

函數(shù)定義

模板內(nèi)容

$def with (name_list)

$def hello(name):
    hello, $name

$def hello_to_all(nlist):
    $for each in nlist:
        $hello(each)

$hello_to_all(name_list)

函數(shù)內(nèi)容

def __template__ (name_list):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([u"
"])
    __lineoffset__ -= 3
    def hello(name):
        self = TemplateResult(); extend_ = self.extend
        extend_([u"hello, ", escape_(name, True), u"
"])
        extend_([u"
"])
        return self
    __lineoffset__ -= 3
    def hello_to_all(nlist):
        self = TemplateResult(); extend_ = self.extend
        for each in loop.setup(nlist):
            extend_([escape_(hello(each), True), u"
"])
            extend_([u"
"])
        return self
    extend_([escape_(hello_to_all(name_list), True), u"
"])

    return self

模板對函數(shù)的支持其實就是定義內(nèi)部函數(shù)并且調(diào)用,每個內(nèi)部函數(shù)的返回結(jié)果也都是TemplateResult實例。

code

模板內(nèi)容

$def with (name_list)

$code:
  new_list = [x.upper() for x in name_list]
  def hello(name):
      return "hello, %s" % (name)

  more_new_list = []
  for each in new_list:
      more_new_list.append(hello(each))

$hello("everybody")
$len(more_new_list)

函數(shù)內(nèi)容

def __template__ (name_list):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    extend_([u"
"])

    new_list = [x.upper() for x in name_list]
    def hello(name):
        return "hello, %s" % (name)

    more_new_list = []
    for each in new_list:
        more_new_list.append(hello(each))

    extend_([escape_(hello("everybody"), True), u"
"])
    extend_([escape_(len(more_new_list), True), u"
"])

    return self

code的語法有點復雜,其內(nèi)部是用來定義原始的Python代碼的,有如下幾個特點:

code內(nèi)部定義的函數(shù)也是內(nèi)部函數(shù),在模板的其他地方可以調(diào)用,但是不會把結(jié)果存放在TemplateResult實例中返回。

code中定義的變量都會作為__template__()函數(shù)的局部變量,在模板的其他地方可以調(diào)用。

注意,code塊中不要使用print語句打印輸出(雖然默認已經(jīng)禁止了)。

var

模板內(nèi)容

$def with (name_list)
$var title: hi
$var title2: "hi"
$var name: $name_list[0]
$var name2: name_list[0]

函數(shù)內(nèi)容

def __template__ (name_list):
    __lineoffset__ = -4
    loop = ForLoop()
    self = TemplateResult(); extend_ = self.extend
    self["title"] = join_(u"hi")
    self["title2"] = join_(u""hi"")
    self["name"] = join_(escape_(name_list[0], True))
    self["name2"] = join_(u"name_list[0]")

    return self

var是用來為模板設(shè)置屬性的,從生成的代碼來看,就是為TemplateResult實例設(shè)置屬性。上面的模板內(nèi)容里有一些關(guān)鍵細節(jié):

var name: value中的冒號后面的內(nèi)容默認是作為字符串處理的,可以不用加引號,如果加了引號,則引號也會作為內(nèi)容的一部分。

如果要在冒號后面訪問一些變量值,需要使用$前綴。

總結(jié)

寫了這些web.py模板支持的語法和生成的代碼的對應(yīng)關(guān)系,希望有助于大家理解模板的語法,了解每種語法的用途,避免踩坑。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/45358.html

相關(guān)文章

  • web.py源碼分析: 模板(1)

    摘要:模板函數(shù)到底長什么樣下面我們就可以來看看模板函數(shù)到底長什么樣了。當然,首先得創(chuàng)建一個模板文件??偨Y(jié)通過打印中間結(jié)果和分析代碼,我們已經(jīng)大概知道了的模板是如何轉(zhuǎn)化成內(nèi)容的。下一篇文章會闡述模板的各種語法所對應(yīng)的動態(tài)函數(shù)內(nèi)容。 web.py模板的實現(xiàn)原理 web.py的模板實現(xiàn)利用了Python的可執(zhí)行對象的動態(tài)特性:根據(jù)模板內(nèi)容和渲染函數(shù)的參數(shù)創(chuàng)建一個函數(shù),該函數(shù)執(zhí)行的時候會返回一個Te...

    Rocko 評論0 收藏0
  • web.py源碼分析: 模板(3)

    摘要:前兩篇文章主要說明了的模板系統(tǒng)將模板文件處理后得到的結(jié)果函數(shù)。生成函數(shù)的代碼這個是模板生成過程中最長最復雜的一段,會應(yīng)用到的分析功能以及動態(tài)編譯功能。參數(shù)都是一個,表示還未解析的模板內(nèi)容。 前兩篇文章主要說明了web.py的模板系統(tǒng)將模板文件處理后得到的結(jié)果:__template__()函數(shù)。本文主要講述模板文件是如何變成__template__()函數(shù)的。 Render和frende...

    OnlyMyRailgun 評論0 收藏0
  • web.py源碼分析: application(1)

    摘要:本文主要分析的是庫的這個模塊中的代碼。將結(jié)果轉(zhuǎn)換成一個迭代器。函數(shù)函數(shù)的定義如下位置位置位置該函數(shù)的參數(shù)中就是,是路由映射表則是,是本次請求路徑。位置,如果是其他情況,比如直接指定一個類對象作為處理對象。 本文主要分析的是web.py庫的application.py這個模塊中的代碼。總的來說,這個模塊主要實現(xiàn)了WSGI兼容的接口,以便應(yīng)用程序能夠被WSGI應(yīng)用服務(wù)器調(diào)用。WSGI是We...

    edgardeng 評論0 收藏0
  • 基于Linux環(huán)境的Web.py框架介紹

    摘要:前言在文章基于環(huán)境搭建框架方法介紹中介紹了客戶端和服務(wù)器的交互過程,服務(wù)器接收客戶端的請求后,由應(yīng)用服務(wù)器對瀏覽器的請求進行處理,將生成的響應(yīng)傳遞給服務(wù)器,再由服務(wù)器返回給客戶端。 前言 在文章《基于Linux環(huán)境搭建Nginx+uWSGI+Python框架方法介紹》中介紹了客戶端和Web服務(wù)器的交互過程,Web服務(wù)器接收客戶端的請求后,由Web應(yīng)用服務(wù)器對瀏覽器的請求進行處理,將生成...

    caikeal 評論0 收藏0
  • newrelic python agent 源碼分析-1

    摘要:是應(yīng)用性能管理監(jiān)控解決方案提供商。目錄是列出的命令腳本所在目錄。包含文件如下的函數(shù)是命令執(zhí)行的入口。而對于硬件信息的檢測則由進行。文檔地址源碼仔細看下去,太復雜了。下一篇再分析一個請求到結(jié)束探針工作的完整過程吧。 Newrelic 是APM(Application Performance Management)(應(yīng)用性能管理/監(jiān)控)解決方案提供商。項目中,通常用它來追蹤應(yīng)用的性能。最近...

    szysky 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<