本文整理汇总了Python中jinja2.runtime.Undefined方法的典型用法代码示例。如果您正苦于以下问题:Python runtime.Undefined方法的具体用法?Python runtime.Undefined怎么用?Python runtime.Undefined使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2.runtime
的用法示例。
在下文中一共展示了runtime.Undefined方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: do_default
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def do_default(value, default_value=u'', boolean=False):
"""If the value is undefined it will return the passed default value,
otherwise the value of the variable:
.. sourcecode:: jinja
{{ my_variable|default('my_variable is not defined') }}
This will output the value of ``my_variable`` if the variable was
defined, otherwise ``'my_variable is not defined'``. If you want
to use default with variables that evaluate to false you have to
set the second parameter to `true`:
.. sourcecode:: jinja
{{ ''|default('the string was empty', true) }}
"""
if isinstance(value, Undefined) or (boolean and not value):
return default_value
return value
示例2: _render_as_html
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def _render_as_html(value, render_options, link_to):
"""Render an object as HTML.
See
---
:func:`render_as_html`
"""
if isinstance(value, str):
html = value
elif isinstance(value, _jinja_undefined):
html = "OMITTED"
else:
if hasattr(value, 'set_render_options'):
value.set_render_options(**render_options)
out = value.render('html')
if link_to:
value.set_render_options(leave_includes_src=('tex' in link_to),
render_includes=('pdf' in link_to),
switched_item_mode='separate files')
if 'tex' in link_to or 'pdf' in link_to: value.render('latex')
if 'pkl' in link_to: value.render('python')
else: # switchboards usually
out = value.render('html')
# Note: out is a dictionary of rendered portions
html = "<script>%(js)s</script>%(html)s" % out
return html
示例3: test_defined
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def test_defined(value):
"""Return true if the variable is defined:
.. sourcecode:: jinja
{% if variable is defined %}
value of variable: {{ variable }}
{% else %}
variable is not defined
{% endif %}
See the :func:`default` filter for a simple way to set undefined
variables.
"""
return not isinstance(value, Undefined)
示例4: test_undefined
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def test_undefined(value):
"""Like :func:`defined` but the other way round."""
return isinstance(value, Undefined)
示例5: do_xmlattr
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def do_xmlattr(_eval_ctx, d, autospace=True):
"""Create an SGML/XML attribute string based on the items in a dict.
All values that are neither `none` nor `undefined` are automatically
escaped:
.. sourcecode:: html+jinja
<ul{{ {'class': 'my_list', 'missing': none,
'id': 'list-%d'|format(variable)}|xmlattr }}>
...
</ul>
Results in something like this:
.. sourcecode:: html
<ul class="my_list" id="list-42">
...
</ul>
As you can see it automatically prepends a space in front of the item
if the filter returned something unless the second parameter is false.
"""
rv = u' '.join(
u'%s="%s"' % (escape(key), escape(value))
for key, value in iteritems(d)
if value is not None and not isinstance(value, Undefined)
)
if autospace and rv:
rv = u' ' + rv
if _eval_ctx.autoescape:
rv = Markup(rv)
return rv
示例6: fail_for_missing_callable
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def fail_for_missing_callable(string, name):
msg = string % name
if isinstance(name, Undefined):
try:
name._fail_with_undefined_error()
except Exception as e:
msg = '%s (%s; did you forget to quote the callable name?)' % (msg, e)
raise TemplateRuntimeError(msg)
示例7: __new__
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def __new__(cls, source,
block_start_string=BLOCK_START_STRING,
block_end_string=BLOCK_END_STRING,
variable_start_string=VARIABLE_START_STRING,
variable_end_string=VARIABLE_END_STRING,
comment_start_string=COMMENT_START_STRING,
comment_end_string=COMMENT_END_STRING,
line_statement_prefix=LINE_STATEMENT_PREFIX,
line_comment_prefix=LINE_COMMENT_PREFIX,
trim_blocks=TRIM_BLOCKS,
lstrip_blocks=LSTRIP_BLOCKS,
newline_sequence=NEWLINE_SEQUENCE,
keep_trailing_newline=KEEP_TRAILING_NEWLINE,
extensions=(),
optimized=True,
undefined=Undefined,
finalize=None,
autoescape=False,
enable_async=False):
env = get_spontaneous_environment(
block_start_string, block_end_string, variable_start_string,
variable_end_string, comment_start_string, comment_end_string,
line_statement_prefix, line_comment_prefix, trim_blocks,
lstrip_blocks, newline_sequence, keep_trailing_newline,
frozenset(extensions), optimized, undefined, finalize, autoescape,
None, 0, False, None, enable_async)
return env.from_string(source, template_class=cls)
示例8: __call__
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def __call__(self, *args, **kwargs):
context = self._template.new_context(dict(*args, **kwargs))
consume(self._template.root_render_func(context))
rv = context.vars['result']
if self._undefined_to_none and isinstance(rv, Undefined):
rv = None
return rv
示例9: is_undefined
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def is_undefined(obj):
"""Check if the object passed is undefined. This does nothing more than
performing an instance check against :class:`Undefined` but looks nicer.
This can be used for custom filters or tests that want to react to
undefined variables. For example a custom default filter can look like
this::
def default(var, default=''):
if is_undefined(var):
return default
return var
"""
from jinja2.runtime import Undefined
return isinstance(obj, Undefined)
示例10: _environment_sanity_check
# 需要导入模块: from jinja2 import runtime [as 别名]
# 或者: from jinja2.runtime import Undefined [as 别名]
def _environment_sanity_check(environment):
"""Perform a sanity check on the environment."""
assert issubclass(environment.undefined, Undefined), 'undefined must ' \
'be a subclass of undefined because filters depend on it.'
assert environment.block_start_string != \
environment.variable_start_string != \
environment.comment_start_string, 'block, variable and comment ' \
'start strings must be different'
assert environment.newline_sequence in ('\r', '\r\n', '\n'), \
'newline_sequence set to unknown line ending string.'
return environment