Reputation: 179
I can not find how to pass multiple named arguments to jinja2 Extension. I want something like that:
{% some_extension foo='foo' bar='bar' %}
data
{% end_someextension %}
Upvotes: 4
Views: 1088
Reputation: 47
This is how I overcame this problem:
{% exttag 'main', scope='global', auto='root' %}{% endexttag %}
Parse method:
def parse(self, parser):
lineno = parser.stream.__next__().lineno
context = nodes.ContextReference()
key = parser.parse_expression()
parser.stream.skip_if('comma')
if parser.stream.skip_if('name:scope'):
parser.stream.skip(1)
scope = parser.parse_expression()
else:
scope = nodes.Const('page')
parser.stream.skip_if('comma')
if parser.stream.skip_if('name:auto'):
parser.stream.skip(1)
auto = parser.parse_expression()
else:
auto = nodes.Const(None)
args = [key,scope,auto,context]
body = parser.parse_statements(['name:endexttag'], drop_needle=True)
return nodes.CallBlock(self.call_method('_render_block', args),
[], [], body).set_lineno(lineno)
There may be a more elegant way to achieve this, but this worked for me without being overly complex. If you want the template writer to be able to specify keywords in any order you'll have to use a loop.
But this definitely allows you to use keywords arguments in an extension.
Upvotes: 1
Reputation: 407
{% macro some_extension(foo='foo', bar="bar") %}
{{ foo }}, {{bar}}
{% endmacro %}
Is this what you are looking for??
Upvotes: 0