Skip to content

Consider adding |items filter #1561

@mitsuhiko

Description

@mitsuhiko

Currently the method of choice to iterate over dictionaries is foo.items() or foo|dictsort. The former requires that the item provided does in fact have an .items method which is sensible to assume in general. The dictsort filter does the same but also sorts.

In MiniJinja the issue came up that there is no .items implemented there as I did not intend on implementing this method there. In MiniJinja I would prefer to have a |pairs filter. The reason for this is twofold: on the one hand is .items() a Python-ism that shines through, on the other hand does it create this challenge that the same syntax does different things (method access vs item lookup). In Jinja2 this problem has been addressed by changing the order of priority between attribute and item access which requires users to use foo["items"] if they do not want to refer to the method. This has been a source of bugs in the past and as a result I was considering not making the same mistake again.

At the same time I do not really wish to create some new patterns in MiniJinja if they do not exist in Jinja2 itself.

My proposal is now to either implement .items() in MiniJinja regardless or to implement |pairs / |items in both. It would effectively just call .items() on the passed value.

The main benefit would be that it's easier to write templates in a common subset of Jinja2 that works also in languages that are not backed by Python objects.

Refs mitsuhiko/minijinja#32

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions