企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
这些类可以用作未定义类型。 [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 > >