自動(dòng)轉(zhuǎn)義

2018-02-24 15:39 更新

New in version 2.4.

從 Jinja 2.4 開(kāi)始,自動(dòng)轉(zhuǎn)義的首選途徑就是啟用?自動(dòng)轉(zhuǎn)義擴(kuò)展?并為自動(dòng)轉(zhuǎn)義配置一個(gè)合適的默認(rèn)值。這使得在單個(gè)模板基礎(chǔ)上開(kāi)關(guān)自動(dòng)轉(zhuǎn)義成為 可能(比如 HTML 對(duì) 文本)

這里推薦為以?.html?、?.htm?、?.xml?以及?.xhtml?的模板開(kāi)啟 自動(dòng)轉(zhuǎn)義 ,并對(duì)所有其它擴(kuò)展名禁用:

def guess_autoescape(template_name):
    if template_name is None or '.' not in template_name:
        return False
    ext = template_name.rsplit('.', 1)[1]
    return ext in ('html', 'htm', 'xml')

env = Environment(autoescape=guess_autoescape,
                  loader=PackageLoader('mypackage'),
                  extensions=['jinja2.ext.autoescape'])

假設(shè)實(shí)現(xiàn)一個(gè)自動(dòng)轉(zhuǎn)義函數(shù),確保你也視?None?為有效模板名接受。這會(huì)在從字符 串生成模板時(shí)傳遞。

可以用?autoescape?塊在模板內(nèi)臨時(shí)地更改這種行為。(見(jiàn)?自動(dòng)轉(zhuǎn)義擴(kuò)展?)。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)