Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:55:45+00:00 2026-06-16T00:55:45+00:00

How are extra args & kwargs handled for a Jinja2 macro? The documentation isn’t

  • 0

How are extra args & kwargs handled for a Jinja2 macro? The documentation isn’t exactly clear offhand.

For example, this is clearly wrong:

{% macro example_1(one, two, **kwargs) %}
    do macro stuff
{% endmacro %}

which results in

jinja2.exceptions.TemplateSyntaxError

TemplateSyntaxError: expected token 'name', got '**'

The documentation says:

kwargs

Like varargs but for keyword arguments. All unconsumed keyword arguments are stored in this special variable.

Unfortunately, any combo of extra keyword arguments is an error,

{% macro example_2(one, two) %}
    do macro stuff
{% endmacro %}

{{ example_2(one, two, test='test') }}

TypeError: macro 'example_2' takes no keyword keyword argument 'test'

I have no examples and am not poking about in the Jinja2 source code atm. The documentation isn’t clear to me at this point. Any thoughts appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:55:46+00:00Added an answer on June 16, 2026 at 12:55 am

    The trick is that kwargs has to be accessed at least once in any macro that should accept them. That is to say, you must call {{ kwargs }} once in macro body without declaring it in macro argument list. The same is true for {{ varargs }}.

    This will not work

    {% macro example_2(one, two) %}
        * {{one}} - {{two}}
    {% endmacro %}
    {{example_2(1, 2, test="Hello")}}
    

    This will

    {% macro example_2(one, two) %}
        * {{one}} - {{two}}
        * {{kwargs}}
    {% endmacro %}
    {{example_2(1, 2, test="Hello")}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added an extra field Specification in Catalog->Product->Data tab. This text is stored
def validate(request, *args, **kwargs): form_class = kwargs.pop('form_class') extra_args_func = kwargs.pop('callback', lambda request, *args, **kwargs:
Using python's optparse module I would like to add extra example lines below the
I'm using std::make_pair() for this example because pretty much any C++ programmer ought to
I wrote my first sample scala program and it looks like this: def main(args:
On my server, I'm using the standard example for Python (with an extra Hello
I found this Q&A one the Web: Q: Which is better a char, short
Let say i have function: <?php function myFunction($param1, $param2, /*0 or more extra args*/)
#!/bin/bash # this works: java '-XX:OnOutOfMemoryError=nohup bash -c service jira stop;service jira stop &'
Take as an example the following C# function: static void Main(string[] args) { var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.