這些類可以用作未定義類型。?[Environment](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Environment "jinja2.Environment")?的構造函數接受一個可以是 那些類或一個[Undefined](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "jinja2.Undefined")?的自定義子類的?undefined?參數。無論何時, 這些對象創建或返回時,模板引擎都不能查出其名稱或訪問其屬性。未定義值上的 某些操作之后是允許的,而其它的會失敗。
最接近常規 Python 行為的是?StrictUndefined?,如果它是一個未定義對象, 它不允許除了測試之外的一切操作。
*class?*jinja2.Undefined[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "Permalink to this definition")
The default undefined type. This undefined type can be printed and iterated over, but every other access will raise an?[UndefinedError](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.UndefinedError "jinja2.UndefinedError"):
~~~
>>> foo = Undefined(name='foo')
>>> str(foo)
''
>>> not foo
True
>>> foo + 42
Traceback (most recent call last):
...
UndefinedError: 'foo' is undefined
~~~
_undefined_hint[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._undefined_hint "Permalink to this definition")
None?或給未定義對象的錯誤消息 unicode 字符串。
_undefined_obj[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._undefined_obj "Permalink to this definition")
None?或引起未定義對象創建的對象(例如一個屬性不存在)。
_undefined_name[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._undefined_name "Permalink to this definition")
未定義變量/屬性的名稱,如果沒有此類信息,留為?None?。
_undefined_exception[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._undefined_exception "Permalink to this definition")
未定義對象想要拋出的異常。這通常是?[UndefinedError](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.UndefinedError "jinja2.UndefinedError")?或?SecurityError?之一。
_fail_with_undefined_error(**args*,?***kwargs*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._fail_with_undefined_error "Permalink to this definition")
參數任意,調用這個方法時會拋出帶有由未定義對象上存儲的未定義 hint 生成的錯誤信息的?[_undefined_exception](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._undefined_exception "jinja2.Undefined._undefined_exception")?異常。
*class?*jinja2.DebugUndefined[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.DebugUndefined "Permalink to this definition")
An undefined that returns the debug info when printed.
~~~
>>> foo = DebugUndefined(name='foo')
>>> str(foo)
'{{ foo }}'
>>> not foo
True
>>> foo + 42
Traceback (most recent call last):
...
UndefinedError: 'foo' is undefined
~~~
*class?*jinja2.StrictUndefined[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.StrictUndefined "Permalink to this definition")
An undefined that barks on print and iteration as well as boolean tests and all kinds of comparisons. In other words: you can do nothing with it except checking if it’s defined using the?defined?test.
~~~
>>> foo = StrictUndefined(name='foo')
>>> str(foo)
Traceback (most recent call last):
...
UndefinedError: 'foo' is undefined
>>> not foo
Traceback (most recent call last):
...
UndefinedError: 'foo' is undefined
>>> foo + 42
Traceback (most recent call last):
...
UndefinedError: 'foo' is undefined
~~~
未定義對象由調用?[undefined](http://docs.jinkan.org/docs/jinja2/templates.html#undefined "undefined")?創建。
實現
[Undefined](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "jinja2.Undefined")?對象通過重載特殊的?__underscore__?方法實現。例如 默認的?[Undefined](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "jinja2.Undefined")?類實現?__unicode__?為返回一個空字符串,但?__int__?和其它會始終拋出異常。你可以自己通過返回?0?實現轉換為 int:
~~~
class NullUndefined(Undefined):
def __int__(self):
return 0
def __float__(self):
return 0.0
~~~
要禁用一個方法,重載它并拋出?[_undefined_exception](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._undefined_exception "jinja2.Undefined._undefined_exception")?。因 為這在未定義對象中非常常用,未定義對象有輔助方法?[_fail_with_undefined_error()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined._fail_with_undefined_error "jinja2.Undefined._fail_with_undefined_error")?自動拋出錯誤。這里的一個類 工作類似正規的?[Undefined](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "jinja2.Undefined")?,但它在迭代時阻塞:
>
>
> class NonIterableUndefined(Undefined):
>
> __iter__ = Undefined._fail_with_undefined_error
>
>
- 介紹
- 預備知識
- 安裝
- 基本 API 使用
- 實驗性的 Python 3 支持
- API
- 基礎
- Unicode
- 高層 API
- 自動轉義
- 標識符的說明
- 未定義類型
- 上下文
- 加載器
- 字節碼緩存
- 實用工具
- 異常
- 自定義過濾器
- 求值上下文
- 自定義測試
- 全局命名空間
- 低層 API
- 元 API
- 沙箱
- API
- 運算符攔截
- 模板設計者文檔
- 概要
- 變量
- 過濾器
- 測試
- 注釋
- 空白控制
- 轉義
- 行語句
- 模板繼承
- HTML 轉義
- 控制結構清單
- 導入上下文行為
- 表達式
- 內置過濾器清單
- 內置測試清單
- 全局函數清單
- 擴展
- 自動轉義擴展
- 擴展
- 添加擴展
- i18n 擴展
- 表達式語句
- 循環控制
- With 語句
- 自動轉義擴展
- 編寫擴展
- 集成
- Babel 集成
- Pylons
- TextMate
- Vim
- 從其它的模板引擎切換
- Jinja1
- Django
- Mako
- 提示和技巧
- Null-Master 退回
- 交替的行
- 高亮活動菜單項
- 訪問父級循環