如何为Jinja2编写一个“连接器”扩展?

1 投票
1 回答
1325 浏览
提问于 2025-04-16 04:50

你好,我一直在尝试为jinja2创建一个扩展,这个扩展可以把多个项目用一个分隔符连接起来,同时跳过那些评估后为空白的项目(模板片段)。

这些片段有很多,而且你事先根本不知道哪些是非空的,哪些是空的。

听起来这是一件简单的事情,但我在jinja2中实现这个功能时遇到了很大的困难。可能部分原因是jinja不允许定义自定义的模板节点。

你有什么建议吗?下面是一个可以进行解析的代码片段,但缺少评估的部分。

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

1 个回答

4

内置的 joiner 类可能会有效吗?这里有一个来自文档的简单例子。

{% 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("|") %}
{% set fragment = gen_fragment1() %}
{% if fragment|trim is not "" %} 
    {{ pipe() }} {{ fragment }}
{% endif %}
...

你甚至可以把上面的模式封装成一个宏,以减少重复:

{% set pipe = joiner("|") %}
{{ print_if_notblank(pipe, gen_fragment1()) }}
{{ print_if_notblank(pipe, gen_fragment2()) }}
...

其中 print_if_notblank 是一个定义好的宏:

{% macro print_if_notblank(separator, content) %}
    {% if content|trim is not "" %}
        {{ separator() }} {{ content }}
    {% endif %}
{% endmacro %}

撰写回答