Python 如何写一个“";“细木工”;金甲2号的分机?
您好,我一直在尝试为jinja2创建一个扩展,该扩展将使用分隔符连接多个项目,同时跳过计算结果为空白的项目(模板片段) 其中有几个片段,你永远不会事先知道哪些片段是非空的,哪些片段是空的 听起来像是一项琐碎的任务,但我真的很难让这项工作在金贾2。可能部分原因是jinja不允许定义自定义模板节点 你有什么建议吗?下面是一个片段,它将完成解析工作,但缺少求值部分Python 如何写一个“";“细木工”;金甲2号的分机?,python,templates,jinja2,Python,Templates,Jinja2,您好,我一直在尝试为jinja2创建一个扩展,该扩展将使用分隔符连接多个项目,同时跳过计算结果为空白的项目(模板片段) 其中有几个片段,你永远不会事先知道哪些片段是非空的,哪些片段是空的 听起来像是一项琐碎的任务,但我真的很难让这项工作在金贾2。可能部分原因是jinja不允许定义自定义模板节点 你有什么建议吗?下面是一个片段,它将完成解析工作,但缺少求值部分 class JoinerExtension(Extension): """Template tag that joins non-
class JoinerExtension(Extension):
"""Template tag that joins non-whitespace (string) items
with a specified separator
Usage syntax:
{% joinitems separator='|' %}
....
{% separator %}
....
{% separator %}
....
{% endjoinitems %}
where value of "separator" within the joinitems tag
can be an expression, not necessarily a sting
"""
tags = set(['joinitems'])
def parse(self, parser):
"""parse function for the
joinitems template tag
"""
lineno = next(parser.stream).lineno
#1) read separator
separator = None
while parser.stream.current.type != 'block_end':
name = parser.stream.expect('name')
if name.value != 'separator':
parser.fail('found %r, "separator" expected' %
name.value, name.lineno,
exc=TemplateAssertionError)
# expressions
if parser.stream.current.type == 'assign':
next(parser.stream)
separator = parser.parse_expression()
else:
var = parser.stream.current
parser.fail('assignment expected after the separator' %
var.value, var.lineno,
exc=TemplateAssertionError)
#2) read the items
items = list()
end_tags = ['name:separator', 'name:endjoinitems']
while True:
item = parser.parse_statements(end_tags)
items.append(item)
if parser.stream.current.test('name:separator'):
next(parser.stream)
else:
next(parser.stream)
break
内置类是否可能工作?下面是文档中的一个简单示例
{% set pipe = joiner("|") %}
{% if categories %} {{ pipe() }}
Categories: {{ categories|join(", ") }}
{% endif %}
{% if author %} {{ pipe() }}
Author: {{ author() }}
{% endif %}
{% if can_edit %} {{ pipe() }}
<a href="?action=edit">Edit</a>
{% endif %}
您甚至可以将上述模式封装在宏中以减少重复:
{% set pipe = joiner("|") %}
{{ print_if_notblank(pipe, gen_fragment1()) }}
{{ print_if_notblank(pipe, gen_fragment2()) }}
...
其中print\u if\u notblank
是一个宏,定义如下:
{% macro print_if_notblank(separator, content) %}
{% if content|trim is not "" %}
{{ separator() }} {{ content }}
{% endif %}
{% endmacro %}
内置类是否可能工作?下面是文档中的一个简单示例
{% set pipe = joiner("|") %}
{% if categories %} {{ pipe() }}
Categories: {{ categories|join(", ") }}
{% endif %}
{% if author %} {{ pipe() }}
Author: {{ author() }}
{% endif %}
{% if can_edit %} {{ pipe() }}
<a href="?action=edit">Edit</a>
{% endif %}
您甚至可以将上述模式封装在宏中以减少重复:
{% set pipe = joiner("|") %}
{{ print_if_notblank(pipe, gen_fragment1()) }}
{{ print_if_notblank(pipe, gen_fragment2()) }}
...
其中print\u if\u notblank
是一个宏,定义如下:
{% macro print_if_notblank(separator, content) %}
{% if content|trim is not "" %}
{{ separator() }} {{ content }}
{% endif %}
{% endmacro %}
这将起作用,现在使用内置函数处理策略性放置的{{pipe()}}调用。这将起作用,现在使用内置函数处理策略性放置的{pipe()}调用。