Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass context to test when using select #1762

Open
wants to merge 1 commit into
base: 3.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jinja2/filters.py
Expand Up @@ -1742,7 +1742,7 @@ def transfunc(x: V) -> V:
args = args[1 + off :]

def func(item: t.Any) -> t.Any:
return context.environment.call_test(name, item, args, kwargs)
return context.environment.call_test(name, item, args, kwargs, context)

except LookupError:
func = bool # type: ignore
Expand Down
12 changes: 12 additions & 0 deletions tests/test_regression.py
Expand Up @@ -736,6 +736,18 @@ def test_nested_loop_scoping(self, env):
)
assert tmpl.render() == "hellohellohello"

def test_pass_context_with_select(self, env):
@pass_context
def is_foo(ctx, s):
assert ctx is not None
return s == "foo"

env.tests["foo"] = is_foo
tmpl = env.from_string(
"{% for x in ['one', 'foo' ] | select('foo') %}{{ x }}{% endfor %}"
)
assert tmpl.render() == "foo"


@pytest.mark.parametrize("unicode_char", ["\N{FORM FEED}", "\x85"])
def test_unicode_whitespace(env, unicode_char):
Expand Down